forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffect.java
More file actions
49 lines (43 loc) · 923 Bytes
/
Effect.java
File metadata and controls
49 lines (43 loc) · 923 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package fj;
import static fj.Unit.unit;
/**
* Represents a side-effect.
*
* @version %build.number%
*/
public abstract class Effect<A> {
public abstract void e(A a);
/**
* Returns a function for the given effect.
*
* @return The function using the given effect.
*/
public final F<A, Unit> e() {
return new F<A, Unit>() {
public Unit f(final A a) {
e(a);
return unit();
}
};
}
/**
* A contra-variant functor on effect.
*
* @param f The function to map over the effect.
* @return An effect after a contra-variant map.
*/
public final <B> Effect<B> comap(final F<B, A> f) {
return new Effect<B>() {
public void e(final B b) {
Effect.this.e(f.f(b));
}
};
}
public static <A> Effect<A> f(final F<A, Unit> f) {
return new Effect<A>() {
public void e(final A a) {
f.f(a);
}
};
}
}