Skip to content

Commit ff424b0

Browse files
committed
CheckedEffect/CheckedSupplier are CheckedFn1, now with more overrides
1 parent fcf9ff8 commit ff424b0

File tree

7 files changed

+416
-12
lines changed

7 files changed

+416
-12
lines changed

CHANGELOG.md

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

66
## [Unreleased]
77
### Added
8-
- `MergeMaps`, a `Monoid` on `Map` formed by `Map#merge`
8+
- `MergeMaps`, a `Monoid` on `Map` formed by `Map#merge`
9+
- `CheckedEffect` is now a `CheckedFn1`
10+
- `CheckedSupplier` is now a `CheckedFn1`
11+
- `CheckedFn1` now overrides all possible methods with covariant return type
912

1013
### Fixed
1114
- issue where certain ways to compose `Effect`s unintentionally nullified the effect

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedEffect.java

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
package com.jnape.palatable.lambda.functions.specialized.checked;
22

3+
import com.jnape.palatable.lambda.adt.Unit;
34
import com.jnape.palatable.lambda.functions.Effect;
5+
import com.jnape.palatable.lambda.functions.Fn1;
6+
import com.jnape.palatable.lambda.functor.Applicative;
7+
import com.jnape.palatable.lambda.io.IO;
8+
9+
import java.util.function.Consumer;
10+
import java.util.function.Function;
411

512
import static com.jnape.palatable.lambda.functions.specialized.checked.Runtime.throwChecked;
13+
import static com.jnape.palatable.lambda.io.IO.io;
614

715
/**
816
* Specialized {@link Effect} that can throw any {@link Throwable}.
@@ -14,8 +22,11 @@
1422
* @see Effect
1523
*/
1624
@FunctionalInterface
17-
public interface CheckedEffect<T extends Throwable, A> extends Effect<A> {
25+
public interface CheckedEffect<T extends Throwable, A> extends Effect<A>, CheckedFn1<T, A, IO<Unit>> {
1826

27+
/**
28+
* {@inheritDoc}
29+
*/
1930
@Override
2031
default void accept(A a) {
2132
try {
@@ -25,6 +36,62 @@ default void accept(A a) {
2536
}
2637
}
2738

39+
/**
40+
* {@inheritDoc}
41+
*/
42+
@Override
43+
default IO<Unit> apply(A a) {
44+
return io(() -> accept(a));
45+
}
46+
47+
/**
48+
* {@inheritDoc}
49+
*/
50+
@Override
51+
default IO<Unit> checkedApply(A a) throws T {
52+
return apply(a);
53+
}
54+
55+
/**
56+
* {@inheritDoc}
57+
*/
58+
@Override
59+
default <Z> CheckedEffect<T, Z> diMapL(Function<? super Z, ? extends A> fn) {
60+
return Effect.super.diMapL(fn)::accept;
61+
}
62+
63+
/**
64+
* {@inheritDoc}
65+
*/
66+
@Override
67+
default <Z> CheckedEffect<T, Z> contraMap(Function<? super Z, ? extends A> fn) {
68+
return Effect.super.contraMap(fn)::accept;
69+
}
70+
71+
/**
72+
* {@inheritDoc}
73+
*/
74+
@Override
75+
default <Z> CheckedEffect<T, Z> compose(Function<? super Z, ? extends A> before) {
76+
return Effect.super.compose(before)::accept;
77+
}
78+
79+
/**
80+
* {@inheritDoc}
81+
*/
82+
@Override
83+
default <C> CheckedEffect<T, A> discardR(Applicative<C, Fn1<A, ?>> appB) {
84+
return Effect.super.discardR(appB)::accept;
85+
}
86+
87+
/**
88+
* {@inheritDoc}
89+
*/
90+
@Override
91+
default CheckedEffect<T, A> andThen(Consumer<? super A> after) {
92+
return Effect.super.andThen(after)::accept;
93+
}
94+
2895
/**
2996
* A version of {@link Effect#accept} that can throw checked exceptions.
3097
*

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedFn1.java

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.jnape.palatable.lambda.functions.specialized.checked;
22

3+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
34
import com.jnape.palatable.lambda.functions.Fn1;
5+
import com.jnape.palatable.lambda.functions.Fn2;
6+
import com.jnape.palatable.lambda.functor.Applicative;
7+
import com.jnape.palatable.lambda.monad.Monad;
8+
9+
import java.util.function.Function;
410

511
import static com.jnape.palatable.lambda.functions.specialized.checked.Runtime.throwChecked;
612

@@ -17,6 +23,9 @@
1723
@FunctionalInterface
1824
public interface CheckedFn1<T extends Throwable, A, B> extends Fn1<A, B> {
1925

26+
/**
27+
* {@inheritDoc}
28+
*/
2029
@Override
2130
default B apply(A a) {
2231
try {
@@ -26,6 +35,119 @@ default B apply(A a) {
2635
}
2736
}
2837

38+
/**
39+
* {@inheritDoc}
40+
*/
41+
@Override
42+
default <C> CheckedFn1<T, A, C> fmap(Function<? super B, ? extends C> f) {
43+
return Fn1.super.<C>fmap(f)::apply;
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
@Override
50+
default <C> CheckedFn1<T, A, C> flatMap(Function<? super B, ? extends Monad<C, Fn1<A, ?>>> f) {
51+
return Fn1.super.flatMap(f).<Fn1<A, C>>coerce()::apply;
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
@Override
58+
default <C> CheckedFn1<T, A, C> discardL(Applicative<C, Fn1<A, ?>> appB) {
59+
return Fn1.super.discardL(appB)::apply;
60+
}
61+
62+
/**
63+
* {@inheritDoc}
64+
*/
65+
@Override
66+
default <C> CheckedFn1<T, A, B> discardR(Applicative<C, Fn1<A, ?>> appB) {
67+
return Fn1.super.discardR(appB)::apply;
68+
}
69+
70+
/**
71+
* {@inheritDoc}
72+
*/
73+
@Override
74+
default <C> CheckedFn1<T, A, C> zip(Applicative<Function<? super B, ? extends C>, Fn1<A, ?>> appFn) {
75+
return Fn1.super.zip(appFn)::apply;
76+
}
77+
78+
/**
79+
* {@inheritDoc}
80+
*/
81+
@Override
82+
default <C> CheckedFn1<T, A, C> zip(Fn2<A, B, C> appFn) {
83+
return Fn1.super.zip(appFn).coerce();
84+
}
85+
86+
/**
87+
* {@inheritDoc}
88+
*/
89+
@Override
90+
default <Z> CheckedFn1<T, Z, B> diMapL(Function<? super Z, ? extends A> fn) {
91+
return Fn1.super.<Z>diMapL(fn).coerce();
92+
}
93+
94+
/**
95+
* {@inheritDoc}
96+
*/
97+
@Override
98+
default <C> CheckedFn1<T, A, C> diMapR(Function<? super B, ? extends C> fn) {
99+
return Fn1.super.<C>diMapR(fn).coerce();
100+
}
101+
102+
/**
103+
* {@inheritDoc}
104+
*/
105+
@Override
106+
default <Z, C> CheckedFn1<T, Z, C> diMap(Function<? super Z, ? extends A> lFn,
107+
Function<? super B, ? extends C> rFn) {
108+
return Fn1.super.diMap(lFn, rFn)::apply;
109+
}
110+
111+
/**
112+
* {@inheritDoc}
113+
*/
114+
@Override
115+
default <C> CheckedFn1<T, Tuple2<C, A>, Tuple2<C, B>> strengthen() {
116+
return Fn1.super.<C>strengthen()::apply;
117+
}
118+
119+
/**
120+
* {@inheritDoc}
121+
*/
122+
@Override
123+
default CheckedFn1<T, A, Tuple2<A, B>> carry() {
124+
return Fn1.super.carry()::apply;
125+
}
126+
127+
/**
128+
* {@inheritDoc}
129+
*/
130+
@Override
131+
default <Z> CheckedFn1<T, Z, B> contraMap(Function<? super Z, ? extends A> fn) {
132+
return Fn1.super.<Z>contraMap(fn).coerce();
133+
}
134+
135+
/**
136+
* {@inheritDoc}
137+
*/
138+
@Override
139+
default <Z> CheckedFn1<T, Z, B> compose(Function<? super Z, ? extends A> before) {
140+
return Fn1.super.<Z>compose(before)::apply;
141+
}
142+
143+
/**
144+
* {@inheritDoc}
145+
*/
146+
@Override
147+
default <C> CheckedFn1<T, A, C> andThen(Function<? super B, ? extends C> after) {
148+
return Fn1.super.<C>andThen(after)::apply;
149+
}
150+
29151
/**
30152
* A version of {@link Fn1#apply} that can throw checked exceptions.
31153
*

src/main/java/com/jnape/palatable/lambda/functions/specialized/checked/CheckedSupplier.java

Lines changed: 99 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package com.jnape.palatable.lambda.functions.specialized.checked;
22

3+
import com.jnape.palatable.lambda.adt.Unit;
4+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
5+
import com.jnape.palatable.lambda.functions.Fn1;
6+
import com.jnape.palatable.lambda.functions.Fn2;
7+
import com.jnape.palatable.lambda.functor.Applicative;
8+
import com.jnape.palatable.lambda.monad.Monad;
9+
10+
import java.util.function.Function;
311
import java.util.function.Supplier;
412

13+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
514
import static com.jnape.palatable.lambda.functions.specialized.checked.Runtime.throwChecked;
615

716
/**
@@ -13,16 +22,7 @@
1322
* @see CheckedRunnable
1423
*/
1524
@FunctionalInterface
16-
public interface CheckedSupplier<T extends Throwable, A> extends Supplier<A> {
17-
18-
@Override
19-
default A get() {
20-
try {
21-
return checkedGet();
22-
} catch (Throwable t) {
23-
throw throwChecked(t);
24-
}
25-
}
25+
public interface CheckedSupplier<T extends Throwable, A> extends Supplier<A>, CheckedFn1<T, Unit, A> {
2626

2727
/**
2828
* A version of {@link Supplier#get()} that can throw checked exceptions.
@@ -41,6 +41,95 @@ default CheckedRunnable<T> toRunnable() {
4141
return this::get;
4242
}
4343

44+
@Override
45+
default A checkedApply(Unit unit) throws T {
46+
return checkedGet();
47+
}
48+
49+
/**
50+
* {@inheritDoc}
51+
*/
52+
@Override
53+
default A get() {
54+
try {
55+
return checkedGet();
56+
} catch (Throwable t) {
57+
throw throwChecked(t);
58+
}
59+
}
60+
61+
/**
62+
* {@inheritDoc}
63+
*/
64+
@Override
65+
default <B> CheckedSupplier<T, B> fmap(Function<? super A, ? extends B> f) {
66+
return CheckedFn1.super.<B>fmap(f).thunk(UNIT)::apply;
67+
}
68+
69+
/**
70+
* {@inheritDoc}
71+
*/
72+
@Override
73+
default <C> CheckedSupplier<T, C> flatMap(Function<? super A, ? extends Monad<C, Fn1<Unit, ?>>> f) {
74+
return CheckedFn1.super.flatMap(f).thunk(UNIT)::apply;
75+
}
76+
77+
/**
78+
* {@inheritDoc}
79+
*/
80+
@Override
81+
default <C> CheckedSupplier<T, C> discardL(Applicative<C, Fn1<Unit, ?>> appB) {
82+
return CheckedFn1.super.discardL(appB).thunk(UNIT)::apply;
83+
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
@Override
89+
default <C> CheckedSupplier<T, A> discardR(Applicative<C, Fn1<Unit, ?>> appB) {
90+
return CheckedFn1.super.discardR(appB).thunk(UNIT)::apply;
91+
}
92+
93+
/**
94+
* {@inheritDoc}
95+
*/
96+
@Override
97+
default <C> CheckedSupplier<T, C> zip(Applicative<Function<? super A, ? extends C>, Fn1<Unit, ?>> appFn) {
98+
return CheckedFn1.super.zip(appFn).thunk(UNIT)::apply;
99+
}
100+
101+
/**
102+
* {@inheritDoc}
103+
*/
104+
@Override
105+
default <C> CheckedSupplier<T, C> zip(Fn2<Unit, A, C> appFn) {
106+
return CheckedFn1.super.zip(appFn).thunk(UNIT)::apply;
107+
}
108+
109+
/**
110+
* {@inheritDoc}
111+
*/
112+
@Override
113+
default <C> CheckedSupplier<T, C> diMapR(Function<? super A, ? extends C> fn) {
114+
return CheckedFn1.super.diMapR(fn).thunk(UNIT)::apply;
115+
}
116+
117+
/**
118+
* {@inheritDoc}
119+
*/
120+
@Override
121+
default CheckedSupplier<T, Tuple2<Unit, A>> carry() {
122+
return CheckedFn1.super.carry().thunk(UNIT)::apply;
123+
}
124+
125+
/**
126+
* {@inheritDoc}
127+
*/
128+
@Override
129+
default <C> CheckedSupplier<T, C> andThen(Function<? super A, ? extends C> after) {
130+
return CheckedFn1.super.andThen(after).thunk(UNIT)::apply;
131+
}
132+
44133
/**
45134
* Convenience static factory method for constructing a {@link CheckedSupplier} without an explicit cast or type
46135
* attribution at the call site.

0 commit comments

Comments
 (0)