Skip to content

Commit eb596f8

Browse files
committed
Cutting all java.util.function/Runnable/Callable ties
- Function replaced with Fn1 - BiFunction replaced with Fn2 - Supplier replaced with Fn0 - Consumer replaced with Effect - Runnable replaced with SideEffect in IO - j.u.f.Predicate replaced with Predicate - j.u.f.BiPredicate replaced with BiPredicate
1 parent 20e00f5 commit eb596f8

File tree

218 files changed

+2218
-2434
lines changed

Some content is hidden

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

218 files changed

+2218
-2434
lines changed

CHANGELOG.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
99
factory methods should continue to work (by simply targeting `Supplier` now instead of an
1010
anonymous `IO`), but some might need to be reworked, and subtyping is obviously no longer
1111
supported.
12+
- ***Breaking Change***: Breaking all dependency on `java.util.function` types across the board. All `Fn*` types target
13+
methods now support throwing `Throwable`; `apply` is now defaulted and will simply bypass javac
14+
to throw checked exceptions as if they were unchecked. All `Checked` variants have been
15+
eliminated as a consequence, as they are no longer necessary. Also, straggler functions like
16+
`Partial2/3` that only existed to aid in partial application of non-curried functions are now
17+
superfluous, and have also been eliminated.
1218
- ***Breaking Change***: `FoldRight` now requires `Lazy` as part of its interface to support short-circuiting operations
1319
- ***Breaking Change***: Eliminated all raw types and java11 warnings. This required using capture in unification
1420
parameters for Functor and friends, so nearly every functor's type-signature changed.
@@ -17,11 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1723
type is now the supertype of `Lens` and `Iso`, and `lens` package has been moved to `optics`
1824
- ***Breaking Change***: `Try` and `Either` no longer preserve `Throwable` type since it was inherently not type-safe
1925
anyway; Try is therefore no longer a `Bifunctor`, and `orThrow` can be used to declare checked
20-
exceptions that could be caught by corresponding catch blocks
21-
- ***Breaking Change***: All `Fn*` types target methods now support throwing `Throwable`; `apply` is now defaulted and
22-
will simply bypass javac to throw checked exceptions as if they were unchecked. This allows all
23-
checked variants to be eliminated
24-
- ***Breaking Change***: All `Checked` variants have been eliminated
26+
exceptions that could be caught by corresponding catch blocks
2527
- `IO` is now stack-safe, regardless of whether the composition nests linearly or recursively
2628

2729
### Added

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

Lines changed: 67 additions & 74 deletions
Large diffs are not rendered by default.

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
66
import com.jnape.palatable.lambda.adt.hlist.HList;
77
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
8+
import com.jnape.palatable.lambda.functions.Effect;
89
import com.jnape.palatable.lambda.functions.Fn0;
10+
import com.jnape.palatable.lambda.functions.Fn1;
911
import com.jnape.palatable.lambda.functions.builtin.fn2.Peek;
1012
import com.jnape.palatable.lambda.functor.Applicative;
1113
import com.jnape.palatable.lambda.functor.Functor;
@@ -15,9 +17,6 @@
1517

1618
import java.util.Objects;
1719
import java.util.Optional;
18-
import java.util.function.Consumer;
19-
import java.util.function.Function;
20-
import java.util.function.Supplier;
2120

2221
import static com.jnape.palatable.lambda.adt.Either.left;
2322
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
@@ -44,11 +43,11 @@ private Maybe() {
4443
/**
4544
* If the value is present, return it; otherwise, return the value supplied by <code>otherSupplier</code>.
4645
*
47-
* @param otherSupplier the supplier for the other value
46+
* @param otherFn0 the supplier for the other value
4847
* @return this value, or the supplied other value
4948
*/
50-
public final A orElseGet(Supplier<A> otherSupplier) {
51-
return match(__ -> otherSupplier.get(), id());
49+
public final A orElseGet(Fn0<A> otherFn0) {
50+
return match(__ -> otherFn0.apply(), id());
5251
}
5352

5453
/**
@@ -72,7 +71,7 @@ public final A orElse(A other) {
7271
*/
7372
public final <E extends Throwable> A orElseThrow(Fn0<? extends E> throwableSupplier) throws E {
7473
return orElseGet(fn0(() -> {
75-
throw throwableSupplier.get();
74+
throw throwableSupplier.apply();
7675
}));
7776
}
7877

@@ -83,20 +82,20 @@ public final <E extends Throwable> A orElseThrow(Fn0<? extends E> throwableSuppl
8382
* @param predicate the predicate to apply to the possibly absent value
8483
* @return maybe the present value that satisfied the predicate
8584
*/
86-
public final Maybe<A> filter(Function<? super A, ? extends Boolean> predicate) {
85+
public final Maybe<A> filter(Fn1<? super A, ? extends Boolean> predicate) {
8786
return flatMap(a -> predicate.apply(a) ? just(a) : nothing());
8887
}
8988

9089
/**
9190
* If this value is absent, return the value supplied by <code>lSupplier</code> wrapped in <code>Either.left</code>.
9291
* Otherwise, wrap the value in <code>Either.right</code> and return it.
9392
*
94-
* @param lSupplier the supplier for the left value
95-
* @param <L> the left parameter type
93+
* @param <L> the left parameter type
94+
* @param lFn0 the supplier for the left value
9695
* @return this value wrapped in an Either.right, or an Either.left around the result of lSupplier
9796
*/
98-
public final <L> Either<L, A> toEither(Supplier<L> lSupplier) {
99-
return fmap(Either::<L, A>right).orElseGet(() -> left(lSupplier.get()));
97+
public final <L> Either<L, A> toEither(Fn0<L> lFn0) {
98+
return fmap(Either::<L, A>right).orElseGet(() -> left(lFn0.apply()));
10099
}
101100

102101
/**
@@ -127,15 +126,15 @@ public final <B> Maybe<B> pure(B b) {
127126
* {@link Maybe#nothing}.
128127
*/
129128
@Override
130-
public final <B> Maybe<B> fmap(Function<? super A, ? extends B> fn) {
129+
public final <B> Maybe<B> fmap(Fn1<? super A, ? extends B> fn) {
131130
return Monad.super.<B>fmap(fn).coerce();
132131
}
133132

134133
/**
135134
* {@inheritDoc}
136135
*/
137136
@Override
138-
public final <B> Maybe<B> zip(Applicative<Function<? super A, ? extends B>, Maybe<?>> appFn) {
137+
public final <B> Maybe<B> zip(Applicative<Fn1<? super A, ? extends B>, Maybe<?>> appFn) {
139138
return Monad.super.zip(appFn).coerce();
140139
}
141140

@@ -147,8 +146,7 @@ public final <B> Maybe<B> zip(Applicative<Function<? super A, ? extends B>, Mayb
147146
* @return the zipped {@link Maybe}
148147
*/
149148
@Override
150-
public <B> Lazy<Maybe<B>> lazyZip(
151-
Lazy<? extends Applicative<Function<? super A, ? extends B>, Maybe<?>>> lazyAppFn) {
149+
public <B> Lazy<Maybe<B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A, ? extends B>, Maybe<?>>> lazyAppFn) {
152150
return match(constantly(lazy(nothing())),
153151
a -> lazyAppFn.fmap(maybeF -> maybeF.<B>fmap(f -> f.apply(a)).coerce()));
154152
}
@@ -174,8 +172,8 @@ public final <B> Maybe<A> discardR(Applicative<B, Maybe<?>> appB) {
174172
*/
175173
@SuppressWarnings("RedundantTypeArguments")
176174
@Override
177-
public final <B> Maybe<B> flatMap(Function<? super A, ? extends Monad<B, Maybe<?>>> f) {
178-
return match(constantly(nothing()), f.andThen(Monad<B, Maybe<?>>::coerce));
175+
public final <B> Maybe<B> flatMap(Fn1<? super A, ? extends Monad<B, Maybe<?>>> f) {
176+
return match(constantly(nothing()), f.fmap(Monad<B, Maybe<?>>::coerce));
179177
}
180178

181179
/**
@@ -205,19 +203,19 @@ public Choice2<A, Unit> invert() {
205203
/**
206204
* If this value is present, accept it by <code>consumer</code>; otherwise, do nothing.
207205
*
208-
* @param consumer the consumer
206+
* @param effect the consumer
209207
* @return the same Maybe instance
210208
*/
211-
public final Maybe<A> peek(Consumer<A> consumer) {
212-
return Peek.peek(consumer, this);
209+
public final Maybe<A> peek(Effect<A> effect) {
210+
return Peek.peek(effect, this);
213211
}
214212

215213
@Override
216214
@SuppressWarnings("unchecked")
217215
public final <B, App extends Applicative<?, App>, TravB extends Traversable<B, Maybe<?>>,
218216
AppB extends Applicative<B, App>,
219-
AppTrav extends Applicative<TravB, App>> AppTrav traverse(Function<? super A, ? extends AppB> fn,
220-
Function<? super TravB, ? extends AppTrav> pure) {
217+
AppTrav extends Applicative<TravB, App>> AppTrav traverse(Fn1<? super A, ? extends AppB> fn,
218+
Fn1<? super TravB, ? extends AppTrav> pure) {
221219
return match(__ -> pure.apply((TravB) Maybe.<B>nothing()), a -> (AppTrav) fn.apply(a).fmap(Maybe::just));
222220
}
223221

@@ -289,7 +287,7 @@ private Nothing() {
289287
}
290288

291289
@Override
292-
public <R> R match(Function<? super Unit, ? extends R> aFn, Function<? super A, ? extends R> bFn) {
290+
public <R> R match(Fn1<? super Unit, ? extends R> aFn, Fn1<? super A, ? extends R> bFn) {
293291
return aFn.apply(UNIT);
294292
}
295293

@@ -308,7 +306,7 @@ private Just(A a) {
308306
}
309307

310308
@Override
311-
public <R> R match(Function<? super Unit, ? extends R> aFn, Function<? super A, ? extends R> bFn) {
309+
public <R> R match(Fn1<? super Unit, ? extends R> aFn, Fn1<? super A, ? extends R> bFn) {
312310
return bFn.apply(a);
313311
}
314312

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.jnape.palatable.lambda.adt.coproduct.CoProduct2;
44
import com.jnape.palatable.lambda.adt.coproduct.CoProduct3;
55
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
6+
import com.jnape.palatable.lambda.functions.Fn1;
67
import com.jnape.palatable.lambda.functor.Applicative;
78
import com.jnape.palatable.lambda.functor.Bifunctor;
89
import com.jnape.palatable.lambda.functor.builtin.Lazy;
910
import com.jnape.palatable.lambda.monad.Monad;
1011
import com.jnape.palatable.lambda.traversable.Traversable;
1112

1213
import java.util.Objects;
13-
import java.util.function.Function;
1414

1515
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
1616
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
@@ -25,7 +25,11 @@
2525
* @param <A> the first possible type
2626
* @param <B> the second possible type
2727
*/
28-
public abstract class These<A, B> implements CoProduct3<A, B, Tuple2<A, B>, These<A, B>>, Monad<B, These<A, ?>>, Bifunctor<A, B, These<?, ?>>, Traversable<B, These<A, ?>> {
28+
public abstract class These<A, B> implements
29+
CoProduct3<A, B, Tuple2<A, B>, These<A, B>>,
30+
Monad<B, These<A, ?>>,
31+
Bifunctor<A, B, These<?, ?>>,
32+
Traversable<B, These<A, ?>> {
2933

3034
private These() {
3135
}
@@ -34,16 +38,16 @@ private These() {
3438
* {@inheritDoc}
3539
*/
3640
@Override
37-
public final <C, D> These<C, D> biMap(Function<? super A, ? extends C> lFn,
38-
Function<? super B, ? extends D> rFn) {
41+
public final <C, D> These<C, D> biMap(Fn1<? super A, ? extends C> lFn,
42+
Fn1<? super B, ? extends D> rFn) {
3943
return match(a -> a(lFn.apply(a)), b -> b(rFn.apply(b)), into((a, b) -> both(lFn.apply(a), rFn.apply(b))));
4044
}
4145

4246
/**
4347
* {@inheritDoc}
4448
*/
4549
@Override
46-
public final <C> These<A, C> flatMap(Function<? super B, ? extends Monad<C, These<A, ?>>> f) {
50+
public final <C> These<A, C> flatMap(Fn1<? super B, ? extends Monad<C, These<A, ?>>> f) {
4751
return match(These::a, b -> f.apply(b).coerce(), into((a, b) -> f.apply(b).<These<A, C>>coerce().biMapL(constantly(a))));
4852
}
4953

@@ -58,9 +62,8 @@ public final <C> These<A, C> pure(C c) {
5862
@Override
5963
@SuppressWarnings("unchecked")
6064
public <C, App extends Applicative<?, App>, TravB extends Traversable<C, These<A, ?>>,
61-
AppB extends Applicative<C, App>,
62-
AppTrav extends Applicative<TravB, App>> AppTrav traverse(Function<? super B, ? extends AppB> fn,
63-
Function<? super TravB, ? extends AppTrav> pure) {
65+
AppB extends Applicative<C, App>, AppTrav extends Applicative<TravB, App>>
66+
AppTrav traverse(Fn1<? super B, ? extends AppB> fn, Fn1<? super TravB, ? extends AppTrav> pure) {
6467
return match(a -> pure.apply((TravB) a(a)),
6568
b -> fn.apply(b).fmap(this::pure).<TravB>fmap(Applicative::coerce).coerce(),
6669
into((a, b) -> fn.apply(b).fmap(c -> both(a, c)).<TravB>fmap(Applicative::coerce).coerce()));
@@ -71,7 +74,7 @@ AppTrav extends Applicative<TravB, App>> AppTrav traverse(Function<? super B, ?
7174
*/
7275
@Override
7376
@SuppressWarnings("unchecked")
74-
public final <Z> These<Z, B> biMapL(Function<? super A, ? extends Z> fn) {
77+
public final <Z> These<Z, B> biMapL(Fn1<? super A, ? extends Z> fn) {
7578
return (These<Z, B>) Bifunctor.super.biMapL(fn);
7679
}
7780

@@ -80,29 +83,29 @@ public final <Z> These<Z, B> biMapL(Function<? super A, ? extends Z> fn) {
8083
*/
8184
@Override
8285
@SuppressWarnings("unchecked")
83-
public final <C> These<A, C> biMapR(Function<? super B, ? extends C> fn) {
86+
public final <C> These<A, C> biMapR(Fn1<? super B, ? extends C> fn) {
8487
return (These<A, C>) Bifunctor.super.biMapR(fn);
8588
}
8689

8790
/**
8891
* {@inheritDoc}
8992
*/
9093
@Override
91-
public final <C> These<A, C> fmap(Function<? super B, ? extends C> fn) {
94+
public final <C> These<A, C> fmap(Fn1<? super B, ? extends C> fn) {
9295
return Monad.super.<C>fmap(fn).coerce();
9396
}
9497

9598
/**
9699
* {@inheritDoc}
97100
*/
98101
@Override
99-
public final <C> These<A, C> zip(Applicative<Function<? super B, ? extends C>, These<A, ?>> appFn) {
102+
public final <C> These<A, C> zip(Applicative<Fn1<? super B, ? extends C>, These<A, ?>> appFn) {
100103
return Monad.super.zip(appFn).coerce();
101104
}
102105

103106
@Override
104107
public <C> Lazy<These<A, C>> lazyZip(
105-
Lazy<? extends Applicative<Function<? super B, ? extends C>, These<A, ?>>> lazyAppFn) {
108+
Lazy<? extends Applicative<Fn1<? super B, ? extends C>, These<A, ?>>> lazyAppFn) {
106109
return projectA().<Lazy<These<A, C>>>fmap(a -> lazy(a(a)))
107110
.orElseGet(() -> Monad.super.lazyZip(lazyAppFn).fmap(Monad<C, These<A, ?>>::coerce));
108111
}
@@ -170,8 +173,8 @@ private _A(A a) {
170173
}
171174

172175
@Override
173-
public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn,
174-
Function<? super Tuple2<A, B>, ? extends R> cFn) {
176+
public <R> R match(Fn1<? super A, ? extends R> aFn, Fn1<? super B, ? extends R> bFn,
177+
Fn1<? super Tuple2<A, B>, ? extends R> cFn) {
175178
return aFn.apply(a);
176179
}
177180

@@ -199,8 +202,8 @@ private _B(B b) {
199202
}
200203

201204
@Override
202-
public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn,
203-
Function<? super Tuple2<A, B>, ? extends R> cFn) {
205+
public <R> R match(Fn1<? super A, ? extends R> aFn, Fn1<? super B, ? extends R> bFn,
206+
Fn1<? super Tuple2<A, B>, ? extends R> cFn) {
204207
return bFn.apply(b);
205208
}
206209

@@ -228,8 +231,8 @@ private Both(Tuple2<A, B> tuple) {
228231
}
229232

230233
@Override
231-
public <R> R match(Function<? super A, ? extends R> aFn, Function<? super B, ? extends R> bFn,
232-
Function<? super Tuple2<A, B>, ? extends R> cFn) {
234+
public <R> R match(Fn1<? super A, ? extends R> aFn, Fn1<? super B, ? extends R> bFn,
235+
Fn1<? super Tuple2<A, B>, ? extends R> cFn) {
233236
return cFn.apply(both);
234237
}
235238

0 commit comments

Comments
 (0)