Skip to content

Instantly share code, notes, and snippets.

@r
Created December 1, 2009 05:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r/246094 to your computer and use it in GitHub Desktop.
Save r/246094 to your computer and use it in GitHub Desktop.
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})
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