vavr lambda函数库

vavr — 官方文档

1.2.1 副作用

在这里插入图片描述

关于副作用的进一步解释:

1
2
3
4
5
6
7
8
9
10
Side effect就是“副作用”(侧面影响),通常是对于一个函数而言的,说一个函数“有副作用”或者“没有副作用”。

如果一个函数修改了自己范围之外的资源,那就叫做有副作用,反之,就是没有副作用。

总结一下, 有副作用(side effects)的函数会做(不限于)这些事情:
+ 修改全局变量
+ 修改输入参数所引用的对象
+ 做输入输出操作
+ 调用其他有副作用的函数
+ 抛出异常

1.2.2 引用透明

在这里插入图片描述

1.2.3 价值观思考

在这里插入图片描述

1.3 数据结构概述

在这里插入图片描述

3.3 价值观

高效写
在这里插入图片描述

代码测试

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.taopanfeng.junit.vavr;

import io.vavr.collection.List;

/**
* 描述
*
* @author 陶攀峰
* @date 2022-07-06 19:19
*/
public class MyVavrTest {

// 2022-07-18 10:25 Lambda的方法引用类型问题测试
/*public static void main(String[] args) throws Exception {
Function1<Integer, Integer> f1 = Function1.of(i -> i + 1); // f1 = {MyVavrTest$lambda@665}
Function1<Integer, Integer> f2 = Function1.of(MyVavrTest::addOne); // f2 = {MyVavrTest$lambda@666}
Function1<Integer, Integer> f3 = Function1.of(f1); // f3 = {MyVavrTest$lambda@665}
Function1<Integer, Integer> f4 = Function1.of(f1::apply); // f4 = {MyVavrTest$lambda@667}

Class<? extends Function1> f1_class = f1.getClass();// f1_class = {Class@651} "class com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$1/1310540333"
Class<? extends Function1> f2_class = f2.getClass();// f2_class = {Class@657} "class com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$2/1340328248"
Class<? extends Function1> f3_class = f3.getClass();// f3_class = {Class@651} "class com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$1/1310540333"
Class<? extends Function1> f4_class = f4.getClass();// f4_class = {Class@661} "class com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$3/540642172"

String f1_typeName = f1.getClass().getTypeName();// f1_typeName = "com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$1/1310540333"
String f2_typeName = f2.getClass().getTypeName();// f2_typeName = "com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$2/1340328248"
String f3_typeName = f3.getClass().getTypeName();// f3_typeName = "com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$1/1310540333"
String f4_typeName = f4.getClass().getTypeName();// f4_typeName = "com.taopanfeng.junit.vavr.MyVavrTest$$Lambda$3/540642172"

Class<?> f1_componentType = f1.getClass().getComponentType();// null
Class<?> f2_componentType = f2.getClass().getComponentType();// null
Class<?> f3_componentType = f3.getClass().getComponentType();// null
Class<?> f4_componentType = f4.getClass().getComponentType();// null
}

public static Integer addOne(Integer i) {
return i + 1;
}*/

// 2022-07-16 19:29:39 currying Lambda函数的柯里化
/*public static void main(String[] args) throws Exception {
Function<Integer, Function<Integer, Function<Integer, Integer>>> currying0 = new Function<Integer, Function<Integer, Function<Integer, Integer>>>() {
@Override
public Function<Integer, Function<Integer, Integer>> apply(Integer x) {
return new Function<Integer, Function<Integer, Integer>>() {
@Override
public Function<Integer, Integer> apply(Integer y) {
return new Function<Integer, Integer>() {
@Override
public Integer apply(Integer z) {
return x + y - z;
}
};
}
};
}
};
Function<Integer, Function<Integer, Function<Integer, Integer>>> currying1 = new Function<Integer, Function<Integer, Function<Integer, Integer>>>() {
@Override
public Function<Integer, Function<Integer, Integer>> apply(Integer x) {
return new Function<Integer, Function<Integer, Integer>>() {
@Override
public Function<Integer, Integer> apply(Integer y) {
return z -> x + y - z;
}
};
}
};
Function<Integer, Function<Integer, Function<Integer, Integer>>> currying2 = new Function<Integer, Function<Integer, Function<Integer, Integer>>>() {
@Override
public Function<Integer, Function<Integer, Integer>> apply(Integer x) {
return y -> z -> x + y - z;
}
};
Function<Integer, Function<Integer, Function<Integer, Integer>>> currying3 = x -> y -> z -> x + y - z;
// 可参考 => Function2#curried() 柯里化版本

int x = 5;
int y = 4;
int z = 3;
// x + y - z
// 5 + 4 - 3 = 6
Integer apply0 = currying0.apply(x).apply(y).apply(z);// 6
Integer apply1 = currying1.apply(x).apply(y).apply(z);// 6
Integer apply2 = currying2.apply(x).apply(y).apply(z);// 6
Integer apply3 = currying3.apply(x).apply(y).apply(z);// 6

// Lambda函数的柯里化
System.out.println();
}*/

// 2022-07-16 17:51 vavr ======> 3.2 Function 函数
/*public static void main(String[] args) throws Exception {
// sum.apply(1, 2) = 3
Function2<Integer, Integer, Integer> function2 = (a, b) -> a + b;

// ↑↑↑ 效果同上 ↑↑↑
Function2<Integer, Integer, Integer> function2_1 = new Function2<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer a, Integer b) {
return a + b;
}
};

// 方法引用 => 接收三个参数的方法
Function3<String, String, String, String> function3 = Function3.of(MyVavrTest::methodWhichAccepts3Parameters);


// ### 3.2.1 Composition 组合【andThen、compose 步骤组合】
if (1 == 1) {
Function1<Integer, Integer> plusOne = a -> a + 1;
Function1<Integer, Integer> multiplyByTwo = a -> a * 2;

Function1<Integer, Integer> add1AndMultiplyBy2 = plusOne.andThen(multiplyByTwo);
Integer add1AndMultiplyBy2_apply = add1AndMultiplyBy2.apply(2);// 2 => 加1 => 乘2 ===> 6

// ↑↑↑ 效果同上 ↑↑↑ plusOne.andThen(multiplyByTwo) ===> 等同于 ===> multiplyByTwo.compose(plusOne)
Function1<Integer, Integer> add1AndMultiplyBy2_2 = multiplyByTwo.compose(plusOne);
Integer add1AndMultiplyBy2_2_apply = add1AndMultiplyBy2_2.apply(2);
System.out.println();
}

// ### 3.2.2 Lifting 起重【Option 异常封装】
if (1 == 1) {
// 测试可以除0
Function2<Integer, Integer, Integer> function2_divide = (a, b) -> a / b;
Function2<Integer, Integer, Option<Integer>> function2_lift = Function2.lift(function2_divide);
Option<Integer> function2_lift_1 = function2_lift.apply(1, 0);// = None
Option<Integer> function2_lift_2 = function2_lift.apply(4, 2);// = Some(2)

// 测试 sum 异常
Function2<Integer, Integer, Option<Integer>> function2_sum = Function2.lift(MyVavrTest::sum);
Option<Integer> function2_sum_op = function2_sum.apply(-1, 2);// = None
System.out.println();
}

// ### 3.2.3 Partial application 部分应用【多个 apply】
if (1 == 1) {
Function2<Integer, Integer, Integer> sum2 = (a, b) -> a + b;// 两数相加
Function1<Integer, Integer> add2 = sum2.apply(2);// 给定一个数 +2
Integer add2_apply = add2.apply(4);// 4+2 = 6

Function5<Integer, Integer, Integer, Integer, Integer, Integer> sum_5 = (a, b, c, d, e) -> a + b + c + d + e;// 六个数相加
Function2<Integer, Integer, Integer> add6 = sum_5.apply(2, 3, 1);// 给定一个数 +6
Integer add6_apply = add6.apply(4, 3);// 4+3 + 2+3+1 = 13
System.out.println();
}

// ### 3.2.4 Currying 咖喱【Lambda函数的柯里化 curried()】
if (1 == 1) {
Function2<Integer, Integer, Integer> sum = (a, b) -> a + b;
Function1<Integer, Integer> add2 = sum.curried().apply(2);
Integer apply = add2.apply(4);// 4+2 = 6

// 对上面的进一步解释 => 步骤拆解
Function1<Integer, Function1<Integer, Integer>> sum_curried = sum.curried();
Function1<Integer, Integer> sum_curried_2 = sum_curried.apply(2);
Integer sum_curried_2_4 = sum_curried_2.apply(4);// 6

System.out.println();
}

// ### 3.2.5 Memoization 记忆化
if (1 == 1) {
Function0<Double> hashCache = Function0.of(Math::random).memoized();

double randomValue1 = hashCache.apply();
double randomValue2 = hashCache.apply();
System.out.println();
}

}*/

static int sum(int first, int second) {
if (first < 0 || second < 0) {
throw new IllegalArgumentException("Only positive integers are allowed");
}
return first + second;
}

static String methodWhichAccepts3Parameters(String s1, String s2, String s3) {
return s1 + s2 + s3;
}

// 2022-07-16 17:34 vavr ======> 3.1 Tuple 元组
/*public static void main(String[] args) throws Exception {
// (Java, 8)
Tuple2<String, Integer> tuple2 = Tuple.of("Java", 8);
String java8_1 = tuple2._1;// "Java"
Integer java8_2 = tuple2._2;// 8

// (vavr, 1)
Tuple2<String, Integer> tuple2_map = tuple2.map(
_1 -> _1.substring(2) + "vr",
_2 -> _2 / 8
);

// (vavr, 1)
Tuple2<String, Integer> tuple2_map2 = tuple2.map(
(_1, _2) -> Tuple.of(_1.substring(2) + "vr", _2 / 8)
);

// "vavr 1"
String that = tuple2.apply(
(_1, _2) -> _1.substring(2) + "vr " + _2 / 8
);
}*/

// 2022-07-06 19:15 vavr ======> 一、简介
/*public static void main(String[] args) throws Exception {
Try<Integer> try1 = Try.of(() -> 1 / 0);
boolean failure = try1.isFailure();// true
Set<Integer> trySet = try1.toSet();// HashSet() size=0

List<Integer> list1 = List.of(1, 2, 3);// List(1, 2, 3)
List<Integer> list2 = list1.prepend(0);// List(0, 1, 2, 3)

Queue<Integer> queue = Queue.of(1, 2, 3).enqueue(4).enqueue(5);
String queueToString = queue.toString();// Queue(1, 2, 3, 4, 5)
Tuple2<Integer, Queue<Integer>> dequeued = queue.dequeue();// = (1, Queue(2, 3, 4, 5))

// = Some((1, Queue()))
Option<Tuple2<Integer, Queue<Integer>>> q_deq_op = Queue.of(1).dequeueOption();
Option<Integer> q_deq_op_map = q_deq_op.map(Tuple2::_1);
Integer q_deq_op_map_get = q_deq_op_map.get();

// = None
Option<Tuple2<Object, Queue<Object>>> q_deq_op1 = Queue.empty().dequeueOption();


// = TreeSet(1, 2, 3);
SortedSet<Integer> set = TreeSet.of(2, 3, 1, 2);

// = TreeSet(3, 2, 1);
Comparator<Integer> comparator = (a, b) -> b - a;
SortedSet<Integer> reversedSet = TreeSet.of(comparator, 2, 3, 1, 2);


// = ["1", "2", "3"] in Java 8
java.util.List<String> javaStreamStringList = Arrays.asList(1, 2, 3)
.stream()
.map(Object::toString)
.collect(Collectors.toList());

// = Stream("1", "2", "3") in Vavr
Stream<String> vavrStreamStringList = Stream.of(1, 2, 3).map(Object::toString);

for (String s : List.of("Java", "Advent")) {
// side effects and mutation【副作用和突变】
}
List.of("Java", "Advent").forEach(s -> {
// side effects and mutation【副作用和突变】
});

String join = join("a", "b", "c");
String join_vavr = join_vavr("a", "b", "c");
String join_vavr2 = join_vavr2("a", "b", "c");


// = (1, "A")
Tuple2<Integer, String> tuple2 = Tuple.of(1, "A");
Integer key = tuple2._1;
String value = tuple2._2;

// = HashMap((0, List(2, 4)), (1, List(1, 3)))
Map<Integer, List<Integer>> listGroupBy = List.of(1, 2, 3, 4).groupBy(i -> i % 2);
Option<List<Integer>> listGroupBy_0 = listGroupBy.get(0);
Option<List<Integer>> listGroupBy_1 = listGroupBy.get(1);

// = List((a, 0), (b, 1), (c, 2))
List<Tuple2<Character, Integer>> listZipWithIndex = List.of('a', 'b', 'c').zipWithIndex();
}*/

static String join(String... words) {
StringBuilder builder = new StringBuilder();
for (String s : words) {
if (builder.length() > 0) {
builder.append(", ");
}
builder.append(s);
}
return builder.toString();
}

static String join_vavr(String... words) {
return List.of(words)
.intersperse(", ")
.foldLeft(new StringBuilder(), StringBuilder::append)
.toString();
}

static String join_vavr2(String... words) {
return List.of(words).mkString(", ");
}


}