AbstractPartialFunction

AbstractPartialFunction reformulates all operations of its supertrait PartialFunction in terms of isDefinedAt and applyOrElse.

This allows more efficient implementations in many cases: - optimized orElse method supports chained orElse in linear time, and with no slow-down if the orElse part is not needed. - optimized lift method helps to avoid double evaluation of pattern matchers & guards of partial function literals.

This trait is used as a basis for implementation of all partial function literals.

Source:
AbstractPartialFunction.scala
trait T1 => R
class Object
trait Matchable
class Any

Value members

Concrete methods

Inherited methods

Composes this partial function with another partial function that gets applied to results of this partial function.

Composes this partial function with another partial function that gets applied to results of this partial function.

Note that calling isDefinedAt on the resulting partial function may apply the first partial function and execute its side effect. It is highly recommended to call applyOrElse instead of isDefinedAt / apply for efficiency.

Type parameters:
C

the result type of the transformation function.

Value parameters:
k

the transformation function

Returns:

a partial function with the domain of this partial function narrowed by other partial function, which maps arguments x to k(this(x)).

Inherited from:
PartialFunction
Source:
PartialFunction.scala
override def andThen[C](k: R => C): PartialFunction[T1, C]

Composes this partial function with a transformation function that gets applied to results of this partial function.

Composes this partial function with a transformation function that gets applied to results of this partial function.

If the runtime type of the function is a PartialFunction then the other andThen method is used (note its cautions).

Type parameters:
C

the result type of the transformation function.

Value parameters:
k

the transformation function

Returns:

a partial function with the domain of this partial function, possibly narrowed by the specified function, which maps arguments x to k(this(x)).

Definition Classes
Inherited from:
PartialFunction
Source:
PartialFunction.scala
def applyOrElse[A1 <: T1, B1 >: R](x: A1, default: A1 => B1): B1

Applies this partial function to the given argument when it is contained in the function domain.

Applies this partial function to the given argument when it is contained in the function domain. Applies fallback function where this partial function is not defined.

Note that expression pf.applyOrElse(x, default) is equivalent to

if(pf isDefinedAt x) pf(x) else default(x)

except that applyOrElse method can be implemented more efficiently. For all partial function literals the compiler generates an applyOrElse implementation which avoids double evaluation of pattern matchers and guards. This makes applyOrElse the basis for the efficient implementation for many operations and scenarios, such as:

- combining partial functions into orElse/andThen chains does not lead to excessive apply/isDefinedAt evaluation - lift and unlift do not evaluate source functions twice on each invocation - runWith allows efficient imperative-style combining of partial functions with conditionally applied actions

For non-literal partial function classes with nontrivial isDefinedAt method it is recommended to override applyOrElse with custom implementation that avoids double isDefinedAt evaluation. This may result in better performance and more predictable behavior w.r.t. side effects.

Value parameters:
default

the fallback function

x

the function argument

Returns:

the result of this function or fallback function application.

Inherited from:
PartialFunction
Source:
PartialFunction.scala

Composes another partial function k with this partial function so that this partial function gets applied to results of k.

Composes another partial function k with this partial function so that this partial function gets applied to results of k.

Note that calling isDefinedAt on the resulting partial function may apply the first partial function and execute its side effect. It is highly recommended to call applyOrElse instead of isDefinedAt / apply for efficiency.

Type parameters:
R

the parameter type of the transformation function.

Value parameters:
k

the transformation function

Returns:

a partial function with the domain of other partial function narrowed by this partial function, which maps arguments x to this(k(x)).

Inherited from:
PartialFunction
Source:
PartialFunction.scala
def compose[A](g: A => T1): A => R

Composes two instances of Function1 in a new Function1, with this function applied last.

Composes two instances of Function1 in a new Function1, with this function applied last.

Type parameters:
A

the type to which function g can be applied

Value parameters:
g

a function A => T1

Returns:

a new function f such that f(x) == apply(g(x))

Inherited from:
Function1
Source:
Function1.scala

Returns an extractor object with a unapplySeq method, which extracts each element of a sequence data.

Returns an extractor object with a unapplySeq method, which extracts each element of a sequence data.

Example:

val firstChar: String => Option[Char] = _.headOption
Seq("foo", "bar", "baz") match {
  case firstChar.unlift.elementWise(c0, c1, c2) =>
    println(s"$c0, $c1, $c2") // Output: f, b, b
}
Inherited from:
PartialFunction
Source:
PartialFunction.scala

Checks if a value is contained in the function's domain.

Checks if a value is contained in the function's domain.

Value parameters:
x

the value to test

Returns:

true, iff x is in the domain of this function, false otherwise.

Inherited from:
PartialFunction
Source:
PartialFunction.scala
def lift: T1 => Option[R]

Turns this partial function into a plain function returning an Option result.

Turns this partial function into a plain function returning an Option result.

Returns:

a function that takes an argument x to Some(this(x)) if this is defined for x, and to None otherwise.

See also:

Function.unlift

Inherited from:
PartialFunction
Source:
PartialFunction.scala
def orElse[A1 <: T1, B1 >: R](that: PartialFunction[A1, B1]): PartialFunction[A1, B1]

Composes this partial function with a fallback partial function which gets applied where this partial function is not defined.

Composes this partial function with a fallback partial function which gets applied where this partial function is not defined.

Type parameters:
A1

the argument type of the fallback function

B1

the result type of the fallback function

Value parameters:
that

the fallback function

Returns:

a partial function which has as domain the union of the domains of this partial function and that. The resulting partial function takes x to this(x) where this is defined, and to that(x) where it is not.

Inherited from:
PartialFunction
Source:
PartialFunction.scala
def runWith[U](action: R => U): T1 => Boolean

Composes this partial function with an action function which gets applied to results of this partial function.

Composes this partial function with an action function which gets applied to results of this partial function. The action function is invoked only for its side effects; its result is ignored.

Note that expression pf.runWith(action)(x) is equivalent to

if(pf isDefinedAt x) { action(pf(x)); true } else false

except that runWith is implemented via applyOrElse and thus potentially more efficient. Using runWith avoids double evaluation of pattern matchers and guards for partial function literals.

Value parameters:
action

the action function

Returns:

a function which maps arguments x to isDefinedAt(x). The resulting function runs action(this(x)) where this is defined.

See also:

applyOrElse.

Inherited from:
PartialFunction
Source:
PartialFunction.scala
override def toString(): String
Definition Classes
Inherited from:
Function1
Source:
Function1.scala
def unapply(a: T1): Option[R]

Tries to extract a B from an A in a pattern matching expression.

Tries to extract a B from an A in a pattern matching expression.

Inherited from:
PartialFunction
Source:
PartialFunction.scala