Java 8中的部分函数应用程序 [英] Partial function application in Java 8

查看:94
本文介绍了Java 8中的部分函数应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是有问题的方法:

  / ** 
*将一段{@code字符}附加到一个{@code字符串}。
*
* @将附加字符的字符串作为StringBuilder返回
* /
private static StringBuilder appendChar(char,int times,String string){
StringBuilder strBuilder = new StringBuilder(string);
for(int i = 0; i< times; i ++){
strBuilder.append(character);
}
返回strBuilder;
}


解决方案

这实现了我想要的做:

  / * 
*应用两个参数。创建的函数接受一个字符串,
*返回一个StringBuilder
* /
函数< String,StringBuilder> addEllipsis = appendToMe - > appendChar(
'。',3,appendToMe);
/ *
*应用一个参数。这将创建一个函数,它接受另外两个
*参数并返回一个StringBuilder
* /
BiFunction< String,Integer,StringBuilder> addBangs =(appendToMe,
次) - > appendChar('!',times,appendToMe);

//通过向另一个函数应用一个参数来创建一个函数
函数< 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(待续);

有关函数对象的所有Java预定义变体的列表,请看这里



<编辑:



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

  / ** 
*表示接受三个参数并产生结果的函数。
*这是{@link Function}的三元专业化。
*
* @param< T>
*函数第一个参数的类型
* @param< U>
*函数的第二个参数的类型
* @param< V>
*函数第三个参数的类型
* @param< R>
*函数结果的类型
*
* @see函数
* /
@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){ 
返回true;

$ / code>

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

  / * 
*传递一个参数。这会创建一个接受三个
*参数的函数。
* /
TriFunction< Integer,Double,Float,Boolean> partialApplied =(i,d,f) - >
manyArgs(,i,d,f);
$ b $ * b $ b *提供其余的参数
* /
布尔值res4 = partiallyApplied.apply(2,3.0,4.0F);
System.out.println(没有仪式的时间:+ res4);


I want to partially apply some arguments to a legacy method using Java 8's newly introduced function objects.

Here is the method in 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;
}

解决方案

This achieves what I wanted to do:

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

For a list of all of Java's predefined varieties of the function object have a look here.

Edit:

If you have a method with a lot of arguments, you can write your own kind of function:

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

Method accepting many arguments; you want to provide some of them:

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

Here is how you use your custom function object:

/*
* 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 8中的部分函数应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆