Skip to content

Instantly share code, notes, and snippets.

@Javran
Last active December 12, 2015 01:18
Show Gist options
  • Save Javran/4690011 to your computer and use it in GitHub Desktop.
Save Javran/4690011 to your computer and use it in GitHub Desktop.
taking advantage of lazy evaluation and functional methodology to handle user interaction.
import scala.io.Source
def getUserInputWhere(inputHint: ()=>Unit, cond: String=>Boolean) = {
Stream.continually[String]( {
inputHint()
readLine()
}).find( l => l == null || cond(l)).get
}
getUserInputWhere(
() => println( "Please input something more than 5 characters"),
l => l.length > 5)
match {
case null => println("User cancelled input")
case s:String => println("User enterred: " + s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment