Skip to content

Commit 817af07

Browse files
committed
Adding Pure implementations for every Monad as a static method on its class
1 parent 6dc9ee1 commit 817af07

76 files changed

Lines changed: 1500 additions & 25 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/main/java/com/jnape/palatable/lambda/adt/Either.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.jnape.palatable.lambda.functions.Fn2;
88
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
99
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek2;
10+
import com.jnape.palatable.lambda.functions.specialized.Pure;
1011
import com.jnape.palatable.lambda.functions.specialized.SideEffect;
1112
import com.jnape.palatable.lambda.functor.Applicative;
1213
import com.jnape.palatable.lambda.functor.Bifunctor;
@@ -403,6 +404,15 @@ public static <L, R> Either<L, R> right(R r) {
403404
return new Right<>(r);
404405
}
405406

407+
/**
408+
* The canonical {@link Pure} instance for {@link Either}.
409+
*
410+
* @return the {@link Pure} instance
411+
*/
412+
public static <L> Pure<Either<L, ?>> pureEither() {
413+
return Either::right;
414+
}
415+
406416
private static final class Left<L, R> extends Either<L, R> {
407417
private final L l;
408418

src/main/java/com/jnape/palatable/lambda/adt/Maybe.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.jnape.palatable.lambda.functions.Fn0;
99
import com.jnape.palatable.lambda.functions.Fn1;
1010
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
11+
import com.jnape.palatable.lambda.functions.specialized.Pure;
1112
import com.jnape.palatable.lambda.functor.Applicative;
1213
import com.jnape.palatable.lambda.functor.Functor;
1314
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -296,6 +297,15 @@ public static <A> Maybe<A> nothing() {
296297
return (Maybe<A>) Nothing.INSTANCE;
297298
}
298299

300+
/**
301+
* The canonical {@link Pure} instance for {@link Maybe}.
302+
*
303+
* @return the {@link Pure} instance
304+
*/
305+
public static Pure<Maybe<? >> pureMaybe() {
306+
return Maybe::just;
307+
}
308+
299309
private static final class Nothing<A> extends Maybe<A> {
300310
private static final Nothing<?> INSTANCE = new Nothing<>();
301311

src/main/java/com/jnape/palatable/lambda/adt/These.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jnape.palatable.lambda.adt.coproduct.CoProduct3;
55
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
66
import com.jnape.palatable.lambda.functions.Fn1;
7+
import com.jnape.palatable.lambda.functions.specialized.Pure;
78
import com.jnape.palatable.lambda.functor.Applicative;
89
import com.jnape.palatable.lambda.functor.Bifunctor;
910
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -164,6 +165,16 @@ public static <A, B> These<A, B> both(A a, B b) {
164165
return new Both<>(tuple(a, b));
165166
}
166167

168+
/**
169+
* The canonical {@link Pure} instance for {@link These}.
170+
*
171+
* @param <A> the first possible type
172+
* @return the {@link Pure} instance
173+
*/
174+
public static <A> Pure<These<A, ?>> pureThese() {
175+
return These::b;
176+
}
177+
167178
private static final class _A<A, B> extends These<A, B> {
168179

169180
private final A a;

src/main/java/com/jnape/palatable/lambda/adt/Try.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
44
import com.jnape.palatable.lambda.functions.Fn0;
55
import com.jnape.palatable.lambda.functions.Fn1;
6+
import com.jnape.palatable.lambda.functions.builtin.fn1.Downcast;
7+
import com.jnape.palatable.lambda.functions.specialized.Pure;
68
import com.jnape.palatable.lambda.functions.specialized.SideEffect;
79
import com.jnape.palatable.lambda.functor.Applicative;
810
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -44,10 +46,9 @@ private Try() {
4446
* @param recoveryFn the function mapping the {@link Throwable} to the result
4547
* @return a new {@link Try} instance around either the original successful result or the mapped result
4648
*/
47-
@SuppressWarnings("unchecked")
4849
public final <S extends Throwable> Try<A> catching(Class<S> throwableType,
4950
Fn1<? super S, ? extends A> recoveryFn) {
50-
return catching(throwableType::isInstance, t -> recoveryFn.apply((S) t));
51+
return catching(throwableType::isInstance, t -> recoveryFn.apply(Downcast.<S, Throwable>downcast(t)));
5152
}
5253

5354
/**
@@ -76,13 +77,13 @@ public final Try<A> catching(Fn1<? super Throwable, ? extends Boolean> predicate
7677
* rules above
7778
*/
7879
public final Try<A> ensuring(SideEffect sideEffect) {
79-
return match(t -> trying(sideEffect)
80-
.<Try<A>>fmap(constantly(failure(t)))
81-
.recover(t2 -> {
82-
t.addSuppressed(t2);
83-
return failure(t);
84-
}),
85-
a -> trying(sideEffect).fmap(constantly(a)));
80+
return this.<Try<A>>match(t -> trying(sideEffect)
81+
.<Try<A>>fmap(constantly(failure(t)))
82+
.recover(t2 -> {
83+
t.addSuppressed(t2);
84+
return failure(t);
85+
}),
86+
a -> trying(sideEffect).fmap(constantly(a)));
8687
}
8788

8889
/**
@@ -133,7 +134,6 @@ public final <T extends Throwable> A orThrow() throws T {
133134
*/
134135
public abstract <T extends Throwable> A orThrow(Fn1<? super Throwable, ? extends T> fn) throws T;
135136

136-
137137
/**
138138
* If this is a success, wrap the value in a {@link Maybe#just} and return it. Otherwise, return {@link
139139
* Maybe#nothing()}.
@@ -381,6 +381,15 @@ public static <A extends AutoCloseable, B extends AutoCloseable, C extends AutoC
381381
return withResources(fn0, bFn, b -> withResources(() -> cFn.apply(b), fn::apply));
382382
}
383383

384+
/**
385+
* The canonical {@link Pure} instance for {@link Try}.
386+
*
387+
* @return the {@link Pure} instance
388+
*/
389+
public static Pure<Try<?>> pureTry() {
390+
return Try::success;
391+
}
392+
384393
private static final class Failure<A> extends Try<A> {
385394
private final Throwable t;
386395

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice2.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.Functor;
@@ -181,6 +182,16 @@ public static <A, B> Choice2<A, B> b(B b) {
181182
return new _B<>(b);
182183
}
183184

185+
/**
186+
* The canonical {@link Pure} instance for {@link Choice2}.
187+
*
188+
* @param <A> the first possible type
189+
* @return the {@link Pure} instance
190+
*/
191+
public static <A> Pure<Choice2<A, ?>> pureChoice() {
192+
return Choice2::b;
193+
}
194+
184195
private static final class _A<A, B> extends Choice2<A, B> {
185196

186197
private final A a;

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice3.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple3;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.Functor;
@@ -198,6 +199,17 @@ public static <A, B, C> Choice3<A, B, C> c(C c) {
198199
return new _C<>(c);
199200
}
200201

202+
/**
203+
* The canonical {@link Pure} instance for {@link Choice3}.
204+
*
205+
* @param <A> the first possible type
206+
* @param <B> the second possible type
207+
* @return the {@link Pure} instance
208+
*/
209+
public static <A, B> Pure<Choice3<A, B, ?>> pureChoice() {
210+
return Choice3::c;
211+
}
212+
201213
private static final class _A<A, B, C> extends Choice3<A, B, C> {
202214

203215
private final A a;
@@ -296,6 +308,4 @@ public String toString() {
296308
'}';
297309
}
298310
}
299-
300-
301311
}

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice4.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple4;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.Functor;
@@ -216,6 +217,18 @@ public static <A, B, C, D> Choice4<A, B, C, D> d(D d) {
216217
return new _D<>(d);
217218
}
218219

220+
/**
221+
* The canonical {@link Pure} instance for {@link Choice4}.
222+
*
223+
* @param <A> the first possible type
224+
* @param <B> the second possible type
225+
* @param <C> the third possible type
226+
* @return the {@link Pure} instance
227+
*/
228+
public static <A, B, C> Pure<Choice4<A, B, C, ?>> pureChoice() {
229+
return Choice4::d;
230+
}
231+
219232
private static final class _A<A, B, C, D> extends Choice4<A, B, C, D> {
220233

221234
private final A a;

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice5.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple5;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -241,6 +242,19 @@ public static <A, B, C, D, E> Choice5<A, B, C, D, E> e(E e) {
241242
return new _E<>(e);
242243
}
243244

245+
/**
246+
* The canonical {@link Pure} instance for {@link Choice5}.
247+
*
248+
* @param <A> the first possible type
249+
* @param <B> the second possible type
250+
* @param <C> the third possible type
251+
* @param <D> the fourth possible type
252+
* @return the {@link Pure} instance
253+
*/
254+
public static <A, B, C, D> Pure<Choice5<A, B, C, D, ?>> pureChoice() {
255+
return Choice5::e;
256+
}
257+
244258
private static final class _A<A, B, C, D, E> extends Choice5<A, B, C, D, E> {
245259

246260
private final A a;

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice6.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple6;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -268,6 +269,20 @@ public static <A, B, C, D, E, F> Choice6<A, B, C, D, E, F> f(F f) {
268269
return new _F<>(f);
269270
}
270271

272+
/**
273+
* The canonical {@link Pure} instance for {@link Choice6}.
274+
*
275+
* @param <A> the first possible type
276+
* @param <B> the second possible type
277+
* @param <C> the third possible type
278+
* @param <D> the fourth possible type
279+
* @param <E> the fifth possible type
280+
* @return the {@link Pure} instance
281+
*/
282+
public static <A, B, C, D, E> Pure<Choice6<A, B, C, D, E, ?>> pureChoice() {
283+
return Choice6::f;
284+
}
285+
271286
private static final class _A<A, B, C, D, E, F> extends Choice6<A, B, C, D, E, F> {
272287

273288
private final A a;

src/main/java/com/jnape/palatable/lambda/adt/choice/Choice7.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple7;
88
import com.jnape.palatable.lambda.functions.Fn1;
9+
import com.jnape.palatable.lambda.functions.specialized.Pure;
910
import com.jnape.palatable.lambda.functor.Applicative;
1011
import com.jnape.palatable.lambda.functor.Bifunctor;
1112
import com.jnape.palatable.lambda.functor.builtin.Lazy;
@@ -294,6 +295,21 @@ public static <A, B, C, D, E, F, G> Choice7<A, B, C, D, E, F, G> g(G g) {
294295
return new _G<>(g);
295296
}
296297

298+
/**
299+
* The canonical {@link Pure} instance for {@link Choice7}.
300+
*
301+
* @param <A> the first possible type
302+
* @param <B> the second possible type
303+
* @param <C> the third possible type
304+
* @param <D> the fourth possible type
305+
* @param <E> the fifth possible type
306+
* @param <F> the sixth possible type
307+
* @return the {@link Pure} instance
308+
*/
309+
public static <A, B, C, D, E, F> Pure<Choice7<A, B, C, D, E, F, ?>> pureChoice() {
310+
return Choice7::g;
311+
}
312+
297313
private static final class _A<A, B, C, D, E, F, G> extends Choice7<A, B, C, D, E, F, G> {
298314

299315
private final A a;

0 commit comments

Comments
 (0)