Skip to content

Instantly share code, notes, and snippets.

@scalolli
Created December 30, 2012 07:08
Show Gist options
  • Save scalolli/4411373 to your computer and use it in GitHub Desktop.
Save scalolli/4411373 to your computer and use it in GitHub Desktop.
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