26
26
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Repeat .repeat ;
27
27
import static com .jnape .palatable .lambda .functions .builtin .fn2 .Take .take ;
28
28
import static com .jnape .palatable .lambda .functor .builtin .Lazy .lazy ;
29
+ import static com .jnape .palatable .lambdakoans .Koans .__ ;
29
30
import static java .util .Arrays .asList ;
30
31
import static org .hamcrest .CoreMatchers .equalTo ;
31
32
import static org .hamcrest .MatcherAssert .assertThat ;
32
33
import static testsupport .matchers .IterableMatcher .iterates ;
33
34
34
35
public class AboutApplicatives {
36
+
35
37
@ Test
36
38
public void applicativesZipEmbeddedValuesWithEmbeddedFunctions () {
37
39
Fn1 <Integer , Integer > inc = x -> x + 1 ;
38
40
39
41
assertThat (just (9 ).zip (just (inc )), equalTo (just (10 )));
40
- assertThat (just (15 ).zip (just (inc )), equalTo (just (16 )));
41
- // Explicit types on the nothing help the compiler
42
- assertThat (Maybe .<Integer >nothing ().zip (just (inc )), equalTo (nothing () ));
42
+ assertThat (just (15 ).zip (just (inc )), equalTo (just (__ )));
43
+ // Explicit types help the compiler
44
+ assertThat (Maybe .<Integer >nothing ().zip (just (inc )), equalTo (__ ));
43
45
44
46
Fn1 <? super String , ? extends Integer > getStringLength = String ::length ;
45
- assertThat (right ("Hello" ).zip (right (getStringLength )), equalTo (right (5 )));
46
- assertThat (right ("World!" ).zip (right (getStringLength )), equalTo (right (6 )));
47
- // Explicit types on the nothing help the compiler
48
- assertThat (Either .<String , String >left ("whoops" ).zip (right (getStringLength )), equalTo (left ("whoops" )));
47
+ assertThat (right ("Hello" ).zip (right (getStringLength )), equalTo (__ ));
48
+ assertThat (right ("World!" ).zip (right (getStringLength )), equalTo (__ ));
49
+ assertThat (Either .<String , String >left ("whoops" ).zip (right (getStringLength )), equalTo (left (__ )));
49
50
50
51
// Moving on to LambdaIterables, where "zipping" is more obvious
51
52
LambdaIterable <Integer > oneThroughThree = LambdaIterable .wrap (asList (1 , 2 , 3 ));
@@ -58,7 +59,7 @@ public void applicativesZipEmbeddedValuesWithEmbeddedFunctions() {
58
59
59
60
Fn1 <Integer , Integer > times3 = x -> x * 3 ;
60
61
LambdaIterable <Fn1 <? super Integer , ? extends Integer >> allFunctions = LambdaIterable .wrap (asList (inc , dec , times3 ));
61
- assertThat (oneThroughThree .zip (allFunctions ).unwrap (), iterates (2 , 0 , 3 , 3 , 1 , 6 , 4 , 2 , 9 ));
62
+ assertThat (oneThroughThree .zip (allFunctions ).unwrap (), iterates (__ () ));
62
63
}
63
64
64
65
@ Test
@@ -69,7 +70,7 @@ public void lazyApplicatives() {
69
70
70
71
LambdaIterable <Fn1 <? super Integer , ? extends Integer >> wrappedInc = LambdaIterable .wrap (asList (inc ));
71
72
LambdaIterable <Integer > zippedOnes = infiniteOnes .zip (wrappedInc );
72
- assertThat (take (3 , zippedOnes .unwrap ()), iterates (2 , 2 , 2 ));
73
+ assertThat (take (3 , zippedOnes .unwrap ()), iterates (__ () ));
73
74
74
75
// We might lazily get a mapping function...
75
76
AtomicInteger computed = new AtomicInteger (0 );
@@ -88,13 +89,13 @@ public void lazyApplicatives() {
88
89
Maybe <Integer > nothing = nothing ();
89
90
Lazy <Maybe <String >> lazyNothingToString = nothing .lazyZip (lazyGetToString );
90
91
91
- assertThat (lazyNothingToString .value (), equalTo (nothing () ));
92
- assertThat (computed .get (), equalTo (0 ));
92
+ assertThat (lazyNothingToString .value (), equalTo (__ ));
93
+ assertThat (computed .get (), equalTo (__ ));
93
94
94
95
// zip, however, we've eagerly generated a mapping function
95
96
Maybe <String > nothingToString = nothing .zip (expensiveWayToGetMaybeToString .apply (100_000 ));
96
- assertThat (nothingToString , equalTo (nothing () ));
97
- assertThat (computed .get (), equalTo (100_000 ));
97
+ assertThat (nothingToString , equalTo (__ ));
98
+ assertThat (computed .get (), equalTo (__ ));
98
99
}
99
100
100
101
@ Test
@@ -110,15 +111,15 @@ public void functionIsApplicative() {
110
111
Fn1 <Integer , Integer > div3 = i -> i / 3 ;
111
112
112
113
Fn1 <Integer , String > showDivision = LiftA2 .liftA2 ((divided , remainder ) -> String .format ("%d * 3 + %d" , divided , remainder ), div3 , mod3 );
113
- assertThat (showDivision .apply (10 ), equalTo ("3 * 3 + 1" ));
114
+ assertThat (showDivision .apply (10 ), equalTo (__ ));
114
115
115
116
116
117
Fn1 <String , Integer > findStart = s -> s .indexOf ('j' );
117
118
Fn1 <String , Integer > findEnd = s -> s .indexOf (' ' );
118
119
Fn3 <String , Integer , Integer , String > cutString = String ::substring ;
119
120
120
121
Fn1 <String , String > transformAndCut = LiftA3 .liftA3 (cutString , toUpper , findStart , findEnd );
121
- assertThat (transformAndCut .apply ("hellojava world" ), equalTo ("JAVA" ));
122
+ assertThat (transformAndCut .apply ("hellojava world" ), equalTo (__ ));
122
123
}
123
124
124
125
@ Test
@@ -138,7 +139,7 @@ public void applicativeRepresentsParallelism() throws ExecutionException, Interr
138
139
long singleThreadStart = System .currentTimeMillis ();
139
140
140
141
applicativeInIo
141
- .flatMap (result -> IO .io (() -> assertThat (result , equalTo (42 ))))
142
+ .flatMap (result -> IO .io (() -> assertThat (result , equalTo (__ ))))
142
143
.unsafePerformIO ();
143
144
144
145
System .out .printf ("Single thread execution took %d seconds%n" , (System .currentTimeMillis () - singleThreadStart ) / 1000 );
@@ -147,10 +148,10 @@ public void applicativeRepresentsParallelism() throws ExecutionException, Interr
147
148
long multipleThreadStart = System .currentTimeMillis ();
148
149
149
150
applicativeInIo
150
- .flatMap (result -> IO .io (() -> assertThat (result , equalTo (42 ))))
151
+ .flatMap (result -> IO .io (() -> assertThat (result , equalTo (__ ))))
151
152
.unsafePerformAsyncIO (Executors .newFixedThreadPool (2 ))
152
153
.get ();
153
154
154
- System .out .printf ("Single thread execution took %d seconds%n" , (System .currentTimeMillis () - multipleThreadStart ) / 1000 );
155
+ System .out .printf ("Multiple thread execution took %d seconds%n" , (System .currentTimeMillis () - multipleThreadStart ) / 1000 );
155
156
}
156
157
}
0 commit comments