Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created November 27, 2010 16:57
Show Gist options
  • Save debasishg/718062 to your computer and use it in GitHub Desktop.
Save debasishg/718062 to your computer and use it in GitHub Desktop.
scala> val l = List(1,2,3)
l: List[Int] = List(1, 2, 3)
// pattern matching
scala> l match {
| case s if s.forall(even(_)) => Some(l)
| case _ => None
| }
res37: Option[List[Int]] = None
// better
scala> Some(l) filter (_.forall(even))
res38: Option[List[Int]] = None
scala> val l = List(2, 4, 6)
l: List[Int] = List(2, 4, 6)
scala> Some(l) filter (_.forall(even))
res39: Option[List[Int]] = Some(List(2, 4, 6))
@retronym
Copy link

scala> List(1, 2, 3).forall(even).guard[Option](l)
res2: Option[List[Int]] = None

scala> List(2, 4).forall(even).guard[Option](l)   
res3: Option[List[Int]] = Some(List(1, 2, 3))

@debasishg
Copy link
Author

cool ..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment