Skip to content

Commit 3490ab1

Browse files
committed
Adding Effect, Fn1<A, Unit>
1 parent 18ab96a commit 3490ab1

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2020
- `Absent`, a monoid over `Maybe` that is absence biased
2121
- `RateLimit`, a function that iterates elements from an `Iterable` according to some rate limit
2222
- `Try#withResources`, `Try`'s expression analog to Java 7's try-with-resources statement
23-
- `Occurrences`, for counting the occurrences of the members of an `Iterable`
23+
- `Occurrences`, for counting the occurrences of the members of an `Iterable`
24+
- `Effect`, an `Fn0` returning `UNIT`
25+
- `Noop`, a no-op `Effect`
2426

2527
### Changed
2628
- `Tuple2-8` now implement `Product2-8`
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.jnape.palatable.lambda.functions;
2+
3+
import com.jnape.palatable.lambda.adt.Unit;
4+
import com.jnape.palatable.lambda.functor.Applicative;
5+
6+
import java.util.function.Consumer;
7+
import java.util.function.Function;
8+
import java.util.function.Supplier;
9+
10+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
11+
12+
/**
13+
* A function taking "no arguments", implemented as an <code>{@link Fn1}&lt;{@link Unit}, A&gt;</code>.
14+
*
15+
* @param <A> the result type
16+
* @see Fn1
17+
* @see Supplier
18+
*/
19+
@FunctionalInterface
20+
public interface Effect<A> extends Fn1<A, Unit>, Consumer<A> {
21+
22+
@Override
23+
default void accept(A a) {
24+
apply(a);
25+
}
26+
27+
@Override
28+
default <Z> Effect<Z> diMapL(Function<? super Z, ? extends A> fn) {
29+
return Fn1.super.diMapL(fn)::apply;
30+
}
31+
32+
@Override
33+
default <Z> Effect<Z> contraMap(Function<? super Z, ? extends A> fn) {
34+
return Fn1.super.contraMap(fn)::apply;
35+
}
36+
37+
@Override
38+
default <Z> Effect<Z> compose(Function<? super Z, ? extends A> before) {
39+
return Fn1.super.compose(before)::apply;
40+
}
41+
42+
@Override
43+
default <C> Effect<A> discardR(Applicative<C, Fn1<A, ?>> appB) {
44+
return Fn1.super.discardR(appB)::apply;
45+
}
46+
47+
@Override
48+
default Effect<A> andThen(Consumer<? super A> after) {
49+
return a -> {
50+
Consumer.super.andThen(after).accept(a);
51+
return UNIT;
52+
};
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.jnape.palatable.lambda.functions.specialized;
2+
3+
import com.jnape.palatable.lambda.adt.Unit;
4+
import com.jnape.palatable.lambda.functions.Effect;
5+
6+
import static com.jnape.palatable.lambda.adt.Unit.UNIT;
7+
8+
/**
9+
* As the name might suggest, this is an {@link Effect} that, *ahem*, has no effect.
10+
*
11+
* @param <A> the argument type
12+
*/
13+
public final class Noop<A> implements Effect<A> {
14+
private static final Noop INSTANCE = new Noop();
15+
16+
private Noop() {
17+
}
18+
19+
@Override
20+
public Unit apply(A a) {
21+
return UNIT;
22+
}
23+
24+
/**
25+
* Static factory method that returns the singleton {@link Noop} instance.
26+
*
27+
* @param <A> the argument type
28+
* @return the singleton {@link Noop} instance
29+
*/
30+
@SuppressWarnings("unchecked")
31+
public static <A> Noop<A> noop() {
32+
return INSTANCE;
33+
}
34+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.jnape.palatable.lambda.functions;
2+
3+
import org.junit.Test;
4+
5+
import java.util.function.Consumer;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
8+
import static com.jnape.palatable.lambda.functions.specialized.Noop.noop;
9+
10+
public class EffectTest {
11+
12+
@Test
13+
@SuppressWarnings("unused")
14+
public void covariantReturns() {
15+
Effect<String> effect = noop();
16+
Effect<Object> diMapL = effect.diMapL(constantly("1"));
17+
Effect<Object> contraMap = effect.contraMap(constantly("1"));
18+
Effect<Object> compose = effect.compose(constantly("1"));
19+
Effect<String> stringEffect = effect.discardR(constantly("1"));
20+
Effect<String> andThen = effect.andThen((Consumer<? super String>) noop());
21+
}
22+
}

0 commit comments

Comments
 (0)