Package io.vavr

Interface Function2<T1,​T2,​R>

  • Type Parameters:
    T1 - argument 1 of the function
    T2 - argument 2 of the function
    R - return type of the function
    All Superinterfaces:
    java.util.function.BiFunction<T1,​T2,​R>, java.io.Serializable
    Functional Interface:
    This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

    @FunctionalInterface
    public interface Function2<T1,​T2,​R>
    extends java.io.Serializable, java.util.function.BiFunction<T1,​T2,​R>
    Represents a function with two arguments.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long serialVersionUID
      The serial version UID for serialization.
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      default <V> Function2<T1,​T2,​V> andThen​(@NonNull java.util.function.Function<? super R,​? extends V> after)
      Returns a composed function that first applies this Function2 to the given argument and then applies Function after to the result.
      default Function1<T2,​R> apply​(T1 t1)
      Applies this function partially to one argument.
      R apply​(T1 t1, T2 t2)
      Applies this function to two arguments and returns the result.
      default int arity()
      Returns the number of function arguments.
      default <S> Function2<S,​T2,​R> compose1​(@NonNull Function1<? super S,​? extends T1> before)
      Returns a composed function that first applies the Function before to the 1st argument and then applies this Function2 to the result and the other argument.
      default <S> Function2<T1,​S,​R> compose2​(@NonNull Function1<? super S,​? extends T2> before)
      Returns a composed function that first applies the Function before to the 2nd argument and then applies this Function2 to the result and the other argument.
      static <T1,​T2,​R>
      Function2<T1,​T2,​R>
      constant​(R value)
      Returns a function that always returns the constant value that you give in parameter.
      default Function1<T1,​Function1<T2,​R>> curried()
      Returns a curried version of this function.
      default boolean isMemoized()
      Checks if this function is memoizing (= caching) computed values.
      static <T1,​T2,​R>
      Function2<T1,​T2,​Option<R>>
      lift​(@NonNull java.util.function.BiFunction<? super T1,​? super T2,​? extends R> partialFunction)
      Lifts the given partialFunction into a total function that returns an Option result.
      static <T1,​T2,​R>
      Function2<T1,​T2,​Try<R>>
      liftTry​(@NonNull java.util.function.BiFunction<? super T1,​? super T2,​? extends R> partialFunction)
      Lifts the given partialFunction into a total function that returns an Try result.
      default Function2<T1,​T2,​R> memoized()
      Returns a memoizing version of this function, which computes the return value for given arguments only one time.
      static <T1,​T2,​R>
      Function2<T1,​T2,​R>
      narrow​(Function2<? super T1,​? super T2,​? extends R> f)
      Narrows the given Function2<? super T1, ? super T2, ? extends R> to Function2<T1, T2, R>
      static <T1,​T2,​R>
      Function2<T1,​T2,​R>
      of​(@NonNull Function2<T1,​T2,​R> methodReference)
      Creates a Function2 based on method reference lambda expression Examples (w.l.o.g.
      default Function2<T2,​T1,​R> reversed()
      Returns a reversed version of this function.
      default Function1<Tuple2<T1,​T2>,​R> tupled()
      Returns a tupled version of this function.
    • Field Detail

      • serialVersionUID

        static final long serialVersionUID
        The serial version UID for serialization.
        See Also:
        Constant Field Values
    • Method Detail

      • constant

        static <T1,​T2,​R> Function2<T1,​T2,​R> constant​(R value)
        Returns a function that always returns the constant value that you give in parameter.
        Type Parameters:
        T1 - generic parameter type 1 of the resulting function
        T2 - generic parameter type 2 of the resulting function
        R - the result type
        Parameters:
        value - the value to be returned
        Returns:
        a function always returning the given value
      • of

        static <T1,​T2,​R> Function2<T1,​T2,​R> of​(@NonNull Function2<T1,​T2,​R> methodReference)
        Creates a Function2 based on Examples (w.l.o.g. referring to Function1):
        // using a lambda expression
         Function1<Integer, Integer> add1 = Function1.of(i -> i + 1);
        
         // using a method reference (, e.g. Integer method(Integer i) { return i + 1; })
         Function1<Integer, Integer> add2 = Function1.of(this::method);
        
         // using a lambda reference
         Function1<Integer, Integer> add3 = Function1.of(add1::apply);
         

        Caution: Reflection loses type information of lambda references.

        // type of a lambda expression
         Type<?, ?> type1 = add1.getType(); // (Integer) -> Integer
        
         // type of a method reference
         Type<?, ?> type2 = add2.getType(); // (Integer) -> Integer
        
         // type of a lambda reference
         Type<?, ?> type3 = add3.getType(); // (Object) -> Object
         
        Type Parameters:
        R - return type
        T1 - 1st argument
        T2 - 2nd argument
        Parameters:
        methodReference - (typically) a method reference, e.g. Type::method
        Returns:
        a Function2
      • lift

        static <T1,​T2,​R> Function2<T1,​T2,​Option<R>> lift​(@NonNull java.util.function.BiFunction<? super T1,​? super T2,​? extends R> partialFunction)
        Lifts the given partialFunction into a total function that returns an Option result.
        Type Parameters:
        R - return type
        T1 - 1st argument
        T2 - 2nd argument
        Parameters:
        partialFunction - a function that is not defined for all values of the domain (e.g. by throwing)
        Returns:
        a function that applies arguments to the given partialFunction and returns Some(result) if the function is defined for the given arguments, and None otherwise.
      • liftTry

        static <T1,​T2,​R> Function2<T1,​T2,​Try<R>> liftTry​(@NonNull java.util.function.BiFunction<? super T1,​? super T2,​? extends R> partialFunction)
        Lifts the given partialFunction into a total function that returns an Try result.
        Type Parameters:
        R - return type
        T1 - 1st argument
        T2 - 2nd argument
        Parameters:
        partialFunction - a function that is not defined for all values of the domain (e.g. by throwing)
        Returns:
        a function that applies arguments to the given partialFunction and returns Success(result) if the function is defined for the given arguments, and Failure(throwable) otherwise.
      • narrow

        static <T1,​T2,​R> Function2<T1,​T2,​R> narrow​(Function2<? super T1,​? super T2,​? extends R> f)
        Narrows the given Function2<? super T1, ? super T2, ? extends R> to Function2<T1, T2, R>
        Type Parameters:
        R - return type
        T1 - 1st argument
        T2 - 2nd argument
        Parameters:
        f - A Function2
        Returns:
        the given f instance as narrowed type Function2<T1, T2, R>
      • apply

        R apply​(T1 t1,
                T2 t2)
        Applies this function to two arguments and returns the result.
        Specified by:
        apply in interface java.util.function.BiFunction<T1,​T2,​R>
        Parameters:
        t1 - argument 1
        t2 - argument 2
        Returns:
        the result of function application
      • apply

        default Function1<T2,​R> apply​(T1 t1)
        Applies this function partially to one argument.
        Parameters:
        t1 - argument 1
        Returns:
        a partial application of this function
      • arity

        default int arity()
        Returns the number of function arguments.
        Returns:
        an int value >= 0
        See Also:
        Arity
      • curried

        default Function1<T1,​Function1<T2,​R>> curried()
        Returns a curried version of this function.
        Returns:
        a curried function equivalent to this.
      • tupled

        default Function1<Tuple2<T1,​T2>,​R> tupled()
        Returns a tupled version of this function.
        Returns:
        a tupled function equivalent to this.
      • reversed

        default Function2<T2,​T1,​R> reversed()
        Returns a reversed version of this function. This may be useful in a recursive context.
        Returns:
        a reversed function equivalent to this.
      • memoized

        default Function2<T1,​T2,​R> memoized()
        Returns a memoizing version of this function, which computes the return value for given arguments only one time. On subsequent calls given the same arguments the memoized value is returned.

        Please note that memoizing functions do not permit null as single argument or return value.

        Returns:
        a memoizing function equivalent to this.
      • isMemoized

        default boolean isMemoized()
        Checks if this function is memoizing (= caching) computed values.
        Returns:
        true, if this function is memoizing, false otherwise
      • andThen

        default <V> Function2<T1,​T2,​V> andThen​(@NonNull java.util.function.Function<? super R,​? extends V> after)
        Returns a composed function that first applies this Function2 to the given argument and then applies Function after to the result.
        Specified by:
        andThen in interface java.util.function.BiFunction<T1,​T2,​R>
        Type Parameters:
        V - return type of after
        Parameters:
        after - the function applied after this
        Returns:
        a function composed of this and after
        Throws:
        java.lang.NullPointerException - if after is null
      • compose1

        default <S> Function2<S,​T2,​R> compose1​(@NonNull Function1<? super S,​? extends T1> before)
        Returns a composed function that first applies the Function before to the 1st argument and then applies this Function2 to the result and the other argument.
        Type Parameters:
        S - argument type of before
        Parameters:
        before - the function applied before this
        Returns:
        a function composed of before and this
        Throws:
        java.lang.NullPointerException - if before is null
      • compose2

        default <S> Function2<T1,​S,​R> compose2​(@NonNull Function1<? super S,​? extends T2> before)
        Returns a composed function that first applies the Function before to the 2nd argument and then applies this Function2 to the result and the other argument.
        Type Parameters:
        S - argument type of before
        Parameters:
        before - the function applied before this
        Returns:
        a function composed of before and this
        Throws:
        java.lang.NullPointerException - if before is null