Created
December 30, 2012 07:08
-
-
Save scalolli/4411373 to your computer and use it in GitHub Desktop.
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
trait Function { | |
def name: String | |
} | |
case class ShellFunction(name: String) extends Function | |
case class ScalaFunction(name: String) extends Function | |
val l: List[Option[Function]] = List(None, Some(ShellFunction("build")), None) | |
val resultUsingForConstruct = for ( | |
i <- l; | |
j <- i | |
) yield (Some(ScalaFunction(j.name))) | |
val resultUsingFlatMap = l flatMap { | |
i: Option[Function] => i map { | |
j: Function => Some(ScalaFunction(j.name)) | |
} | |
} | |
Output from both the constructs: | |
Result using for construct: List(Some(ScalaFunction(build))) | |
Result using flatmap construct: List(Some(ScalaFunction(build))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment