scala

object PartialFunction

[source: scala/PartialFunction.scala]

object PartialFunction
extends AnyRef
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 }
 
Author
Paul Phillips
Since
2.8
Method Summary
def cond [T](x : T)(pf : PartialFunction[T, Boolean]) : Boolean
Creates a Boolean test based on a value and a partial function. It behaves like a 'match' statement with an implied 'case _ => false' following the supplied cases.
def condOpt [T, U](x : T)(pf : PartialFunction[T, U]) : Option[U]
Transforms a PartialFunction[T,U] `pf' into Function1[T, Option[U]] `f' whose result is Some(x) if the argument is in pf's domain and None otherwise, and applies it to the value `x'. In effect, it is a 'match' statement which wraps all case results in Some(_) and adds 'case _ => None' to the end.
Methods inherited from AnyRef
getClass, hashCode, equals, clone, toString, notify, notifyAll, wait, wait, wait, finalize, ==, !=, eq, ne, synchronized
Methods inherited from Any
==, !=, isInstanceOf, asInstanceOf
Method Details
def cond[T](x : T)(pf : PartialFunction[T, Boolean]) : Boolean
Creates a Boolean test based on a value and a partial function. It behaves like a 'match' statement with an implied 'case _ => false' following the supplied cases.
Parameters
x - the value to test
pf - the partial function
Returns
true, iff x is in the domain of pf && pf(x) == true

def condOpt[T, U](x : T)(pf : PartialFunction[T, U]) : Option[U]
Transforms a PartialFunction[T,U] `pf' into Function1[T, Option[U]] `f' whose result is Some(x) if the argument is in pf's domain and None otherwise, and applies it to the value `x'. In effect, it is a 'match' statement which wraps all case results in Some(_) and adds 'case _ => None' to the end.
Parameters
x - the value to test
pf - the PartialFunction[T,U]
Returns
Some(pf(x)) iff (pf isDefinedAt x) and None otherwise