-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monad.java
56 lines (38 loc) · 1.38 KB
/
Monad.java
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
50
51
52
53
54
55
56
import java.util.function.Function;
import javax.lang.model.type.NullType;
public interface Monad <T, E> {
public Monad<T, NullType> return_();
public Monad<NullType, E> bind(Function<T, Monad<NullType, E>> f);
public Monad<NullType, E> sequencer(Monad<NullType, E> b);
public T extractLeft();
public E extractRight();
public class Kek<T, E> implements Monad<T,E> {
private T left;
private E right;
public Kek(T left, E right) {
this.left = left;
this.right = right;
}
public T extractLeft() {
return this.left;
}
public E extractRight() {
return this.right;
}
public Monad<T, NullType> return_() {
return new Kek<T, NullType>((T) this.left, null);
}
public Monad<NullType, E> bind(Function<T, Monad<NullType, E>> f) {
return f.apply((T) left);
}
public Monad<NullType, E> sequencer(Monad<NullType, E> b) {
return bind(__ -> b);
}
}
public static void main(String[] args) {
Kek<Object,Object> three = new Kek<>(3, null);
System.out.println(three.return_().extractLeft());
System.out.println(three.bind(k -> new Kek((int) k + 2, null)).extractLeft());
System.out.println(three.sequencer(new Kek(null, "kek")).extractRight());
}
}