Functional Programming With Java 简明教程
Functional Programming with Java - Functions
函数是一段执行特定任务的语句块。函数接受数据,处理数据及返回结果。函数主要编写为支持可重用性概念。一个函数一旦编写完毕,可以轻松调用,而无需一遍又一遍地编写相同的代码。
A function is a block of statements that performs a specific task. Functions accept data, process it, and return a result. Functions are written primarily to support the concept of re usability. Once a function is written, it can be called easily, without having to write the same code again and again.
函数式编程围绕头等函数、纯函数和高阶函数展开。
Functional Programming revolves around first class functions, pure functions and high order functions.
-
A First Class Function is the one that uses first class entities like String, numbers which can be passed as arguments, can be returned or assigned to a variable.
-
A High Order Function is the one which can take a function as an argument and/or can return a function.
-
A Pure Function is the one which has no side effect while its execution.
First Class Function
头等函数可以视为变量。这意味着它可以作为参数传递给函数,可以由函数返回,或者也可以分配给变量。Java 使用 lambda 表达式来支持头等函数。lambda 表达式类似于匿名函数。请看下面的示例 −
A first class function can be treated as a variable. That means it can be passed as a parameter to a function, it can be returned by a function or can be assigned to a variable as well. Java supports first class function using lambda expression. A lambda expression is analogous to an anonymous function. See the example below −
public class FunctionTester {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
SquareMaker squareMaker = item -> item * item;
for(int i = 0; i < array.length; i++){
System.out.println(squareMaker.square(array[i]));
}
}
}
interface SquareMaker {
int square(int item);
}
High Order Function
高阶函数要么接受一个函数作为参数,要么返回一个函数。在 Java 中,我们可以传递或返回 lambda 表达式来实现此类功能。
A high order function either takes a function as a parameter or returns a function. In Java, we can pass or return a lambda expression to achieve such functionality.
import java.util.function.Function;
public class FunctionTester {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5};
Function<Integer, Integer> square = t -> t * t;
Function<Integer, Integer> cube = t -> t * t * t;
for(int i = 0; i < array.length; i++){
print(square, array[i]);
}
for(int i = 0; i < array.length; i++){
print(cube, array[i]);
}
}
private static <T, R> void print(Function<T, R> function, T t ) {
System.out.println(function.apply(t));
}
}
Pure Function
纯函数不会修改任何全局变量或修改任何作为参数传递给它的引用。因此,它没有副作用。无论使用相同的参数调用多少次,它总能返回相同的值。此类函数非常有用,并且线程安全。在下面的示例中,sum 是一个纯函数。
A pure function does not modify any global variable or modify any reference passed as a parameter to it. So it has no side-effect. It always returns the same value when invoked with same parameters. Such functions are very useful and are thread safe. In example below, sum is a pure function.
public class FunctionTester {
public static void main(String[] args) {
int a, b;
a = 1;
b = 2;
System.out.println(sum(a, b));
}
private static int sum(int a, int b){
return a + b;
}
}