scala - Reusable streams from file -
scala - Reusable streams from file -
how create reusable stream file in scala? have huge file, , want utilize contents multiple times, may not need read whole file completely
i have tried this, without success,
// file iterator val f = source.fromfile("numberseq.txt").getlines // build stream file iterator def numseq: stream[bigint] = stream.cons(bigint(f.next()),numseq) //test numseq take 5 foreach println numseq take 5 foreach println //the stream continues print next file lines instead of going first line
the simplest way utilize tostream
right on iterator:
scala> val f = list(1,2,3,4,5,6,7,8,9,10).toiterator.tostream f: scala.collection.immutable.stream[int] = stream(1, ?) scala> f take 5 foreach println 1 2 3 4 5 scala> f take 5 foreach println 1 2 3 4 5
in concrete case, problem had whole new stream on every numseq
phone call because of def
used instead of val
. still need def
recursive definition, don't forget save val
before use:
scala> def numseq: stream[bigint] = stream.cons(bigint(f.next()),numseq) numseq: stream[bigint] scala> val numseq1 = numseq numseq1: stream[bigint] = stream(1, ?) scala> numseq1 take 5 foreach println 1 2 3 4 5 scala> numseq1 take 5 foreach println 1 2 3 4 5
example of wrong usage (notice numseq
instead of numseq1
):
scala> numseq take 5 foreach println 6 7 8 9 10 scala> numseq take 5 foreach println java.util.nosuchelementexception: next on empty iterator @ scala.collection.iterator$$anon$2.next(iterator.scala:39) @ scala.collection.iterator$$anon$2.next(iterator.scala:37) @ scala.collection.linearseqlike$$anon$1.next(linearseqlike.scala:59) @ .numseq(<console>:14) ... 33 elided
btw, there more cute #::
syntax cons
:
import stream._ def numseq: stream[bigint] = bigint(f.next()) #:: numseq val numseq1 = numseq
finally, version improve encapsulation:
val numseq = { def numseq: stream[bigint] = bigint(f.next()) #:: numseq numseq }
file scala io scala-collections scala-2.10
Comments
Post a Comment