score:5

Accepted answer

as per stream.map documentation:

returns the stream resulting from applying the given function f to each element of this stream. this returns a lazy stream such that it does not need to be fully realized.

so using map does not evaluate the stream, it produces a new stream.

score:2

don't map but loop if you are after the side effect of println.

scala> var flist = stream(new java.io.file("foo1") , new java.io.file("foo2"))
flist: scala.collection.immutable.stream[java.io.file] = stream(foo1, ?)

scala> flist.foreach(f => println(f.getabsolutepath))
/tmp/foo1
/tmp/foo2

scala> flist take 2 foreach(f => println(f.getabsolutepath))
/tmp/foo1
/tmp/foo2

Related Query

More Query from same tag