Skip to content

Instantly share code, notes, and snippets.

@klaeufer
Last active August 29, 2015 14:17
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 klaeufer/350ae39af27d19b156c2 to your computer and use it in GitHub Desktop.
Save klaeufer/350ae39af27d19b156c2 to your computer and use it in GitHub Desktop.
Scala Iterator loop idiom (stateful): repeatedly produce a value until a condition is met
Iterator continually {
// ...
reader.readLine()
} takeWhile {
Option(_).isDefined
} foreach {
processExpr
}
replaces
while ((/* ... */ ; line = reader.readLine()) != null ; line) {
processExpr(line)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment