aaron.harnly.net

each_line do |line| in Scala

I’m so used to the each_line idiom in Ruby, I felt utterly lost in Scala without it.

Here’s an approximation of it, though I’d love to hear that there’s an even more scala-ish (scalic?) way of doing this:

def eachline(stream : InputStream)(f : String => Unit) { val buf = new BufferedReader( new InputStreamReader( stream ) ) var line = buf.readLine while (line != null) { f(line) line = buf.readLine } }

I use this with a block as so: eachline(System.in) { line => Console.println(line) }

Not bad, but it could be better, I think. Can I tickle this to be a method on streams, instead of a funny little function sitting on its own?

Digg this     Create a del.icio.us Bookmark     Add to Newsvine

No Responses to “each_line do |line| in Scala”

No comments yet

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word