当前位置: 首页 > 面试题库 >

Java 8中的部分函数应用程序

濮彬
2023-03-14
问题内容

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

问题答案:

这实现了我想做的事情:

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

有关Java函数对象的所有预定义变体的列表,请在此处查看。

编辑:

如果您的方法带有很多参数,则可以编写自己的函数:

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


 类似资料:
  • 我想封装我的,因此它被称为:。但是,我似乎无法正确获取类型/语法。我如何重新编写函数定义以实现这一目标?

  • 本文向大家介绍C#函数式编程中的部分应用详解,包括了C#函数式编程中的部分应用详解的使用技巧和注意事项,需要的朋友参考一下 何谓函数式编程 相信大家在实际的开发中,很多情况下完成一个功能都需要借助多个类,那么我们这里的基本单元就是类。而函数式编程则更加细化,致使我们解决一个功能的基本单元是函数,而不是类,每个功能都是由多个函数构成,并且函数之间没有直接的关系。如果简单的文字描述还不足以让你理解,下

  • 问题内容: 给定以下变量 我想使用以下代码将占位符$ {name}替换为值“ Joe”(不起作用) 但是,如果我采用“旧式”方式,则一切都将正常运行: 我肯定在这里想念的东西:) 问题答案: 您还可以使用Stream.reduce(identity,accumulator,combiner)。 身份 是减少函数的初始值。 累加器 减少到,如果流是 顺序的 ,这是下一个减少的条件。 合路器 永远不要

  • 上一章重点在于代码重复:提升现有的函数功能、或者将函数进行组合。 这一章,我们来看看另外两种函数重用的机制:函数的部分应用(Partial Application of Functions) 、 柯里化(Currying) 。 部分应用的函数 和其他遵循函数式编程范式的语言一样,Scala 允许部分应用一个函数。 调用一个函数时,不是把函数需要的所有参数都传递给它,而是仅仅传递一部分,其他参数留空

  • 我想出了这两个: 我的例子正确地说明了这个练习吗? 给定两个参数: null null 我在SO上看到了一些类似的问题(比如,这个问题),这个问题几乎是我要找的,但不完全是(我只是在找函数的例子,没有别的--没有应用性,没有单子)。

  • 为什么供应商只支持无参数构造函数? 如果默认构造函数存在,我可以这样做: 但如果唯一的构造函数采用字符串,我必须这样做: