Created
December 1, 2009 05:35
-
-
Save r/246094 to your computer and use it in GitHub Desktop.
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
def stream[T](f: => T, t:T => Boolean):Stream[T] = { | |
val v = f | |
if (t(v)) Stream.cons(v, stream(f, t)) else Stream.empty | |
} | |
def whileNotNull[T](f: => T):Stream[T] = stream(f, {t:T => t != null}) |
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
import java.io._ | |
val br = new BufferedReader(new FileReader("foo.txt")) | |
val stream = whileNotNull(br.readLine) // get the scala Stream | |
stream.foreach { line => System.out.println(line) } // print out each line of the file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment