Skip to content

Instantly share code, notes, and snippets.

@DeaconDesperado
Last active December 17, 2015 07:58
Show Gist options
  • Save DeaconDesperado/5576397 to your computer and use it in GitHub Desktop.
Save DeaconDesperado/5576397 to your computer and use it in GitHub Desktop.
An example of for-yield flatmapping in scala.
val inputList = List("hi","hello","Bye");
val salutations = List("guvnor","sir","madame","dude","buddy")
def isFormal(ele:String )= ele match{
case "guvnor"=>true
case "sir"=>true
case "madame"=>true
case _=>false
}
def isGreeting(ele:String) = ele match{
case "hi" => true
case "hello"=> true
case _ => false
}
def greetEveryone(greetings: List[String], salutations: List[String]){
for{
greet <- inputList
sal <- salutations
if isGreeting(greet)
if isFormal(sal)
}yield{
println(greet+" "+sal)
}
}
greetEveryone(inputList,salutations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment