2
2
3
3
import com .jnape .palatable .lambda .adt .Either ;
4
4
import com .jnape .palatable .lambda .adt .Maybe ;
5
+ import com .jnape .palatable .lambda .adt .hlist .Tuple2 ;
5
6
import com .jnape .palatable .lambda .functions .Fn1 ;
7
+ import com .jnape .palatable .lambda .functions .Fn3 ;
8
+ import com .jnape .palatable .lambda .functions .builtin .fn3 .LiftA2 ;
9
+ import com .jnape .palatable .lambda .functions .builtin .fn4 .LiftA3 ;
6
10
import com .jnape .palatable .lambda .functor .Applicative ;
7
11
import com .jnape .palatable .lambda .functor .builtin .Lazy ;
8
12
import com .jnape .palatable .lambda .traversable .LambdaIterable ;
14
18
import static com .jnape .palatable .lambda .adt .Either .right ;
15
19
import static com .jnape .palatable .lambda .adt .Maybe .just ;
16
20
import static com .jnape .palatable .lambda .adt .Maybe .nothing ;
21
+ import static com .jnape .palatable .lambda .adt .hlist .HList .tuple ;
17
22
import static com .jnape .palatable .lambda .functions .Fn1 .fn1 ;
18
23
import static com .jnape .palatable .lambda .functions .builtin .fn1 .Repeat .repeat ;
19
24
import static com .jnape .palatable .lambda .functions .builtin .fn2 .Take .take ;
@@ -88,4 +93,28 @@ public void lazyApplicatives() {
88
93
assertThat (nothingToString , equalTo (nothing ()));
89
94
assertThat (computed .get (), equalTo (100_000 ));
90
95
}
96
+
97
+ @ Test
98
+ public void functionIsApplicative () {
99
+ Fn1 <String , Integer > strLen = String ::length ;
100
+ Fn1 <String , String > toUpper = String ::toUpperCase ;
101
+
102
+ // Result of unary function calls are passed to mapping function as arguments
103
+ Fn1 <String , Tuple2 <Integer , String >> lengthAndUppercase = LiftA2 .liftA2 (Tuple2 ::tuple , strLen , toUpper );
104
+ assertThat (lengthAndUppercase .apply ("hello world" ), equalTo (tuple (11 , "HELLO WORLD" )));
105
+
106
+ Fn1 <Integer , Integer > mod3 = i -> i % 3 ;
107
+ Fn1 <Integer , Integer > div3 = i -> i / 3 ;
108
+
109
+ Fn1 <Integer , String > showDivision = LiftA2 .liftA2 ((divided , remainder ) -> String .format ("%d * 3 + %d" , divided , remainder ), div3 , mod3 );
110
+ assertThat (showDivision .apply (10 ), equalTo ("3 * 3 + 1" ));
111
+
112
+
113
+ Fn1 <String , Integer > findStart = s -> s .indexOf ('j' );
114
+ Fn1 <String , Integer > findEnd = s -> s .indexOf (' ' );
115
+ Fn3 <String , Integer , Integer , String > cutString = String ::substring ;
116
+
117
+ Fn1 <String , String > transformAndCut = LiftA3 .liftA3 (cutString , toUpper , findStart , findEnd );
118
+ assertThat (transformAndCut .apply ("hellojava world" ), equalTo ("JAVA" ));
119
+ }
91
120
}
0 commit comments