Skip to content

Commit ed2e477

Browse files
committed
Adding static factory method to create an Effect from an Fn1
1 parent 9e2fb48 commit ed2e477

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
1515
- `IO#externallyManaged`, for supplying an `IO` with externally-managed futures
1616
- test jar is now published
1717
- `Monad#join` static alias for `flatMap(id())`
18+
- `Effect#effect` static factory method taking `Fn1`
1819

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

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import static com.jnape.palatable.lambda.functions.Fn0.fn0;
1010
import static com.jnape.palatable.lambda.functions.IO.io;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1112

1213
/**
1314
* A function returning "no result", and therefore only useful as a side-effect.
@@ -26,22 +27,22 @@ default IO<Unit> apply(A a) {
2627

2728
@Override
2829
default <Z> Effect<Z> diMapL(Function<? super Z, ? extends A> fn) {
29-
return effect(z -> Fn1.super.diMapL(fn).apply(z).unsafePerformIO());
30+
return effect(Fn1.super.diMapL(fn));
3031
}
3132

3233
@Override
3334
default <Z> Effect<Z> contraMap(Function<? super Z, ? extends A> fn) {
34-
return effect(z -> Fn1.super.contraMap(fn).apply(z).unsafePerformIO());
35+
return effect(Fn1.super.contraMap(fn));
3536
}
3637

3738
@Override
3839
default <Z> Effect<Z> compose(Function<? super Z, ? extends A> before) {
39-
return effect(z -> Fn1.super.compose(before).apply(z).unsafePerformIO());
40+
return effect(Fn1.super.compose(before));
4041
}
4142

4243
@Override
4344
default <C> Effect<A> discardR(Applicative<C, Fn1<A, ?>> appB) {
44-
return effect(a -> Fn1.super.discardR(appB).apply(a).unsafePerformIO());
45+
return effect(Fn1.super.discardR(appB));
4546
}
4647

4748
@Override
@@ -67,6 +68,17 @@ static <A> Effect<A> effect(Consumer<A> effect) {
6768
* @return the effect
6869
*/
6970
static Effect<Unit> effect(Runnable runnable) {
70-
return effect(__ -> runnable.run());
71+
return effect(constantly(io(runnable)));
72+
}
73+
74+
/**
75+
* Create an {@link Effect} from an {@link Fn1} that yields an {@link IO}.
76+
*
77+
* @param fn the function
78+
* @param <A> the effect argument type
79+
* @return the effect
80+
*/
81+
static <A> Effect<A> effect(Fn1<A, ? extends IO<?>> fn) {
82+
return a -> fn.apply(a).unsafePerformIO();
7183
}
7284
}

src/test/java/com/jnape/palatable/lambda/functions/EffectTest.java

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

3+
import com.jnape.palatable.lambda.adt.Unit;
34
import org.junit.Test;
45

56
import java.util.ArrayList;
67
import java.util.List;
8+
import java.util.concurrent.atomic.AtomicInteger;
79

10+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
11+
import static com.jnape.palatable.lambda.functions.Effect.effect;
812
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
913
import static java.util.Arrays.asList;
1014
import static org.junit.Assert.assertEquals;
@@ -31,4 +35,17 @@ public void covariantReturns() {
3135

3236
assertEquals(asList("1", "2", "3", "4", "5", "6", "6"), results);
3337
}
38+
39+
@Test
40+
public void staticFactoryMethods() {
41+
AtomicInteger counter = new AtomicInteger();
42+
43+
Effect<Unit> runnableEffect = effect(counter::incrementAndGet);
44+
runnableEffect.apply(UNIT).unsafePerformIO();
45+
assertEquals(1, counter.get());
46+
47+
Effect<AtomicInteger> fnEffect = effect(AtomicInteger::incrementAndGet);
48+
fnEffect.apply(counter).unsafePerformIO();
49+
assertEquals(2, counter.get());
50+
}
3451
}

0 commit comments

Comments
 (0)