Skip to content

Instantly share code, notes, and snippets.

@godwhoa
Created February 6, 2019 07:24
Show Gist options
  • Save godwhoa/7248894792fb66afac4feb03cb12f622 to your computer and use it in GitHub Desktop.
Save godwhoa/7248894792fb66afac4feb03cb12f622 to your computer and use it in GitHub Desktop.
Lift scala
scala> val pf: PartialFunction[Int, Boolean] = { case i if i > 0 => i % 2 == 0}
pf: PartialFunction[Int,Boolean] = <function1>

scala> pf
res0: PartialFunction[Int,Boolean] = <function1>

scala> pf(-1)
scala.MatchError: -1 (of class java.lang.Integer)
  at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:259)
  at scala.PartialFunction$$anon$1.apply(PartialFunction.scala:257)
  at $anonfun$1.applyOrElse(<console>:11)
  at $anonfun$1.applyOrElse(<console>:11)
  at scala.runtime.AbstractPartialFunction$mcZI$sp.apply$mcZI$sp(AbstractPartialFunction.scala:38)
  ... 28 elided

scala> pf(1)
res2: Boolean = false

scala> pf.lift
res3: Int => Option[Boolean] = <function1>

scala> pf.lift(-1)
res4: Option[Boolean] = None

scala> pf.lift(1)
res5: Option[Boolean] = Some(false)

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