Class Functions
Most methods of this class implement constructs that can also be expressed with lambda expressions. However, lambda expressions have two downsides:
- Every lambda expression requires parameter names that are not already taken, polluting the local variable name space.
- When a
reference to a function object is captured for later delegation, it is not
checked for
nullright away; aNullPointerExceptionmay arise later and in a remote context.
Therefore, the operations in this class provide means for point-free and fail-fast function-level programming.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,B> Function <A, B> Returns an equivalent function with different argument and result types.static <A> AapplyAll(A start, Iterable<? extends UnaryOperator<A>> ops) Applies a sequence of unary operators in sequence to a start value.static <A> AapplyAll(A start, UnaryOperator<A>... ops) Applies a sequence of unary operators in sequence to a start value.static <A,B, C> BiFunction <A, B, C> binConstant(C result) Returns a constant binary function.static <A,B, C, D>
BiFunction<A, B, D> compose(BiFunction<? super A, ? super B, ? extends C> f, Function<? super C, ? extends D> g) Returns a binary function that applies two given functions in sequence.static <A,B, C, D, E>
BiFunction<A, C, E> compose(Function<? super A, ? extends B> f, Function<? super C, ? extends D> g, BiFunction<? super B, ? super D, ? extends E> h) Returns a binary function that applies three given functions in sequence.static <A,B> Function <A, B> constant(B result) Returns a constant function.curry(BiFunction<A, B, C> f) Returns a function that takes one argument of a binary function instantly, and one later.static <A,B> Function <A, B> Returns a function that substitutes a default value if a given function returns null.static <A,B> Predicate <A> Returns a predicate that checks whether a function returns null for an argument.static <A,B> Function <A, B> fail()Returns a function that always returns null.static <A,B, C> BiFunction <B, A, C> flip(BiFunction<A, B, C> f) Returns a binary function that calls another binary with the arguments flipped.static <A,B, C> Function <A, C> leftSection(BiFunction<? super A, ? super B, ? extends C> f, B b) Returns a function that calls a binary function with a fixed second argument.static <A,B, C> eu.bandm.tools.util.java.Functions.MemoizedBiFunction <A, B, C> memoize(BiFunction<A, B, C> fun) Returns a binary function that remembers its computed values indefinitely.static <A,B> Function <A, B> Returns a unary function that remembers its computed values indefinitely.static <A> BinaryOperator<A> merge()Returns a binary function that returns one of its arguments if they are equal.static <A,B> Function <A, B> Returns a function that tries two given functions in sequence, preferring the first if its result is non-null.static <A,B> Function <A, B> Returns a function that applies a given function only if a precondition is satisfied.static <A,B, C> Function <B, C> rightSection(BiFunction<? super A, ? super B, ? extends C> f, A a) Returns a function that calls a binary function with a fixed first argument.static <A,B> Function <A, B> Returns a function that applies a given function only if the argument is non-null.static <A> Function<A, A> trace(PrintStream out, String label) Returns a function that returns its argument, and prints it as a side-effect.static <A> Function<A, A> Returns a function that returns its argument, and prints it as a side-effect.static <A,B, C> BiFunction <A, B, C> Returns a binary function that applies its first argument to a unary function-valued function, and its second to the result.static <A,B> Function <A, B> Returns a function that is undefined for all arguments.
-
Method Details
-
undef
Returns a function that is undefined for all arguments.- Type Parameters:
A- the value typeB- the result type- Parameters:
n- a descriptive name for the function- Returns:
- a function that throws
IllegalArgumentExceptionfor any argument
-
curry
Returns a function that takes one argument of a binary function instantly, and one later.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
f- a binary function- Returns:
- a function that, when applied to
a, returns a function that, when applied tob, callsfwith the pair(a, b) - Throws:
NullPointerException- iffis null
-
uncurry
Returns a binary function that applies its first argument to a unary function-valued function, and its second to the result.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
f- a unary function-valued function- Returns:
- a function that, when applied to
(a, b), appliesftoaand the resulting intermediate function tob - Throws:
NullPointerException- iffis null
-
constant
Returns a constant function.This method may be preferrable to a lambda expression in some contexts, because no parameter has to be introduced.
- Type Parameters:
A- the type of argumentsB- the type of results- Parameters:
result- the specified result- Returns:
- a function that just returns
resultfor any argument
-
binConstant
Returns a constant binary function.This method may be preferrable to a lambda expression in some contexts, because no parameters have to be introduced.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
result- the specified result- Returns:
- a binary function that returns
resultfor any arguments
-
flip
Returns a binary function that calls another binary with the arguments flipped.- Type Parameters:
A- the first argument type (of the given function)B- the second argument type (of the given function)C- the result type- Parameters:
f- a binary function- Returns:
- a function that, when called with
(b, a)callsfwith(a, b)
-
rightSection
public static <A,B, Function<B,C> C> rightSection(BiFunction<? super A, ? super B, ? extends C> f, A a) Returns a function that calls a binary function with a fixed first argument.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
f- a binary functiona- the fixed first argument- Returns:
- a function that, when applied to
b, appliesfto the pair(a, b) - Throws:
NullPointerException- iffis null
-
leftSection
public static <A,B, Function<A,C> C> leftSection(BiFunction<? super A, ? super B, ? extends C> f, B b) Returns a function that calls a binary function with a fixed second argument.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
f- a binary functionb- the fixed second argument- Returns:
- a function that, when applied to
a, appliesfto the pair(a, b) - Throws:
NullPointerException- iffis null
-
compose
public static <A,B, BiFunction<A,C, D> B, composeD> (BiFunction<? super A, ? super B, ? extends C> f, Function<? super C, ? extends D> g) Returns a binary function that applies two given functions in sequence.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given functions are null.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the type of intermediate valuesD- the type of results- Parameters:
f- the binary function to apply firstg- the function to apply second- Returns:
- a function that applies
innerfollowed byouterto its arguments - Throws:
NullPointerException- ifforgis null
-
compose
public static <A,B, BiFunction<A,C, D, E> C, composeE> (Function<? super A, ? extends B> f, Function<? super C, ? extends D> g, BiFunction<? super B, ? super D, ? extends E> h) Returns a binary function that applies three given functions in sequence.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given functions are null.
- Type Parameters:
A- the first argument typeB- the first intermediate value typeC- the second argument typeD- the second intermediate value typeE- the type of results- Parameters:
f- the function to apply to first argumentsg- the function to apply to second argumentsh- the binary function to apply after both- Returns:
- a function that applies
fandgin unspecified order to its arguments, andhon the pair of results - Throws:
NullPointerException- ifforgorhis null
-
fail
Returns a function that always returns null.- Type Parameters:
A- the type of argumentsB- the thype of results- Returns:
- a function that returns
nullfor any argument
-
restrict
public static <A,B> Function<A,B> restrict(Function<? super A, ? extends B> fun, Predicate<? super A> cond) Returns a function that applies a given function only if a precondition is satisfied.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given functions are null.
- Type Parameters:
A- the argument typeB- the result type- Parameters:
fun- a function to call conditionallycond- a predicate specifying a precondition- Returns:
- a function that, when called with
a, callsfifcondreturnstrue, or returnsnullwithout callingfotherwise - Throws:
NullPointerException- iffunorcondis null
-
strict
Returns a function that applies a given function only if the argument is non-null.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the argument typeB- the result type- Parameters:
fun- a function to call conditionally- Returns:
- a function that, when called with non-null
a, callsf, or just returnsnullotherwise - Throws:
NullPointerException- iffunis null
-
override
public static <A,B> Function<A,B> override(Function<? super A, ? extends B> first, Function<? super A, ? extends B> second) Returns a function that tries two given functions in sequence, preferring the first if its result is non-null.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given functions are null.
- Type Parameters:
A- the argument typeB- the result type- Parameters:
first- the first function to trysecond- the second function to try- Returns:
- a function that calls
firstand returns the result if non-zero; otherwisesecondis called with the same argument. - Throws:
NullPointerException- iffirstorsecondis null
-
defaultTo
Returns a function that substitutes a default value if a given function returns null.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the type of argumentsB- the type of results- Parameters:
def- the specified default valuefun- a function- Returns:
- the result of
funif non-null; the default value otherwise - Throws:
NullPointerException- iffunis null
-
domain
Returns a predicate that checks whether a function returns null for an argument.This method may be preferrable to a lambda expression in some contexts, because it fails immediately if the given function is null.
- Type Parameters:
A- the type of argumentsB- the type of results- Parameters:
fun- a function- Returns:
- a predicate that returns
trueif and only iffunreturns a non-null value for the same argument - Throws:
NullPointerException- iffunis null
-
adjust
Returns an equivalent function with different argument and result types.- Type Parameters:
A- the desired argument typeB- the desired result type- Parameters:
f- the function- Returns:
- an equivalent function, but possibly with a more restricted argument type and/or a more general result type
-
trace
Returns a function that returns its argument, and prints it as a side-effect.- Type Parameters:
A- the value type- Parameters:
label- a label for printing- Returns:
- a function that returns its argument after printing it, prefixed
with
label, toSystem.err
-
trace
Returns a function that returns its argument, and prints it as a side-effect.- Type Parameters:
A- the value type- Parameters:
out- an output streamlabel- a label for printing- Returns:
- a function that returns its argument after printing it, prefixed
with
label, toout
-
memoize
Returns a unary function that remembers its computed values indefinitely.Instead of computing the result for the same argument (up to
Object.equals(java.lang.Object)) twice, the previous result is stored and retrieved.Behavior is unspecified for functions that are not both stateless and deterministic.
Storage of values may create a space leak; make sure to dispose of (all references to) the memoized function when no longer needed.
- Type Parameters:
A- the argument typeB- the result type- Parameters:
fun- binary function- Returns:
- a function that computes the same values, but only once
- Throws:
NullPointerException- iffunis null
-
memoize
public static <A,B, eu.bandm.tools.util.java.Functions.MemoizedBiFunction<A,C> B, memoizeC> (BiFunction<A, B, C> fun) Returns a binary function that remembers its computed values indefinitely.Instead of computing the result for the same pair of arguments (up to
Object.equals(java.lang.Object)) twice, the previous result is stored and retrieved.Behavior is unspecified for functions that are not both stateless and deterministic.
Storage of values may create a space leak; make sure to dispose of (all references to) the memoized function when no longer needed.
- Type Parameters:
A- the first argument typeB- the second argument typeC- the result type- Parameters:
fun- a binary function- Returns:
- a binary function that computes the same values, but only once
- Throws:
NullPointerException- iffunis null
-
applyAll
Applies a sequence of unary operators in sequence to a start value.- Type Parameters:
A- the type of values- Parameters:
start- the start valueops- a sequence of unary operators- Returns:
- the value resulting from applying the first operator to the start value, the second operator to that value, etc.
-
applyAll
Applies a sequence of unary operators in sequence to a start value.- Type Parameters:
A- the type of values- Parameters:
start- the start valueops- a sequence of unary operators- Returns:
- the value resulting from applying the first operator to the start value, the second operator to that value, etc.
-
merge
Returns a binary function that returns one of its arguments if they are equal.- Type Parameters:
A- the type of values- Returns:
- a binary function that returns one of its arguments if they are
equal, but throws
IllegalArgumentExceptionotherwise
-