当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Currying Functions用法及代码示例


函数柯里化是一个概念将具有多个参数的函数分解为多个具有单个参数的函数,以使输出相同。换句话说,它是一种将multi-valued参数函数简化为single-valued参数multi-functions的技术。 考虑这个例子来明确概念:柯里化将高阶函数分解为一系列较小的级联函数,这些函数接受一个参数并返回一个函数,除了最后一个级联函数返回所需值之外。例如:设一个函数映射为f:(u, v) -> w柯里化上面的函数会产生g:(u->(v->w))因此gMap来自u到一个函数,该函数又映射自vw上述数学表达式也可以表示为:{g(u)}(v)=f(u, v)因此,curry(f) = g 下面是 Java 中的一些示例来演示函数柯里化: 示例 1:使用函数柯里化将 2 个数字相加

// Java Program to demonstrate Function Currying 
  
import java.util.function.Function; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
  
        // Using Java 8 Functions 
        // to create lambda expressions for functions 
        // and with this, applying Function Currying 
  
        // Curried Function for Adding u & v 
        Function<Integer, 
                 Function<Integer, Integer> > 
            curryAdder = u -> v -> u + v; 
  
        // Calling the curried functions 
  
        // Calling Curried Function for Adding u & v 
        System.out.println("Add 2, 3 :"
                           + curryAdder 
                                 .apply(2) 
                                 .apply(3)); 
  
        } 
} 
输出:
Add 2, 3 :5
示例 2:使用函数柯里化将 2 个数字相乘

// Java Program to demonstrate Function Currying 
  
import java.util.function.Function; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
  
        // Using Java 8 Functions 
        // to create lambda expressions for functions 
        // and with this, applying Function Currying 
  
        // Curried Function for Multiplying u & v 
        Function<Integer, 
                 Function<Integer, Integer> > 
            curryMulti = u -> v -> u * v; 
  
        // Calling the curried functions 
         
        // Calling Curried Function for Multiplying u & v 
        System.out.println("Multiply 2, 3 :"
                           + curryMulti 
                                 .apply(2) 
                                 .apply(3)); 
    } 
} 
输出:
Multiply 2, 3 :6
示例 3:使用函数柯里化将 3 个数字相加

// Java Program to demonstrate Function Currying 
  
import java.util.function.Function; 
  
public class GFG { 
    public static void main(String args[]) 
    { 
  
        // Using Java 8 Functions 
        // to create lambda expressions for functions 
        // and with this, applying Function Currying 
  
        // Curried Function for Adding u, v & w 
        Function<Integer, 
                 Function<Integer, 
                          Function<Integer, Integer> > > 
            triadder = u -> w -> v -> u + w + v; 
  
        // Calling the curried functions 
  
        // Calling Curried Function for Adding u, v & w 
        System.out.println("Add 2, 3, 4 :"
                           + triadder 
                                 .apply(2) 
                                 .apply(3) 
                                 .apply(4)); 
    } 
} 
输出:
Add 2, 3, 4 :9


相关用法


注:本文由纯净天空筛选整理自psil123大神的英文原创作品 Currying Functions in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。