forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResult.java
More file actions
163 lines (145 loc) · 3.86 KB
/
Result.java
File metadata and controls
163 lines (145 loc) · 3.86 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package fj.parser;
import fj.F;
import fj.F2;
import static fj.Function.curry;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* A parse result made up of a value (A) and the remainder of the parse input (I).
*
* @version %build.number%
*/
public final class Result<I, A> implements Iterable<A> {
private final I i;
private final A a;
private Result(final I i, final A a) {
this.i = i;
this.a = a;
}
/**
* The remainder of the parse input.
*
* @return The remainder of the parse input.
*/
public I rest() {
return i;
}
/**
* The parsed value.
*
* @return The parsed value.
*/
public A value() {
return a;
}
/**
* Maps the given function across the remainder of the parse input.
*
* @param f The function to map with.
* @return A result with a different parse input.
*/
public <J> Result<J, A> mapRest(final F<I, J> f) {
return result(f.f(i), a);
}
/**
* First-class function mapping across the remainder of the parse input.
*
* @return A first-class function mapping across the remainder of the parse input.
*/
public <J> F<F<I, J>, Result<J, A>> mapRest() {
return new F<F<I, J>, Result<J, A>>() {
public Result<J, A> f(final F<I, J> f) {
return mapRest(f);
}
};
}
/**
* Maps the given function across the parse value.
*
* @param f The function to map with.
* @return A result with a different parse value.
*/
public <B> Result<I, B> mapValue(final F<A, B> f) {
return result(i, f.f(a));
}
/**
* First-class function mapping across the parse value.
*
* @return A first-class function mapping across the parse value.
*/
public <B> F<F<A, B>, Result<I, B>> mapValue() {
return new F<F<A, B>, Result<I, B>>() {
public Result<I, B> f(final F<A, B> f) {
return mapValue(f);
}
};
}
/**
* A bifunctor map across both the remainder of the parse input and the parse value.
*
* @param f The function to map the remainder of the parse input with.
* @param g The function to map the parse value with.
* @return A result with a different parse input and parse value.
*/
public <B, J> Result<J, B> bimap(final F<I, J> f, final F<A, B> g) {
return mapRest(f).mapValue(g);
}
/**
* First-class bifunctor map.
*
* @return A first-class bifunctor map.
*/
public <B, J> F<F<I, J>, F<F<A, B>, Result<J, B>>> bimap() {
return curry(new F2<F<I, J>, F<A, B>, Result<J, B>>() {
public Result<J, B> f(final F<I, J> f, final F<A, B> g) {
return bimap(f, g);
}
});
}
/**
* Returns an iterator over the parse value. This method exists to permit the use in a <code>for</code>-each loop.
*
* @return An iterator over the parse value.
*/
public Iterator<A> iterator() {
return new Iterator<A>() {
private boolean r;
public boolean hasNext() {
return !r;
}
public A next() {
if (r)
throw new NoSuchElementException();
else {
r = true;
return a;
}
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
/**
* Construct a result with the given remainder of the parse input and parse value.
*
* @param i The remainder of the parse input.
* @param a The parse value.
* @return A result with the given remainder of the parse input and parse value.
*/
public static <A, I> Result<I, A> result(final I i, final A a) {
return new Result<I, A>(i, a);
}
/**
* First-class construction of a result.
*
* @return A first-class function for construction of a result.
*/
public static <A, I> F<I, F<A, Result<I, A>>> result() {
return curry(new F2<I, A, Result<I, A>>() {
public Result<I, A> f(final I i, final A a) {
return result(i, a);
}
});
}
}