Last active
December 17, 2015 07:58
-
-
Save DeaconDesperado/5576397 to your computer and use it in GitHub Desktop.
An example of for-yield flatmapping in scala.
This file contains hidden or 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
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