Application de fonction partielle en Java 8


Je veux appliquer partiellement certains arguments à une méthode héritée en utilisant les objets de fonction nouvellement introduits de Java 8.

Voici la méthode en question:

/**
 * Appends a {@code character} a couple of {@code times} to a {@code string}.
 * 
 * @return the string with the appended characters as a StringBuilder
 */
private static StringBuilder appendChar(char character, int times, String string) {
    StringBuilder strBuilder = new StringBuilder(string);
    for (int i = 0; i < times; i++) {
        strBuilder.append(character);
    }
    return strBuilder;
}
Author: Matthias Braun, 2014-03-22

1 answers

Cela réalise ce que je voulais faire:

/*
 * Apply two arguments. The created function accepts a String and
 * returns a StringBuilder
 */
Function<String, StringBuilder> addEllipsis = appendToMe -> appendChar(
        '.', 3, appendToMe);
/*
 * Apply one argument. This creates a function that takes another two
 * arguments and returns a StringBuilder
 */
BiFunction<String, Integer, StringBuilder> addBangs = (appendToMe,
        times) -> appendChar('!', times, appendToMe);

// Create a function by applying one argument to another function
Function<String, StringBuilder> addOneBang = appendToMe -> addBangs
        .apply(appendToMe, 1);

StringBuilder res1 = addBangs.apply("Java has gone functional", 2);
StringBuilder res2 = addOneBang.apply("Lambdas are sweet");
StringBuilder res3 = addEllipsis.apply("To be continued");

Pour une liste de toutes les variétés prédéfinies de Java de l'objet function, regardez ici.

Modifier:

Si vous avez une méthode avec beaucoup d'arguments, vous pouvez écrire votre propre fonction:

/**
 * Represents a function that accepts three arguments and produces a result.
 * This is the three-arity specialization of {@link Function}.
 *
 * @param <T>
 *            the type of the first argument to the function
 * @param <U>
 *            the type of the second argument to the function
 * @param <V>
 *            the type of the third argument to the function
 * @param <R>
 *            the type of the result of the function
 *
 * @see Function
 */
@FunctionalInterface
public interface TriFunction<T, U, V, R> {

    R apply(T t, U u, V v);
}

Méthode acceptant de nombreux arguments; vous voulez en fournir certains:

private static boolean manyArgs(String str, int i, double d, float f) {
    return true;
}

Voici comment vous utilisez votre objet fonction personnalisé:

/*
* Pass one of the arguments. This creates a function accepting three
* arguments.
*/
TriFunction<Integer, Double, Float, Boolean> partiallyApplied = (i, d, f) -> 
                                                           manyArgs("", i, d, f);

/*
* Provide the rest of the arguments
*/
boolean res4 = partiallyApplied.apply(2, 3.0, 4.0F);
System.out.println("No time for ceremonies: " + res4);
 14
Author: Matthias Braun, 2014-07-11 14:33:26