java - Java 8 中的偏函数应用

标签 java functional-programming java-8

我想使用 Java 8 新引入的函数对象将遗留方法部分应用于某些参数。

这里是有问题的方法:

/**
 * 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;
}

最佳答案

这实现了我想做的事情:

/*
 * Pass two arguments. The created function accepts a String and
 * returns a StringBuilder
 */
Function<String, StringBuilder> addEllipsis = appendToMe -> appendChar(
        '.', 3, appendToMe);
/*
 * Pass 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 passing 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");

有关 Java 的所有预定义函数对象变体的列表,请查看 here .

编辑:

如果你有一个有很多参数的方法,你可以写你自己的函数:

/**
 * 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);
}

接受多个参数的方法;你想提供其中的一些:

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

以下是您如何使用自定义函数对象:

/*
* 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);

关于java - Java 8 中的偏函数应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22577261/

相关文章:

java - 多线程模式下Log4j2自定义ContextDataInjector

java-8 - 如何将未捕获的异常处理程序/完成附加到 CompletableFuture 链

javascript - 是否可以将 Scratch 项目转换为 Android 应用程序?

java - 模糊的边缘不断恢复 100% 的不透明度(处理中)

Java 8 功能 : How to compute a list of dependent evolutions of an object?

c - C 函数式编程有哪些工具?

javascript - 如何让这个功能更可重用/更具体/更好的设计?

java - Java 8 Lambda 表达式编译成什么?

java - 使用 Java 6/7 编译的注释可以用于 Java 8 中的类型参数吗?

java - JAVA 和 EMAIL 中的格式化字符串