Class Functions


  • public class Functions
    extends java.lang.Object
    Utility methods for Functions

    Note that this class defines Partial Functions to be functions that return an Option of the result type, and has some methods for creating them.

    Since:
    1.1
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private Functions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <A,​B,​C>
      java.util.function.Function<C,​B>
      ap​(java.util.function.Function<C,​A> ca, java.util.function.Function<C,​java.util.function.Function<A,​B>> cab)
      Performs function application within a higher-order function (applicative functor pattern).
      static <A,​B>
      java.util.function.Function<java.util.function.Function<A,​B>,​B>
      apply​(A arg)
      Function that takes another function and applies it to the argument.
      static <A,​B>
      java.util.function.Function<java.util.function.Function<A,​B>,​B>
      apply​(java.util.function.Supplier<A> lazyA)
      Function that takes another function and applies it to the argument supplied by the parameter.
      static <A,​B,​C>
      java.util.function.Function<A,​C>
      compose​(java.util.function.Function<? super B,​? extends C> g, java.util.function.Function<? super A,​? extends B> f)
      Returns the composition of two functions.
      static <A,​B,​C>
      java.util.function.Function<A,​Option<C>>
      composeOption​(java.util.function.Function<? super B,​? extends Option<? extends C>> bc, java.util.function.Function<? super A,​? extends Option<? extends B>> ab)
      Compose two PartialFunctions into one.
      static <A,​B>
      java.util.function.Function<A,​B>
      constant​(B constant)
      Create a function ignores it's input an produces a constant value
      (package private) static <A> java.util.function.Predicate<A> countingPredicate​(int n)  
      static <A,​B,​C>
      java.util.function.Function<A,​java.util.function.Function<B,​C>>
      curried​(java.util.function.BiFunction<A,​B,​C> f2)
      Transforms a function that takes 2 arguments into a function that takes the first argument and return a new function that takes the second argument and return the final result.
      static <A,​B,​C>
      java.util.function.Function<B,​java.util.function.Function<A,​C>>
      flip​(java.util.function.Function<A,​java.util.function.Function<B,​C>> f2)
      Transforms a function from A -> (B -> C) into a function from B -> (A -> C).
      static <F,​T>
      T
      fold​(java.util.function.BiFunction<? super T,​F,​T> f, T zero, java.lang.Iterable<? extends F> elements)
      Apply f to each element in elements, with each application using the result of the previous application as the other argument to f.
      static <F,​S,​T extends S>
      T
      fold​(java.util.function.Function<Pair<S,​F>,​T> f, T zero, java.lang.Iterable<? extends F> elements)
      Apply f to each element in elements, with each application using the result of the previous application as the other argument to f.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      forMap​(java.util.Map<A,​B> map)
      Create a function that performs a map lookup returning None for null If you do not need a nondefaulted return result using a method reference is preferred map::get
      static <A,​B>
      java.util.function.Function<A,​B>
      forMapWithDefault​(java.util.Map<A,​B> map, B defaultValue)
      Create a function that performs a map lookup supplying a default value when a Map#get returns null If you do not need a defaulted return result using a method reference is preferred map::get
      static <D> java.util.function.Function<D,​Unit> fromConsumer​(java.util.function.Consumer<D> consumer)
      Get a function (with Unit return type) that calls the consumer for all inputs.
      (package private) static <D,​R>
      java.util.function.Function<D,​R>
      fromSupplier​(java.util.function.Supplier<R> supplier)
      Get a function that uses the Supplier as a factory for all inputs.
      static <A> java.util.function.Function<A,​A> identity()
      Returns the identity function.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      isInstanceOf​(java.lang.Class<B> cls)
      Partial Function that does a type check and matches if the value is of the right type.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      mapNullToOption​(java.util.function.Function<? super A,​? extends B> f)
      Maps a function that returns nulls into a Partial function that returns an Option of the result.
      private static <A,​B>
      Functions.Matcher<A,​B>
      matcher​(java.util.function.Function<? super A,​? extends Option<? extends B>>... fs)  
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1, java.util.function.Function<? super A,​? extends Option<? extends B>> f2)
      Creates a stack of matcher functions and returns the first result that matches.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1, java.util.function.Function<? super A,​? extends Option<? extends B>> f2, java.util.function.Function<? super A,​? extends Option<? extends B>> f3)
      Creates a stack of matcher functions and returns the first result that matches.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1, java.util.function.Function<? super A,​? extends Option<? extends B>> f2, java.util.function.Function<? super A,​? extends Option<? extends B>> f3, java.util.function.Function<? super A,​? extends Option<? extends B>> f4)
      Creates a stack of matcher functions and returns the first result that matches.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1, java.util.function.Function<? super A,​? extends Option<? extends B>> f2, java.util.function.Function<? super A,​? extends Option<? extends B>> f3, java.util.function.Function<? super A,​? extends Option<? extends B>> f4, java.util.function.Function<? super A,​? extends Option<? extends B>> f5, java.util.function.Function<? super A,​? extends Option<? extends B>>... fs)
      Creates a stack of matcher functions and returns the first result that matches.
      static <A> java.util.function.Function<A,​Option<A>> nullToOption()
      Function that turns null inputs into a none, and not-null inputs into some.
      static <A,​B>
      java.util.function.Function<A,​Option<B>>
      partial​(java.util.function.Predicate<? super A> p, java.util.function.Function<? super A,​? extends B> f)
      Create a PartialFunction from a Predicate and a Function.
      static <A,​B,​C>
      java.util.function.BiFunction<A,​B,​C>
      toBiFunction​(java.util.function.Function<Pair<A,​B>,​C> fpair)
      Converts a function that takes a pair of arguments to a function that takes two arguments
      static <A,​B>
      java.util.function.Function<A,​B>
      weakMemoize​(java.util.function.Function<A,​B> f)
      Takes a Function and memoizes (caches) the result for each input.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Functions

        private Functions()
    • Method Detail

      • compose

        public static <A,​B,​C> java.util.function.Function<A,​C> compose​(java.util.function.Function<? super B,​? extends C> g,
                                                                                         java.util.function.Function<? super A,​? extends B> f)
        Returns the composition of two functions. For f: A->B and g: B->C, composition is defined as the function h such that h(a) == g(f(a)) for each a.
        Type Parameters:
        A - The start type
        B - The intermediate type
        C - The finished type
        Parameters:
        g - the second function to apply, must not be null
        f - the first function to apply, must not be null
        Returns:
        the composition of f and g
        See Also:
        function composition
      • ap

        public static <A,​B,​C> java.util.function.Function<C,​B> ap​(java.util.function.Function<C,​A> ca,
                                                                                    java.util.function.Function<C,​java.util.function.Function<A,​B>> cab)
        Performs function application within a higher-order function (applicative functor pattern).
        Parameters:
        ca - A function to apply within a higher-order function.
        cab - The higher-order function to apply a function to.
        Returns:
        A new function after applying the given higher-order function to the given function.
        Since:
        4.0
      • fold

        public static <F,​T> T fold​(java.util.function.BiFunction<? super T,​F,​T> f,
                                         T zero,
                                         java.lang.Iterable<? extends F> elements)
        Apply f to each element in elements, with each application using the result of the previous application as the other argument to f. zero is used as the first 'result' value. The final result is returned.
        Type Parameters:
        F - the element type
        T - the final result type
        Parameters:
        f - the function to apply to all the elements
        zero - the starting point for the function
        elements - the series of which each element will be accumulated into a result
        Returns:
        the result of accumulating the application of f to all elements
        Since:
        1.1
      • fold

        public static <F,​S,​T extends S> T fold​(java.util.function.Function<Pair<S,​F>,​T> f,
                                                           T zero,
                                                           java.lang.Iterable<? extends F> elements)
        Apply f to each element in elements, with each application using the result of the previous application as the other argument to f. zero is used as the first 'result' value. The final result is returned.
        Type Parameters:
        F - the element type
        S - the accumulator function input type
        T - the return result type
        Parameters:
        f - the function to apply to all elements
        zero - the starting point for the function
        elements - the series of which each element will be accumulated into a result
        Returns:
        the result of accumulating the application of f to all elements
        Since:
        1.1
      • apply

        public static <A,​B> java.util.function.Function<java.util.function.Function<A,​B>,​B> apply​(A arg)
        Function that takes another function and applies it to the argument.
        Type Parameters:
        A - the argument and function input type
        B - the result type
        Parameters:
        arg - the argument that will be applied to any input functions
        Returns:
        a function that takes a function from A to B , applies the arg and returns the result
        Since:
        1.1
      • apply

        public static <A,​B> java.util.function.Function<java.util.function.Function<A,​B>,​B> apply​(java.util.function.Supplier<A> lazyA)
        Function that takes another function and applies it to the argument supplied by the parameter.
        Type Parameters:
        A - the type of the argument supplied, and the function input type
        B - the result type of the function
        Parameters:
        lazyA - the supplier of the argument that will be applied to any input functions
        Returns:
        a function that takes a function from A to B, applies the argument from the supplier and returns the result
        Since:
        2.0
      • isInstanceOf

        public static <A,​B> java.util.function.Function<A,​Option<B>> isInstanceOf​(java.lang.Class<B> cls)
        Partial Function that does a type check and matches if the value is of the right type.
        Type Parameters:
        A - the input type
        B - the type we expect it to be
        Parameters:
        cls - the type to check against, must not be null
        Returns:
        a function that returns a Some with the value if of the right type otherwise a None
        Since:
        1.2
      • partial

        public static <A,​B> java.util.function.Function<A,​Option<B>> partial​(java.util.function.Predicate<? super A> p,
                                                                                         java.util.function.Function<? super A,​? extends B> f)
        Create a PartialFunction from a Predicate and a Function.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        p - the predicate to test the value against, must not be null
        f - the function to apply if the predicate passes, must not be null
        Returns:
        a PartialFunction that tests the supplied predicate before applying the function.
        Since:
        1.2
      • composeOption

        public static <A,​B,​C> java.util.function.Function<A,​Option<C>> composeOption​(java.util.function.Function<? super B,​? extends Option<? extends C>> bc,
                                                                                                       java.util.function.Function<? super A,​? extends Option<? extends B>> ab)
        Compose two PartialFunctions into one.

        Kleisli composition. In Haskell it is defined as >=>, AKA "compose, fishy, compose"

        Type Parameters:
        A - the input type
        B - the middle type
        C - the output type
        Parameters:
        bc - partial function from B -> C, must not be null
        ab - partial function from A -> B, must not be null
        Returns:
        a PartialFunction that flatMaps g on to the result of applying f.
        Since:
        1.2
      • toBiFunction

        public static <A,​B,​C> java.util.function.BiFunction<A,​B,​C> toBiFunction​(java.util.function.Function<Pair<A,​B>,​C> fpair)
        Converts a function that takes a pair of arguments to a function that takes two arguments
        Type Parameters:
        A - the type of the left of the pair
        B - the type of the right of the pair
        C - the result type
        Parameters:
        fpair - the source function that takes a pair of arguments, must not be null
        Returns:
        a function that takes two arguments
        Since:
        2.0
      • curried

        public static <A,​B,​C> java.util.function.Function<A,​java.util.function.Function<B,​C>> curried​(java.util.function.BiFunction<A,​B,​C> f2)
        Transforms a function that takes 2 arguments into a function that takes the first argument and return a new function that takes the second argument and return the final result.
        Type Parameters:
        A - the type of the first argument
        B - the type of the second argument
        C - the type of the final result
        Parameters:
        f2 - the original function that takes 2 arguments, must not be null
        Returns:
        the curried form of the original function
        Since:
        2.0
      • flip

        public static <A,​B,​C> java.util.function.Function<B,​java.util.function.Function<A,​C>> flip​(java.util.function.Function<A,​java.util.function.Function<B,​C>> f2)
        Transforms a function from A -> (B -> C) into a function from B -> (A -> C).
        Type Parameters:
        A - the type of the first argument
        B - the type of the second argument
        C - the type of the final result
        Parameters:
        f2 - the original function from A -> (B -> C), must not be null
        Returns:
        the flipped form of the original function
        Since:
        2.0
      • mapNullToOption

        public static <A,​B> java.util.function.Function<A,​Option<B>> mapNullToOption​(java.util.function.Function<? super A,​? extends B> f)
        Maps a function that returns nulls into a Partial function that returns an Option of the result.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        f - the function that may return nulls
        Returns:
        a function that converts any nulls into Options
        Since:
        2.0
      • nullToOption

        public static <A> java.util.function.Function<A,​Option<A>> nullToOption()
        Function that turns null inputs into a none, and not-null inputs into some.
        Type Parameters:
        A - the input type
        Returns:
        a function that never returns nulls.
        Since:
        2.2.1
      • weakMemoize

        public static <A,​B> java.util.function.Function<A,​B> weakMemoize​(java.util.function.Function<A,​B> f)
        Takes a Function and memoizes (caches) the result for each input. This memoization is weak, so it shouldn't leak memory on its own, but equally it may expunge entries if no-one else is holding the reference in the meantime.

        NOTE: it is very important that the docs on the input type are read carefully. Failure to heed adhere to this will lead to unspecified behavior (bugs!)

        Type Parameters:
        A - the input type, like any cache, this type should be a value, that is it should be immutable and have correct hashcode and equals implementations.
        B - the output type
        Parameters:
        f - the function who's output will be memoized, must not be null
        Returns:
        a function that memoizes the results of the function using the input as a weak key
        Since:
        2.2
      • fromSupplier

        static <D,​R> java.util.function.Function<D,​R> fromSupplier​(java.util.function.Supplier<R> supplier)
        Get a function that uses the Supplier as a factory for all inputs.
        Type Parameters:
        D - the key type, ignored
        R - the result type
        Parameters:
        supplier - called for all inputs, must not be null
        Returns:
        the function
        Since:
        2.2
      • fromConsumer

        public static <D> java.util.function.Function<D,​Unit> fromConsumer​(java.util.function.Consumer<D> consumer)
        Get a function (with Unit return type) that calls the consumer for all inputs.
        Type Parameters:
        D - the key type
        Parameters:
        consumer - called for all inputs, must not be null
        Returns:
        the function
        Since:
        4.4.0
      • identity

        public static <A> java.util.function.Function<A,​A> identity()
        Returns the identity function. Consider using Function.identity()
        Type Parameters:
        A - a A object.
        Returns:
        a Function that retruns it's input.
      • constant

        public static <A,​B> java.util.function.Function<A,​B> constant​(B constant)
        Create a function ignores it's input an produces a constant value
        Type Parameters:
        A - type of the ignored input
        B - type of the constant returned
        Parameters:
        constant - value to return
        Returns:
        a function producing a constant value
      • forMap

        public static <A,​B> java.util.function.Function<A,​Option<B>> forMap​(java.util.Map<A,​B> map)
        Create a function that performs a map lookup returning None for null If you do not need a nondefaulted return result using a method reference is preferred map::get
        Type Parameters:
        A - map key type
        B - map value type
        Parameters:
        map - map to use for lookup
        Returns:
        result of calling Map#get replacing null with none
        See Also:
        to supply a default value for none
      • forMapWithDefault

        public static <A,​B> java.util.function.Function<A,​B> forMapWithDefault​(java.util.Map<A,​B> map,
                                                                                           B defaultValue)
        Create a function that performs a map lookup supplying a default value when a Map#get returns null If you do not need a defaulted return result using a method reference is preferred map::get
        Type Parameters:
        A - map key type
        B - map value type
        Parameters:
        map - map to use for lookup
        defaultValue - a B to use when the map returns null from Map#get.
        Returns:
        result of calling Map#get returning defaultValue instead if the result was null
      • matches

        public static <A,​B> java.util.function.Function<A,​Option<B>> matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f2)
        Creates a stack of matcher functions and returns the first result that matches.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        f1 - partial function, tried in order.
        f2 - partial function, tried in order.
        Returns:
        a PartialFunction that composes all the functions and tries each one in sequence.
        Since:
        1.2
      • matches

        public static <A,​B> java.util.function.Function<A,​Option<B>> matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f2,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f3)
        Creates a stack of matcher functions and returns the first result that matches.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        f1 - partial function, tried in order.
        f2 - partial function, tried in order.
        f3 - partial function, tried in order.
        Returns:
        a PartialFunction that composes all the functions and tries each one in sequence.
        Since:
        1.2
      • matches

        public static <A,​B> java.util.function.Function<A,​Option<B>> matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f2,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f3,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f4)
        Creates a stack of matcher functions and returns the first result that matches.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        f1 - partial function, tried in order.
        f2 - partial function, tried in order.
        f3 - partial function, tried in order.
        f4 - partial function, tried in order.
        Returns:
        a PartialFunction that composes all the functions and tries each one in sequence.
        Since:
        1.2
      • matches

        @SafeVarargs
        public static <A,​B> java.util.function.Function<A,​Option<B>> matches​(java.util.function.Function<? super A,​? extends Option<? extends B>> f1,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f2,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f3,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f4,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>> f5,
                                                                                         java.util.function.Function<? super A,​? extends Option<? extends B>>... fs)
        Creates a stack of matcher functions and returns the first result that matches.
        Type Parameters:
        A - the input type
        B - the output type
        Parameters:
        f1 - partial function, tried in order.
        f2 - partial function, tried in order.
        f3 - partial function, tried in order.
        f4 - partial function, tried in order.
        f5 - partial function, tried in order.
        fs - partial functions, tried in order.
        Returns:
        a PartialFunction that composes all the functions and tries each one in sequence.
        Since:
        1.2
      • matcher

        @SafeVarargs
        private static <A,​B> Functions.Matcher<A,​B> matcher​(java.util.function.Function<? super A,​? extends Option<? extends B>>... fs)
      • countingPredicate

        static <A> java.util.function.Predicate<A> countingPredicate​(int n)