Last active
December 12, 2015 01:18
-
-
Save Javran/4690011 to your computer and use it in GitHub Desktop.
taking advantage of lazy evaluation and functional methodology to handle user interaction.
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 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