AbstractPartialFunction reformulates all operations of its supertraitPartialFunction in terms ofisDefinedAt andapplyOrElse.
This allows more efficient implementations in many cases: - optimizedorElse method supports chainedorElse in linear time, and with no slow-down if theorElse part is not needed. - optimizedlift 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.
Apply the body of this function to the argument.
Apply the body of this function to the argument.
the result of function application.
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 callingisDefinedAt on the resulting partial function may apply the first partial function and execute its side effect. For efficiency, it is recommended to callapplyOrElse instead ofisDefinedAt orapply.
the result type of the transformation function.
the transformation function
a partial function with the domain of this partial function narrowed by other partial function, which maps argumentsx tok(this(x)).
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 aPartialFunction then the otherandThen method is used (note its cautions).
the result type of the transformation function.
the transformation function
a partial function with the domain of this partial function, possibly narrowed by the specified function, which maps argumentsx tok(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 expressionpf.applyOrElse(x, default) is equivalent to
if(pf isDefinedAt x) pf(x) else default(x)except thatapplyOrElse method can be implemented more efficiently. For all partial function literals the compiler generates anapplyOrElse implementation which avoids double evaluation of pattern matchers and guards. This makesapplyOrElse the basis for the efficient implementation for many operations and scenarios, such as:
- combining partial functions intoorElse/andThen chains does not lead to excessiveapply/isDefinedAt evaluation -lift andunlift 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 nontrivialisDefinedAt method it is recommended to overrideapplyOrElse with custom implementation that avoids doubleisDefinedAt evaluation. This may result in better performance and more predictable behavior w.r.t. side effects.
the fallback function
the function argument
the result of this function or fallback function application.
Composes another partial functionk with this partial function so that this partial function gets applied to results ofk.
Composes another partial functionk with this partial function so that this partial function gets applied to results ofk.
Note that callingisDefinedAt on the resulting partial function may apply the first partial function and execute its side effect. For efficiency, it is recommended to callapplyOrElse instead ofisDefinedAt orapply.
the parameter type of the transformation function.
the transformation function
a partial function with the domain of other partial function narrowed by this partial function, which maps argumentsx tothis(k(x)).
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
the type to which functiong can be applied
a function A => T1
a new functionf such thatf(x) == apply(g(x))
Returns an extractor object with aunapplySeq method, which extracts each element of a sequence data.
Returns an extractor object with aunapplySeq method, which extracts each element of a sequence data.
val firstChar: String => Option[Char] = _.headOptionSeq("foo", "bar", "baz") match { case firstChar.unlift.elementWise(c0, c1, c2) => println(s"$c0, $c1, $c2") // Output: f, b, b}Turns this partial function into a plain function returning anOption result.
Turns this partial function into a plain function returning anOption result.
a function that takes an argumentx toSome(this(x)) ifthis is defined forx, and toNone 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 andthat. The resulting partial function takesx tothis(x) wherethis is defined, and tothat(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 expressionpf.runWith(action)(x) is equivalent to
if(pf isDefinedAt x) { action(pf(x)); true } else falseexcept thatrunWith is implemented viaapplyOrElse and thus potentially more efficient. UsingrunWith avoids double evaluation of pattern matchers and guards for partial function literals.
the action function
a function which maps argumentsx toisDefinedAt(x). The resulting function runsaction(this(x)) wherethis is defined.
applyOrElse.
Returns a string representation of the object.
Returns a string representation of the object.
The default representation is platform dependent.
a string representation of the object.
Tries to extract aB from anA in a pattern matching expression.
Tries to extract aB from anA in a pattern matching expression.
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, iffx is in the domain of this function,false otherwise.