函数式编程

有哪些编程范式

  • 命令式编程:告诉计算机要做什么。代表语言有:C, C++, Java, Javascript, BASIC,Ruby
  • 声明式编程:以数据结构的形式来表达程序执行的逻辑。代表语言有:SQL,HTML,CSS
  • 函数式编程:将函数作为编程中的“一等公民”,关注于流程而非具体实现。代表语言有:JAVA(8以上),js(ES6),C#,Scala,python等

什么是函数式编程

编程是以函数作为单元来处理各个业务逻辑,函数既可以当做参数传递也可以作为返回值,可以把函数理解一个值到另一个值得映射关系,即数学领域的函数概念。


优缺点

优点:

  • 代码简洁,开发速度快
  • 适合数据运算、大数据处理等
  • 易于理解,抽象度高

缺点:

  • 调试上相对命令式要困难
  • 由于函数内数据不变原则,导致的资源占用

java中的函数式编程

可以简单概括为:lambda + 方法引用 + stream API = java函数式编程


基本函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class FunctionDemo {

public static void main(String[] args) {
//断言型
// predicate();
//消费型
// consumer();
//一元函数 输入输出不同
// function();
//提供型
// supplier();
//一元函数 输入输出类型相同
// unaryOperator();
//二元函数 输入输出不同
// biFunction();
//二元函数 输入输出相同
binaryOperator();
}

/**
*
*/
public static void predicate(){
Predicate<Integer> predicate = i -> i > 0;
IntPredicate intPredicate = i -> i > 0;
System.out.print(predicate.test(6));
System.out.print(intPredicate.test(-1));
}

public static void consumer(){
Consumer<String> consumer = s -> System.out.println(s);
consumer.accept("我是一个消费者");
}

public static void function(){
Function<Integer,String> function = x -> "数字是:"+ x;
System.out.println(function.apply(88));
}

public static void supplier(){
Supplier<String> supplier = () -> "我是一个提供者";
System.out.println(supplier.get());
}

public static void unaryOperator(){
UnaryOperator<Integer> unaryOperator = x -> ++x;
System.out.println(unaryOperator.apply(1));
}

public static void biFunction(){
BiFunction<Integer,Double,Double> biFunction = (x,y) -> {
++x;
++y;
return x+y;
};
System.out.println(biFunction.apply(1,2.3));
}

public static void binaryOperator(){
IntBinaryOperator intBinaryOperator = (x,y) -> x + y;
System.out.println(intBinaryOperator.applyAsInt(2,3));
}
}

lambda表达式

可以搜索参考“java8新特性-lambda表达式”


方法引用

直接使用两个冒号::来调用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
public class MethodReferenceDemo {
public static void main(String[] args) {
//消费者 方法引用模式
// consumer();
//静态方法引用
// callStaticMethod();
//非静态 实例方法引用
// callMethod();
//非静态 类方法引用
// callMethodByClass();
//构造函数方法引用
// callConstructorMethod();
//数据不变模式
callMethod2();
}

public static void consumer(){
Consumer<String> consumer = System.out::println;
consumer.accept("我是一个消费者");
}

private static void callStaticMethod() {
Consumer<Dog> consumer = Dog::bark;
consumer.accept(new Dog());
}

private static void callMethod() {
Dog dog = new Dog();
Function<Integer,Integer> function = dog::eat;
System.out.println("还剩[" + function.apply(3) + "]斤狗粮");
}

private static void callMethodByClass() {
BiFunction<Dog,Integer,Integer> biFunction = Dog::eat;
System.out.println("还剩[" + biFunction.apply(new Dog(),4) + "]斤狗粮");
}

private static void callConstructorMethod() {
Supplier<Dog> supplier = Dog::new;
System.out.println("new 了一个对象" + supplier.get());
}

private static void callMethod2() {
Dog dog = new Dog();
Function<Integer,Integer> function = dog::eat; //函数声明
dog = null;
System.out.println("还剩[" + function.apply(3) + "]斤狗粮");
}
}

Stream流API

可以搜索参考“Java8中的Stream详解”



版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接,感谢各位看官!!!

本文出自:monkeyGeek

座右铭:生于忧患,死于安乐

欢迎志同道合的朋友一起交流、探讨!

monkeyGeek
#

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×