Skip to content

Instantly share code, notes, and snippets.

@noahlz
Last active August 29, 2015 13:56
Show Gist options
  • Save noahlz/9164763 to your computer and use it in GitHub Desktop.
Save noahlz/9164763 to your computer and use it in GitHub Desktop.
Learning about Scala's for comprehensions.
// See http://stackoverflow.com/questions/1052476/what-is-scalas-yield/1059501#1059501
// Compilation error is:
// [error] found : Seq[String]
// [error] required: Option[?]
// [error] child <- foo.children
// [error] ^
// [error] one error found
case class Foo(name: String, children: Seq[Foo])
object Yield extends App {
val c1 = Foo("child1", Nil)
val c2 = Foo("child2", Nil)
val c3 = Foo("child3", Nil)
val c4 = Foo("child4", Nil)
val parent1 = Foo("parent1", List(c1,c2,c3))
val parent2 = Foo("parent2", List(c4))
val fooz = List(parent1, parent2)
val found = for {
foo <- fooz.find(_.name == "parent1")
child <- foo.children
if child.name != "child2"
} yield child.name
println(s"Found: $found")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment