-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
VavrFunctions.java
57 lines (46 loc) · 1.95 KB
/
VavrFunctions.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
package vavr;
import io.vavr.Function0;
import io.vavr.Function1;
import io.vavr.Function2;
import io.vavr.collection.List;
import org.junit.Test;
import java.util.ArrayList;
public class VavrFunctions {
@Test
public void funcFeatures() {
Function2<String, Integer, Boolean> func1 = (str, intValue) -> str.equals("hello") && intValue == 1981;
System.out.println(func1);//Function
Function1<Integer, Boolean> partialFunc = func1.apply("hello");
System.out.println(partialFunc); //Partial application
Function1<Integer, Boolean> funcCurried = func1.curried().apply("hello");
System.out.println(funcCurried); //Curried
System.out.println(func1.apply("hello", 1981));//Full
}
@Test
public void funcCompositionFeatures() {
Function2<String, Integer, Boolean> func1 = (str, intValue) -> str.equals("hello") && intValue == 1981;
Function1<Boolean, String> func2 = (bool) -> bool ? "True" : "False";
Function2<String, Integer, String> stringIntegerStringFunction2 = func1.andThen(func2);
String responseTrue = stringIntegerStringFunction2.apply("hello", 1981);
System.out.println(responseTrue);
String responseFalse = stringIntegerStringFunction2.apply("hello", 1111);
System.out.println(responseFalse);//Function
}
@Test
public void funcMemorization() {
Function0<Long> funMemorized = System::nanoTime;
Function0<Long> memoized = funMemorized.memoized();
System.out.println(memoized.apply());
System.out.println(memoized.apply());
System.out.println(memoized.apply());
}
@Test
public void foldFunc() {
var numbers = List.of(1, 2, 30, 3, 4, 5,10, 8);
String strNumbers = numbers
.filter(number -> number <= 5)
.foldRight("", (number, output) ->
output.concat("-").concat(String.valueOf(number)));
System.out.println(strNumbers);
}
}