forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.java
More file actions
118 lines (94 loc) · 2.43 KB
/
State.java
File metadata and controls
118 lines (94 loc) · 2.43 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package fj.data;
import fj.*;
import static fj.P.p;
/**
* Created by MarkPerry on 7/07/2014.
*/
public final class State<S, A> {
private final F<S, P2<S, A>> run;
private State(F<S, P2<S, A>> f) {
run = f;
}
public P2<S, A> run(S s) {
return run.f(s);
}
public static <S, A> State<S, A> unit(F<S, P2<S, A>> f) {
return new State<>(f);
}
public static <S> State<S, S> units(F<S, S> f) {
return unit((S s) -> {
S s2 = f.f(s);
return p(s2, s2);
});
}
public static <S, A> State<S, A> constant(A a) {
return unit(s -> p(s, a));
}
public <B> State<S, B> map(F<A, B> f) {
return unit((S s) -> {
P2<S, A> p2 = run(s);
B b = f.f(p2._2());
return p(p2._1(), b);
});
}
public static <S> State<S, Unit> modify(F<S, S> f) {
return State.<S>init().flatMap(s -> unit(s2 -> p(f.f(s), Unit.unit())));
}
public <B> State<S, B> mapState(F<P2<S, A>, P2<S, B>> f) {
return unit(s -> f.f(run(s)));
}
public static <S, B, C> State<S, C> flatMap(State<S, B> mb, F<B, State<S, C>> f) {
return mb.flatMap(f);
}
public <B> State<S, B> flatMap(F<A, State<S, B>> f) {
return unit((S s) -> {
P2<S, A> p = run(s);
A a = p._2();
S s2 = p._1();
State<S, B> smb = f.f(a);
return smb.run(s2);
});
}
public static <S> State<S, S> init() {
return unit(s -> p(s, s));
}
public State<S, S> gets() {
return unit(s -> {
P2<S, A> p = run(s);
S s2 = p._1();
return p(s2, s2);
});
}
public static <S> State<S, Unit> put(S s) {
return unit((S z) -> p(s, Unit.unit()));
}
public A eval(S s) {
return run(s)._2();
}
public S exec(S s) {
return run(s)._1();
}
public State<S, A> withs(F<S, S> f) {
return unit(F1Functions.andThen(f, run));
}
public static <S, A> State<S, A> gets(F<S, A> f) {
return State.<S>init().map(f);
}
/**
* Evaluate each action in the sequence from left to right, and collect the results.
*/
public static <S, A> State<S, List<A>> sequence(List<State<S, A>> list) {
return list.foldLeft((State<S, List<A>> acc, State<S, A> ma) ->
acc.flatMap((List<A> xs) -> ma.map(xs::snoc)
), constant(List.nil()));
}
/**
* Map each element of a structure to an action, evaluate these actions from left to right
* and collect the results.
*/
public static <S, A, B> State<S, List<B>> traverse(List<A> list, F<A, State<S, B>> f) {
return list.foldLeft((State<S, List<B>> acc, A a) ->
acc.flatMap(bs -> f.f(a).map(bs::snoc)
), constant(List.nil()));
}
}