A few handy operations which leverage the extra bit of information available in partial functions. Examples:
import PartialFunction._def strangeConditional(other: Any): Boolean = cond(other) { case x: String if x == "abc" || x == "def" => true case x: Int => true}def onlyInt(v: Any): Option[Int] = condOpt(v) { case x: Int => x }A Boolean test that is the result of the given function where defined, and false otherwise.
A Boolean test that is the result of the given function where defined, and false otherwise.
It behaves like acase _ => false were added to the partial function.
the partial function
the value to test
true, iffx is in the domain ofpf andpf(x) == true.
Apply the function to the given value if defined, and return the result in aSome; otherwise, returnNone.
Apply the function to the given value if defined, and return the result in aSome; otherwise, returnNone.
the PartialFunction[T, U]
the value to test
Some(pf(x)) ifpf isDefinedAt x,None otherwise.
The partial function with empty domain.
The partial function with empty domain. Any attempt to invoke empty partial function leads to throwingscala.MatchError exception.
Converts an ordinary function to a partial function.
Converts an ordinary function to a partial function. Note that callingisDefinedAt(x) on this partial function will returntrue for everyx.
an ordinary function
a partial function which delegates to the ordinary functionf