Skip to content

Instantly share code, notes, and snippets.

@dwins
Created July 14, 2011 15:04
Show Gist options
  • Save dwins/1082626 to your computer and use it in GitHub Desktop.
Save dwins/1082626 to your computer and use it in GitHub Desktop.
def tryWithDefault[A, B](a: A, b: B)(pf: PartialFunction[A, B]): B =
if (pf isDefinedAt a) pf(a)
else b
println(tryWithDefault(1, 0) { case n if n % 2 == 0 => n / 2 }) // 0
println(tryWithDefault(12, 0) { case n if n % 2 == 0 => n / 2 }) // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment