Skip to content

Commit 5b3121f

Browse files
committed
Adding Fn5-8; adding #into on Tuple5-8
1 parent 1a09821 commit 5b3121f

22 files changed

Lines changed: 1466 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
55

66
## [Unreleased]
77
### Added
8-
- `Tuple6` through `Tuple8`
98
- `Fn3#fn3` and `Fn4#fn4` static factory methods
9+
- `Fn5` through `Fn8`
10+
- `Tuple5#into`
11+
- `Tuple6` through `Tuple8`
1012

1113
## [2.0.0] - 2017-11-13
1214
### Changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ In addition to implementing `fmap` from `Functor`, implementing an applicative f
338338

339339
### <a name="monads">Monads</a>
340340

341-
Monads are applicative functors that additionally support a chaining operation, `flatMap :: (a -> f b) -> f a -> f b`: a function from the functor's parameter to a new instance of the same functor over a potentially different parameter. Because the function passed to `flatMap` can return a different instance of the same functor, functors can take advantage of multiple constructions that yield different results functorial operations, like short-circuiting, as in the following example using `Either`:
341+
Monads are applicative functors that additionally support a chaining operation, `flatMap :: (a -> f b) -> f a -> f b`: a function from the functor's parameter to a new instance of the same functor over a potentially different parameter. Because the function passed to `flatMap` can return a different instance of the same functor, functors can take advantage of multiple constructions that yield different functorial operations, like short-circuiting, as in the following example using `Either`:
342342

343343
```Java
344344
class Person {

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple5.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn5;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.functor.Bifunctor;
67
import com.jnape.palatable.lambda.monad.Monad;
@@ -91,6 +92,19 @@ public _5 _5() {
9192
return _5;
9293
}
9394

95+
/**
96+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
97+
* slots.
98+
*
99+
* @param fn the function to apply
100+
* @param <R> the return type of the function
101+
* @return the result of applying the destructured tuple to the function
102+
* @see Tuple2#into
103+
*/
104+
public <R> R into(Fn5<? super _1, ? super _2, ? super _3, ? super _4, ? super _5, ? extends R> fn) {
105+
return fn.apply(_1, _2, _3, _4, _5);
106+
}
107+
94108
@Override
95109
public <_5Prime> Tuple5<_1, _2, _3, _4, _5Prime> fmap(Function<? super _5, ? extends _5Prime> fn) {
96110
return Monad.super.<_5Prime>fmap(fn).coerce();

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple6.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn6;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.functor.Bifunctor;
67
import com.jnape.palatable.lambda.monad.Monad;
@@ -104,6 +105,18 @@ public _6 _6() {
104105
return _6;
105106
}
106107

108+
/**
109+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
110+
* slots.
111+
*
112+
* @param fn the function to apply
113+
* @param <R> the return type of the function
114+
* @return the result of applying the destructured tuple to the function
115+
* @see Tuple2#into
116+
*/
117+
public <R> R into(Fn6<? super _1, ? super _2, ? super _3, ? super _4, ? super _5, ? super _6, ? extends R> fn) {
118+
return fn.apply(_1, _2, _3, _4, _5, _6);
119+
}
107120

108121
@Override
109122
public <_6Prime> Tuple6<_1, _2, _3, _4, _5, _6Prime> fmap(Function<? super _6, ? extends _6Prime> fn) {

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple7.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn7;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.functor.Bifunctor;
67
import com.jnape.palatable.lambda.monad.Monad;
@@ -117,6 +118,20 @@ public _7 _7() {
117118
return _7;
118119
}
119120

121+
/**
122+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
123+
* slots.
124+
*
125+
* @param fn the function to apply
126+
* @param <R> the return type of the function
127+
* @return the result of applying the destructured tuple to the function
128+
* @see Tuple2#into
129+
*/
130+
public <R> R into(
131+
Fn7<? super _1, ? super _2, ? super _3, ? super _4, ? super _5, ? super _6, ? super _7, ? extends R> fn) {
132+
return fn.apply(_1, _2, _3, _4, _5, _6, _7);
133+
}
134+
120135
@Override
121136
public <_7Prime> Tuple7<_1, _2, _3, _4, _5, _6, _7Prime> fmap(Function<? super _7, ? extends _7Prime> fn) {
122137
return Monad.super.<_7Prime>fmap(fn).coerce();

src/main/java/com/jnape/palatable/lambda/adt/hlist/Tuple8.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.jnape.palatable.lambda.adt.hlist;
22

33
import com.jnape.palatable.lambda.adt.hlist.HList.HCons;
4+
import com.jnape.palatable.lambda.functions.Fn8;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.functor.Bifunctor;
67
import com.jnape.palatable.lambda.monad.Monad;
@@ -130,6 +131,20 @@ public _8 _8() {
130131
return _8;
131132
}
132133

134+
/**
135+
* Destructure and apply this tuple to a function accepting the same number of arguments as this tuple's
136+
* slots.
137+
*
138+
* @param fn the function to apply
139+
* @param <R> the return type of the function
140+
* @return the result of applying the destructured tuple to the function
141+
* @see Tuple2#into
142+
*/
143+
public <R> R into(
144+
Fn8<? super _1, ? super _2, ? super _3, ? super _4, ? super _5, ? super _6, ? super _7, ? super _8, ? extends R> fn) {
145+
return fn.apply(_1, _2, _3, _4, _5, _6, _7, _8);
146+
}
147+
133148
@Override
134149
public <_8Prime> Tuple8<_1, _2, _3, _4, _5, _6, _7, _8Prime> fmap(Function<? super _8, ? extends _8Prime> fn) {
135150
return Monad.super.<_8Prime>fmap(fn).coerce();

src/main/java/com/jnape/palatable/lambda/functions/Fn1.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ default <C> Fn1<A, B> discardR(Applicative<C, Fn1<A, ?>> appB) {
9090
*
9191
* @param <Z> the new argument type
9292
* @param fn the contravariant argument mapping function
93-
* @return a new function from Z (the new argument type) to B (the same result)
93+
* @return an {@link Fn1}&lt;Z, B&gt;
9494
*/
9595
@Override
9696
default <Z> Fn1<Z, B> diMapL(Function<? super Z, ? extends A> fn) {
@@ -103,7 +103,7 @@ default <Z> Fn1<Z, B> diMapL(Function<? super Z, ? extends A> fn) {
103103
*
104104
* @param <C> the new result type
105105
* @param fn the covariant result mapping function
106-
* @return a new function from A (the same argument type) to C (the new result type)
106+
* @return an {@link Fn1}&lt;A, C&gt;
107107
*/
108108
@Override
109109
default <C> Fn1<A, C> diMapR(Function<? super B, ? extends C> fn) {
@@ -117,7 +117,7 @@ default <C> Fn1<A, C> diMapR(Function<? super B, ? extends C> fn) {
117117
* @param <C> the new result type
118118
* @param lFn the contravariant argument mapping function
119119
* @param rFn the covariant result mapping function
120-
* @return a new function from Z (the new argument type) to C (the new result type)
120+
* @return an {@link Fn1}&lt;Z, C&gt;
121121
*/
122122
@Override
123123
default <Z, C> Fn1<Z, C> diMap(Function<? super Z, ? extends A> lFn, Function<? super B, ? extends C> rFn) {
@@ -130,12 +130,12 @@ default <Z> Fn1<Z, B> contraMap(Function<? super Z, ? extends A> fn) {
130130
}
131131

132132
/**
133-
* Override of {@link Function#compose(Function)}, returning an instance of <code>Fn1</code> for compatibility.
133+
* Override of {@link Function#compose(Function)}, returning an instance of {@link Fn1} for compatibility.
134134
* Right-to-left composition.
135135
*
136136
* @param before the function who's return value is this function's argument
137137
* @param <Z> the new argument type
138-
* @return a new function from Z (the new argument type) to B (the same result type)
138+
* @return an {@link Fn1}&lt;Z, B&gt;
139139
*/
140140
@Override
141141
default <Z> Fn1<Z, B> compose(Function<? super Z, ? extends A> before) {
@@ -149,7 +149,7 @@ default <Z> Fn1<Z, B> compose(Function<? super Z, ? extends A> before) {
149149
* @param before the function to pass its return value to this function's input
150150
* @param <Y> the resulting function's first argument type
151151
* @param <Z> the resulting function's second argument type
152-
* @return a new function from Y x Z to B
152+
* @return an {@link Fn2}&lt;Y, Z, B&gt;
153153
*/
154154
default <Y, Z> Fn2<Y, Z, B> compose(BiFunction<? super Y, ? super Z, ? extends A> before) {
155155
return compose(fn2(before));
@@ -161,7 +161,7 @@ default <Y, Z> Fn2<Y, Z, B> compose(BiFunction<? super Y, ? super Z, ? extends A
161161
* @param before the function to pass its return value to this function's input
162162
* @param <Y> the resulting function's first argument type
163163
* @param <Z> the resulting function's second argument type
164-
* @return a new function from Y x Z to B
164+
* @return an {@link Fn2}&lt;Y, Z, B&gt;
165165
*/
166166
default <Y, Z> Fn2<Y, Z, B> compose(Fn2<? super Y, ? super Z, ? extends A> before) {
167167
return fn2(before.fmap(this::compose))::apply;
@@ -174,19 +174,19 @@ default <Y, Z> Fn2<Y, Z, B> compose(Fn2<? super Y, ? super Z, ? extends A> befor
174174
* @param after the function to invoke on this function's return value
175175
* @param <C> the resulting function's second argument type
176176
* @param <D> the resulting function's return type
177-
* @return a new function from A x C to D
177+
* @return an {@link Fn2}&lt;A, C, D&gt;
178178
*/
179179
default <C, D> Fn2<A, C, D> andThen(BiFunction<? super B, ? super C, ? extends D> after) {
180180
return (a, c) -> after.apply(apply(a), c);
181181
}
182182

183183
/**
184-
* Override of {@link Function#andThen(Function)}, returning an instance of <code>Fn1</code> for compatibility.
184+
* Override of {@link Function#andThen(Function)}, returning an instance of {@link Fn1} for compatibility.
185185
* Left-to-right composition.
186186
*
187187
* @param after the function to invoke on this function's return value
188188
* @param <C> the new result type
189-
* @return a new function from A (the same argument type) to C (the new result type)
189+
* @return an <code>{@link Fn1}&lt;A, C&gt;</code>
190190
*/
191191
@Override
192192
default <C> Fn1<A, C> andThen(Function<? super B, ? extends C> after) {
@@ -200,7 +200,7 @@ default <C> Fn1<A, C> andThen(Function<? super B, ? extends C> after) {
200200
* @param function the function to adapt
201201
* @param <A> the input argument type
202202
* @param <B> the output type
203-
* @return the Fn1
203+
* @return the {@link Fn1}
204204
*/
205205
static <A, B> Fn1<A, B> fn1(Function<? super A, ? extends B> function) {
206206
return function::apply;

src/main/java/com/jnape/palatable/lambda/functions/Fn2.java

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.jnape.palatable.lambda.functions;
22

33
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
4+
import com.jnape.palatable.lambda.functor.Applicative;
45

56
import java.util.function.BiFunction;
67
import java.util.function.Function;
78

9+
import static com.jnape.palatable.lambda.functions.Fn3.fn3;
10+
811
/**
9-
* A function taking two arguments. Note that defining <code>Fn2</code> in terms of <code>Fn1</code> provides a
10-
* reasonable approximation of currying in the form of multiple <code>apply</code> overloads that take different numbers
11-
* of arguments.
12+
* A function taking two arguments.
13+
* <p>
14+
* Note that defining {@link Fn2} in terms of <code>Fn1</code> provides a reasonable approximation of currying in the
15+
* form of multiple {@link Fn2#apply} overloads that take different numbers of arguments.
1216
*
1317
* @param <A> The first argument type
1418
* @param <B> The second argument type
@@ -28,11 +32,11 @@ public interface Fn2<A, B, C> extends Fn1<A, Fn1<B, C>> {
2832
C apply(A a, B b);
2933

3034
/**
31-
* Same as normal composition, except that the result is an instance of <code>Fn2</code> for convenience.
35+
* Same as normal composition, except that the result is an instance of {@link Fn2} for convenience.
3236
*
3337
* @param before the function who's return value is this function's argument
3438
* @param <Z> the new argument type
35-
* @return a new Fn2&lt;Z,B,C&gt;
39+
* @return an {@link Fn2}&lt;Z, B, C&gt;
3640
*/
3741
@Override
3842
default <Z> Fn2<Z, B, C> compose(Function<? super Z, ? extends A> before) {
@@ -43,7 +47,7 @@ default <Z> Fn2<Z, B, C> compose(Function<? super Z, ? extends A> before) {
4347
* Partially apply this function by passing its first argument.
4448
*
4549
* @param a the first argument
46-
* @return an Fn1 that takes the second argument and returns the result
50+
* @return an {@link Fn1}&lt;B, C&gt;
4751
*/
4852
@Override
4953
default Fn1<B, C> apply(A a) {
@@ -53,31 +57,56 @@ default Fn1<B, C> apply(A a) {
5357
/**
5458
* Flip the order of the arguments.
5559
*
56-
* @return an Fn2 that takes the first and second arguments in reversed order
60+
* @return an {@link Fn2}&lt;B, A, C&gt;
5761
*/
5862
default Fn2<B, A, C> flip() {
5963
return (b, a) -> apply(a, b);
6064
}
6165

6266
/**
63-
* Returns an <code>Fn1</code> that takes the arguments as a <code>Tuple2&lt;A, B&gt;</code>.
67+
* Returns an {@link Fn1} that takes the arguments as a <code>{@link Tuple2}&lt;A, B&gt;</code>.
6468
*
65-
* @return an Fn1 taking a Tuple2
69+
* @return an {@link Fn1} taking a {@link Tuple2}
6670
*/
6771
default Fn1<Tuple2<A, B>, C> uncurry() {
6872
return (ab) -> apply(ab._1(), ab._2());
6973
}
7074

7175
/**
72-
* View this <code>Fn2</code> as a <code>j.u.f.BiFunction</code>.
76+
* View this {@link Fn2} as a {@link BiFunction}.
7377
*
74-
* @return the same logic as a <code>BiFunction</code>
78+
* @return the same logic as a {@link BiFunction}
7579
* @see BiFunction
7680
*/
7781
default BiFunction<A, B, C> toBiFunction() {
7882
return this::apply;
7983
}
8084

85+
@Override
86+
default <D> Fn2<A, B, C> discardR(Applicative<D, Fn1<A, ?>> appB) {
87+
return fn2(Fn1.super.discardR(appB));
88+
}
89+
90+
@Override
91+
default <Z> Fn2<Z, B, C> diMapL(Function<? super Z, ? extends A> fn) {
92+
return fn2(Fn1.super.diMapL(fn));
93+
}
94+
95+
@Override
96+
default <Z> Fn2<Z, B, C> contraMap(Function<? super Z, ? extends A> fn) {
97+
return fn2(Fn1.super.contraMap(fn));
98+
}
99+
100+
@Override
101+
default <Y, Z> Fn3<Y, Z, B, C> compose(BiFunction<? super Y, ? super Z, ? extends A> before) {
102+
return fn3(Fn1.super.compose(before));
103+
}
104+
105+
@Override
106+
default <Y, Z> Fn3<Y, Z, B, C> compose(Fn2<? super Y, ? super Z, ? extends A> before) {
107+
return fn3(Fn1.super.compose(before));
108+
}
109+
81110
/**
82111
* Static factory method for wrapping a {@link BiFunction} in an {@link Fn2}. Useful for avoid explicit casting when
83112
* using method references as {@link Fn2}s.
@@ -86,7 +115,7 @@ default BiFunction<A, B, C> toBiFunction() {
86115
* @param <A> the first input argument type
87116
* @param <B> the second input argument type
88117
* @param <C> the output type
89-
* @return the Fn2
118+
* @return the {@link Fn2}
90119
*/
91120
static <A, B, C> Fn2<A, B, C> fn2(BiFunction<? super A, ? super B, ? extends C> biFunction) {
92121
return biFunction::apply;
@@ -99,7 +128,7 @@ static <A, B, C> Fn2<A, B, C> fn2(BiFunction<? super A, ? super B, ? extends C>
99128
* @param <A> the first input argument type
100129
* @param <B> the second input argument type
101130
* @param <C> the output type
102-
* @return the Fn2
131+
* @return the {@link Fn2}
103132
*/
104133
static <A, B, C> Fn2<A, B, C> fn2(Fn1<A, Fn1<B, C>> curriedFn1) {
105134
return (a, b) -> curriedFn1.apply(a).apply(b);

0 commit comments

Comments
 (0)