Skip to content

Instantly share code, notes, and snippets.

@seraphr
Created August 28, 2013 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seraphr/6367931 to your computer and use it in GitHub Desktop.
Save seraphr/6367931 to your computer and use it in GitHub Desktop.
collect!
case class Data(opt: Option[String], value: Int)
def d(o: String, v: Int) = Data(Some(o), v)
def n(v: Int) = Data(None, v)
val tList = List(d("a", 1), d("b", 2), n(10), d("c", 3), n(0))
tList.collect {
case Data(Some(o), v) => v
}
tList.collect {
case Data(Some(o), v) => (o, v)
}
scala> tList.collect {
| case Data(Some(o), v) => v
| }
res3: List[Int] = List(1, 2, 3)
scala> tList.collect {
| case Data(Some(o), v) => (o, v)
| }
res4: List[(String, Int)] = List((a,1), (b,2), (c,3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment