forked from miciek/grokkingfp-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch02_PureFunctions.java
57 lines (44 loc) · 1.18 KB
/
ch02_PureFunctions.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
import java.util.ArrayList;
import java.util.List;
public class ch02_PureFunctions {
static double f(double x) {
return x * 95.0 / 100.0;
}
static int f1(String s) {
return s.length() * 3;
}
static double f2(double x) {
return x * Math.random();
}
static int increment(int x) {
return x + 1;
}
static double randomPart(double x) {
return x * Math.random();
}
static int add(int a, int b) {
return a + b;
}
static class ShoppingCart {
private List<String> items = new ArrayList<>();
public int addItem(String item) {
items.add(item);
return items.size() + 5;
}
}
static char getFirstCharacter(String s) {
return s.charAt(0);
}
public static void main(String[] args) {
assert(f(20) == 19);
assert(f(100) == 95);
assert(f(10) == 9.5);
assert(f1("Scala") == 15);
assert(f2(20) <= 20);
assert(increment(2) == 3);
assert(randomPart(10) >= 0);
assert(add(2, 3) == 5);
ShoppingCart cart = new ShoppingCart();
assert(cart.addItem("item") == 6);
}
}