@@ -75,22 +75,22 @@ compile group: 'com.jnape.palatable', name: 'lambda', version: '4.0.0'
75
75
76
76
First, the obligatory ` map ` /` filter ` /` reduce ` example:
77
77
``` Java
78
- Integer sumOfEvenIncrements =
78
+ Maybe< Integer > sumOfEvenIncrements =
79
79
reduceLeft((x, y) - > x + y,
80
80
filter(x - > x % 2 == 0 ,
81
81
map(x - > x + 1 , asList(1 , 2 , 3 , 4 , 5 ))));
82
- // -> 12
82
+ // -> Just 12
83
83
```
84
84
85
85
Every function in lambda is [ curried] ( https://www.wikiwand.com/en/Currying ) , so we could have also done this:
86
86
``` Java
87
- Fn1<Iterable<Integer > , Integer > sumOfEvenIncrementsFn =
87
+ Fn1<Iterable<Integer > , Maybe< Integer > > sumOfEvenIncrementsFn =
88
88
map((Integer x) - > x + 1 )
89
- .andThen (filter(x - > x % 2 == 0 ))
90
- .andThen (reduceLeft((x, y) - > x + y));
89
+ .fmap (filter(x - > x % 2 == 0 ))
90
+ .fmap (reduceLeft((x, y) - > x + y));
91
91
92
- Integer sumOfEvenIncrements = sumOfEvenIncrementsFn. apply(asList(1 , 2 , 3 , 4 , 5 ));
93
- // -> 12
92
+ Maybe< Integer > sumOfEvenIncrements = sumOfEvenIncrementsFn. apply(asList(1 , 2 , 3 , 4 , 5 ));
93
+ // -> Just 12
94
94
```
95
95
96
96
How about the positive squares below 100:
@@ -106,7 +106,7 @@ We could have also used `unfoldr`:
106
106
``` Java
107
107
Iterable<Integer > positiveSquaresBelow100 = unfoldr(x - > {
108
108
int square = x * x;
109
- return square < 100 ? Optional . of (tuple(square, x + 1 )) : Optional . empty ();
109
+ return square < 100 ? Maybe . just (tuple(square, x + 1 )) : Maybe . nothing ();
110
110
}, 1 );
111
111
// -> [1, 4, 9, 16, 25, 36, 49, 64, 81]
112
112
```
@@ -116,7 +116,7 @@ What if we want the cross product of a domain and codomain:
116
116
``` Java
117
117
Iterable<Tuple2<Integer , String > > crossProduct =
118
118
take(10 , cartesianProduct(asList(1 , 2 , 3 ), asList(" a" , " b" , " c" )));
119
- // -> (1,"a"), (1,"b"), (1,"c"), (2,"a"), (2,"b"), (2,"c"), (3,"a"), (3,"b"), (3,"c")
119
+ // -> [ (1,"a"), (1,"b"), (1,"c"), (2,"a"), (2,"b"), (2,"c"), (3,"a"), (3,"b"), (3,"c")]
120
120
```
121
121
122
122
Let's compose two functions:
@@ -125,9 +125,9 @@ Let's compose two functions:
125
125
Fn1<Integer , Integer > add = x - > x + 1 ;
126
126
Fn1<Integer , Integer > subtract = x - > x - 1 ;
127
127
128
- Fn1<Integer , Integer > noOp = add. andThen (subtract);
128
+ Fn1<Integer , Integer > noOp = add. fmap (subtract);
129
129
// same as
130
- Fn1<Integer , Integer > alsoNoOp = subtract. compose (add);
130
+ Fn1<Integer , Integer > alsoNoOp = subtract. contraMap (add);
131
131
```
132
132
133
133
And partially apply some:
@@ -144,7 +144,7 @@ And have fun with 3s:
144
144
145
145
``` Java
146
146
Iterable<Iterable<Integer > > multiplesOf3InGroupsOf3 =
147
- take(3 , inGroupsOf(3 , unfoldr(x - > Optional . of (tuple(x * 3 , x + 1 )), 1 )));
147
+ take(3 , inGroupsOf(3 , unfoldr(x - > Maybe . just (tuple(x * 3 , x + 1 )), 1 )));
148
148
// -> [[3, 6, 9], [12, 15, 18], [21, 24, 27]]
149
149
```
150
150
@@ -176,7 +176,7 @@ Check out the [semigroup](https://palatable.github.io/lambda/javadoc/com/jnape/p
176
176
177
177
``` Java
178
178
Monoid<Integer > multiply = monoid((x, y) - > x * y, 1 );
179
- multiple . reduceLeft(emptyList()); // -> 1
179
+ multiply . reduceLeft(emptyList()); // -> 1
180
180
multiply. reduceLeft(asList(1 , 2 , 3 )); // -> 6
181
181
multiply. foldMap(Integer :: parseInt, asList(" 1" , " 2" , " 3" )); // -> also 6
182
182
```
0 commit comments