All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vavr.Function8 Maven / Gradle / Ivy

There is a newer version: 1.0.0-alpha-4
Show newest version
/*                        __    __  __  __    __  ___
 *                       \  \  /  /    \  \  /  /  __/
 *                        \  \/  /  /\  \  \/  /  /
 *                         \____/__/  \__\____/__/.ɪᴏ
 * ᶜᵒᵖʸʳᶦᵍʰᵗ ᵇʸ ᵛᵃᵛʳ ⁻ ˡᶦᶜᵉⁿˢᵉᵈ ᵘⁿᵈᵉʳ ᵗʰᵉ ᵃᵖᵃᶜʰᵉ ˡᶦᶜᵉⁿˢᵉ ᵛᵉʳˢᶦᵒⁿ ᵗʷᵒ ᵈᵒᵗ ᶻᵉʳᵒ
 */
package io.vavr;

/*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*\
   G E N E R A T O R   C R A F T E D
\*-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/

import static io.vavr.Function8Module.sneakyThrow;

import io.vavr.control.Option;
import io.vavr.control.Try;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;

/**
 * Represents a function with 8 arguments.
 *
 * @param  argument 1 of the function
 * @param  argument 2 of the function
 * @param  argument 3 of the function
 * @param  argument 4 of the function
 * @param  argument 5 of the function
 * @param  argument 6 of the function
 * @param  argument 7 of the function
 * @param  argument 8 of the function
 * @param  return type of the function
 * @author Daniel Dietrich
 */
@FunctionalInterface
public interface Function8 extends Lambda {

    /**
     * The serial version uid.
     */
    long serialVersionUID = 1L;

    /**
     * Creates a {@code Function8} 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
     * 
* * @param methodReference (typically) a method reference, e.g. {@code Type::method} * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @param 6th argument * @param 7th argument * @param 8th argument * @return a {@code Function8} */ static Function8 of(Function8 methodReference) { return methodReference; } /** * Lifts the given {@code partialFunction} into a total function that returns an {@code Option} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @param 6th argument * @param 7th argument * @param 8th argument * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Some(result)} * if the function is defined for the given arguments, and {@code None} otherwise. */ @SuppressWarnings("RedundantTypeArguments") static Function8> lift(Function8 partialFunction) { return (t1, t2, t3, t4, t5, t6, t7, t8) -> Try.of(() -> partialFunction.apply(t1, t2, t3, t4, t5, t6, t7, t8)).toOption(); } /** * Lifts the given {@code partialFunction} into a total function that returns an {@code Try} result. * * @param partialFunction a function that is not defined for all values of the domain (e.g. by throwing) * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @param 6th argument * @param 7th argument * @param 8th argument * @return a function that applies arguments to the given {@code partialFunction} and returns {@code Success(result)} * if the function is defined for the given arguments, and {@code Failure(throwable)} otherwise. */ static Function8> liftTry(Function8 partialFunction) { return (t1, t2, t3, t4, t5, t6, t7, t8) -> Try.of(() -> partialFunction.apply(t1, t2, t3, t4, t5, t6, t7, t8)); } /** * Narrows the given {@code Function8} to {@code Function8} * * @param f A {@code Function8} * @param return type * @param 1st argument * @param 2nd argument * @param 3rd argument * @param 4th argument * @param 5th argument * @param 6th argument * @param 7th argument * @param 8th argument * @return the given {@code f} instance as narrowed type {@code Function8} */ @SuppressWarnings("unchecked") static Function8 narrow(Function8 f) { return (Function8) f; } /** * Applies this function to 8 arguments and returns the result. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @param t5 argument 5 * @param t6 argument 6 * @param t7 argument 7 * @param t8 argument 8 * @return the result of function application * */ R apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8); /** * Applies this function partially to one argument. * * @param t1 argument 1 * @return a partial application of this function */ default Function7 apply(T1 t1) { return (T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to two arguments. * * @param t1 argument 1 * @param t2 argument 2 * @return a partial application of this function */ default Function6 apply(T1 t1, T2 t2) { return (T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to three arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @return a partial application of this function */ default Function5 apply(T1 t1, T2 t2, T3 t3) { return (T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to 4 arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @return a partial application of this function */ default Function4 apply(T1 t1, T2 t2, T3 t3, T4 t4) { return (T5 t5, T6 t6, T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to 5 arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @param t5 argument 5 * @return a partial application of this function */ default Function3 apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { return (T6 t6, T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to 6 arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @param t5 argument 5 * @param t6 argument 6 * @return a partial application of this function */ default Function2 apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) { return (T7 t7, T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } /** * Applies this function partially to 7 arguments. * * @param t1 argument 1 * @param t2 argument 2 * @param t3 argument 3 * @param t4 argument 4 * @param t5 argument 5 * @param t6 argument 6 * @param t7 argument 7 * @return a partial application of this function */ default Function1 apply(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) { return (T8 t8) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } @Override default int arity() { return 8; } /** * Returns a function that always returns the constant * value that you give in parameter. * * @param generic parameter type 1 of the resulting function * @param generic parameter type 2 of the resulting function * @param generic parameter type 3 of the resulting function * @param generic parameter type 4 of the resulting function * @param generic parameter type 5 of the resulting function * @param generic parameter type 6 of the resulting function * @param generic parameter type 7 of the resulting function * @param generic parameter type 8 of the resulting function * @param the result type * @param value the value to be returned * @return a function always returning the given value */ static Function8 constant(R value) { return (t1, t2, t3, t4, t5, t6, t7, t8) -> value; } @Override default Function1>>>>>>> curried() { return t1 -> t2 -> t3 -> t4 -> t5 -> t6 -> t7 -> t8 -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } @Override default Function1, R> tupled() { return t -> apply(t._1, t._2, t._3, t._4, t._5, t._6, t._7, t._8); } @Override default Function8 reversed() { return (t8, t7, t6, t5, t4, t3, t2, t1) -> apply(t1, t2, t3, t4, t5, t6, t7, t8); } @Override default Function8 memoized() { if (isMemoized()) { return this; } else { final Map, R> cache = new HashMap<>(); return (Function8 & Memoized) (t1, t2, t3, t4, t5, t6, t7, t8) -> Memoized.of(cache, Tuple.of(t1, t2, t3, t4, t5, t6, t7, t8), tupled()); } } /** * Returns a composed function that first applies this Function8 to the given argument and then applies * {@linkplain Function} {@code after} to the result. * * @param return type of after * @param after the function applied after this * @return a function composed of this and after * @throws NullPointerException if after is null */ default Function8 andThen(Function after) { Objects.requireNonNull(after, "after is null"); return (t1, t2, t3, t4, t5, t6, t7, t8) -> after.apply(apply(t1, t2, t3, t4, t5, t6, t7, t8)); } } interface Function8Module { // DEV-NOTE: we do not plan to expose this as public API @SuppressWarnings("unchecked") static R sneakyThrow(Throwable t) throws T { throw (T) t; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy