【问题标题】:Partial function application in Java 8Java 8 中的偏函数应用
【发布时间】:2014-04-29 21:53:59
【问题描述】:

我想使用 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;
}

【问题讨论】:

    标签: java functional-programming java-8


    【解决方案1】:

    这实现了我想做的:

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

    【讨论】:

    • 不可避免地使用apply是一个不幸的提醒,函数在Java中不是一流的......
    • @ach 物有所值,它们是对象,而对象在 Java 中是一流的。
    猜你喜欢
    • 2018-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2017-11-15
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多