Apply the body of this function to the argument.
Apply the body of this function to the argument.
the result of function application.
Checks if a value is contained in the function's domain.
Checks if a value is contained in the function's domain.
the value to test
true
, iff x
is in the domain of this function, false
otherwise.
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.
the result type of the transformation function.
the transformation function
a partial function with the same domain as this partial function, which maps
arguments x
to k(this(x))
.
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:
orElse
/andThen
chains does not lead to
excessive apply
/isDefinedAt
evaluationlift
and unlift
do not evaluate source functions twice on each invocationrunWith
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.
the function argument
the fallback function
the result of this function or fallback function application.
2.10
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.
the type to which function g
can be applied
a function A => T1
a new function f
such that f(x) == apply(g(x))
Returns string formatted according to given format
string.
Returns string formatted according to given format
string.
Format strings are as for String.format
(@see java.lang.String.format).
Turns this partial function into a plain function returning an Option
result.
Turns this partial function into a plain function returning an Option
result.
a function that takes an argument x
to Some(this(x))
if this
is defined for x
, and to None
otherwise.
Function.unlift
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.
the argument type of the fallback function
the result type of the fallback function
the fallback function
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.
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.
the action function
a function which maps arguments x
to isDefinedAt(x)
. The resulting function
runs action(this(x))
where this
is defined.
2.10
applyOrElse
.
A partial function of type
PartialFunction[A, B]
is a unary function where the domain does not necessarily include all values of typeA
. The functionisDefinedAt
allows to test dynamically if a value is in the domain of the function.Even if
isDefinedAt
returns true for ana: A
, callingapply(a)
may still throw an exception, so the following code is legal:The main distinction between
PartialFunction
and scala.Function1 is that the user of aPartialFunction
may choose to do something different with input that is declared to be outside its domain. For example:1.0, 16/07/2003