Created
April 5, 2012 01:58
-
-
Save raymondtay/2307391 to your computer and use it in GitHub Desktop.
Exploring Scala Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Looks like a good idea but ... | |
val toInfinityNBeyond = Stream.continually{1} | |
// when you abuse it, it can bring ur REPL to a halt | |
// and it doesn't do what u think it does anyway so ... | |
(0 /: toInfinityNBeyond)(_+_) | |
// you should see ur ALL ur CPUs spike to 100% | |
// Ctrl+C (kill it) | |
// If you know how many values to create in the first place, then you can do this ... | |
val max = 1000 | |
val finiteStreamFromOne = Stream.fill(max)(1) | |
// again, this expression doesn't do what you think it does as with List[_] because | |
// all the values are lost except for the last one | |
(0 /: finiteStreamFromOne)(_+_) | |
// To build a stream s.t. each subsequent element is a successor of the previous s.t. | |
// successor = previous + 1 | |
val iS = Stream.iterate(1, 99){ _ + 1 } | |
val result = (0 /: iS)(_ + _) | |
// result should read 4950 | |
// One operation i tried to do is to determine whether a finite Seq[_] is present | |
// in a infinite Stream[_]. The following expression runs into OutOfMemoryError: Java heap space | |
toInfinityNBeyond.containsSlice(1 to 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment