forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1.java
More file actions
372 lines (320 loc) · 10.5 KB
/
P1.java
File metadata and controls
372 lines (320 loc) · 10.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package fj;
import fj.data.Array;
import fj.data.Either;
import fj.data.List;
import fj.data.Option;
import fj.data.Stream;
import fj.data.Validation;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import static fj.P.p;
import static fj.Unit.unit;
//import fj.data.*;
public abstract class P1<A> implements F0<A> {
@Override
public final A f() {
return _1();
}
/**
* Access the first element of the product.
*
* @return The first element of the product.
*/
public abstract A _1();
/**
* Returns a function that returns the first element of a product.
*
* @return A function that returns the first element of a product.
*/
public static <A> F<P1<A>, A> __1() {
return P1::_1;
}
/**
* Promote any function to a transformation between P1s.
*
* @deprecated As of release 4.5, use {@link #map_}
* @param f A function to promote to a transformation between P1s.
* @return A function promoted to operate on P1s.
*/
public static <A, B> F<P1<A>, P1<B>> fmap(final F<A, B> f) {
return map_(f);
}
/**
* Promote any function to a transformation between P1s.
*
* @param f A function to promote to a transformation between P1s.
* @return A function promoted to operate on P1s.
*/
public static <A, B> F<P1<A>, P1<B>> map_(final F<A, B> f) {
return a -> a.map(f);
}
/**
* Binds the given function to the value in a product-1 with a final join.
*
* @param f A function to apply to the value in a product-1.
* @return The result of applying the given function to the value of given product-1.
*/
public final <B> P1<B> bind(final F<A, P1<B>> f) {
P1<A> self = this;
return P.lazy(() -> f.f(self._1())._1());
}
/**
* Promotes the given function so that it returns its value in a P1.
*
* @param f A function to have its result wrapped in a P1.
* @return A function whose result is wrapped in a P1.
*/
public static <A, B> F<A, P1<B>> curry(final F<A, B> f) {
return a -> P.lazy(() -> f.f(a));
}
/**
* Performs function application within a P1 (applicative functor pattern).
*
* @param cf The P1 function to apply.
* @return A new P1 after applying the given P1 function to the first argument.
*/
public final <B> P1<B> apply(final P1<F<A, B>> cf) {
P1<A> self = this;
return cf.bind(f -> map_(f).f(self));
}
/**
* Binds the given function to the values in the given P1s with a final join.
*
* @param cb A given P1 to bind the given function with.
* @param f The function to apply to the values in the given P1s.
* @return A new P1 after performing the map, then final join.
*/
public final <B, C> P1<C> bind(final P1<B> cb, final F<A, F<B, C>> f) {
return cb.apply(map_(f).f(this));
}
/**
* Binds the given function to the values in the given P1s with a final join.
*/
public final <B, C> P1<C> bind(final P1<B> cb, final F2<A, B, C> f) {
return bind(cb, F2W.lift(f).curry());
}
/**
* Joins a P1 of a P1 with a bind operation.
*
* @param a The P1 of a P1 to join.
* @return A new P1 that is the join of the given P1.
*/
public static <A> P1<A> join(final P1<P1<A>> a) {
return a.bind(Function.identity());
}
/**
* Promotes a function of arity-2 to a function on P1s.
*
* @param f The function to promote.
* @return A function of arity-2 promoted to map over P1s.
*/
public static <A, B, C> F<P1<A>, F<P1<B>, P1<C>>> liftM2(final F<A, F<B, C>> f) {
return Function.curry((pa, pb) -> pa.bind(pb, f));
}
public final <B, C> P1<C> liftM2(P1<B> pb, F2<A, B, C> f) {
return P.lazy(() -> f.f(_1(), pb._1()));
}
/**
* Turns a List of P1s into a single P1 of a List.
*
* @param as The list of P1s to transform.
* @return A single P1 for the given List.
*/
public static <A> P1<List<A>> sequence(final List<P1<A>> as) {
return as.foldRight(liftM2(List.cons()), p(List.nil()));
}
/**
* A first-class version of the sequence method for lists of P1s.
*
* @return A function from a List of P1s to a single P1 of a List.
*/
public static <A> F<List<P1<A>>, P1<List<A>>> sequenceList() {
return P1::sequence;
}
/**
* Turns a stream of P1s into a single P1 of a stream.
*
* @param as The stream of P1s to transform.
* @return A single P1 for the given stream.
*/
public static <A> P1<Stream<A>> sequence(final Stream<P1<A>> as) {
return as.foldRight(liftM2(Stream.cons()), p(Stream.nil()));
}
/**
* Turns an optional P1 into a lazy option.
*/
public static <A> P1<Option<A>> sequence(final Option<P1<A>> o) {
return P.lazy(() -> o.map(P1::_1));
}
/**
* Turns an array of P1s into a single P1 of an array.
*
* @param as The array of P1s to transform.
* @return A single P1 for the given array.
*/
public static <A> P1<Array<A>> sequence(final Array<P1<A>> as) {
return P.lazy(() -> as.map(P1.__1()));
}
/**
* Traversable instance of P1 for List
*
* @param f The function that takes A and produces a List<B> (non-deterministic result)
* @return A List of P1<B>
*/
public final <B> List<P1<B>> traverseList(final F<A, List<B>> f){
return f.f(_1()).map(P::p);
}
/**
* Traversable instance of P1 for Either
*
* @param f The function produces Either
* @return An Either of P1<B>
*/
public final <B, X> Either<X, P1<B>> traverseEither(final F<A, Either<X, B>> f){
return f.f(_1()).right().map(P::p);
}
/**
* Traversable instance of P1 for Option
*
* @param f The function that produces Option
* @return An Option of P1<B>
*/
public final <B> Option<P1<B>> traverseOption(final F<A, Option<B>> f){
return f.f(_1()).map(P::p);
}
/**
* Traversable instance of P1 for Validation
*
* @param f The function might produces Validation
* @return An Validation of P1<B>
*/
public final <B, E> Validation<E, P1<B>> traverseValidation(final F<A, Validation<E, B>> f){
return f.f(_1()).map(P::p);
}
/**
* Traversable instance of P1 for Stream
*
* @param f The function that produces Stream
* @return An Stream of P1<B>
*/
public final <B> Stream<P1<B>> traverseStream(final F<A, Stream<B>> f){
return f.f(_1()).map(P::p);
}
/**
* Map the element of the product.
*
* @param f The function to map with.
* @return A product with the given function applied.
*/
public final <B> P1<B> map(final F<A, B> f) {
final P1<A> self = this;
return P.lazy(() -> f.f(self._1()));
}
/**
* @deprecated since 4.7. Use {@link P1#weakMemo()} instead.
*/
@Deprecated
public final P1<A> memo() {
return weakMemo();
}
/**
* Returns a P1 that remembers its value.
*
* @return A P1 that calls this P1 once and remembers the value for subsequent calls.
*/
public P1<A> hardMemo() { return new Memo<>(this); }
/**
* Like <code>memo</code>, but the memoized value is wrapped into a <code>WeakReference</code>
*/
public P1<A> weakMemo() { return new WeakReferenceMemo<>(this); }
/**
* Like <code>memo</code>, but the memoized value is wrapped into a <code>SoftReference</code>
*/
public P1<A> softMemo() { return new SoftReferenceMemo<>(this); }
/**
* @deprecated since 4.7. Use {@link P#weakMemo(F0)} instead.
*/
@Deprecated
public static <A> P1<A> memo(F<Unit, A> f) {
return P.weakMemo(() -> f.f(unit()));
}
/**
* @deprecated since 4.7. Use {@link P#weakMemo(F0)} instead.
*/
@Deprecated
public static <A> P1<A> memo(F0<A> f) {
return P.weakMemo(f);
}
static final class Memo<A> extends P1<A> {
private volatile F0<A> fa;
private A value;
Memo(F0<A> fa) { this.fa = fa; }
@Override public final A _1() {
return (fa == null) ? value : computeValue();
}
private synchronized A computeValue() {
F0<A> fa = this.fa;
if (fa != null) {
value = fa.f();
this.fa = null;
}
return value;
}
@Override public P1<A> hardMemo() { return this; }
@Override public P1<A> softMemo() { return this; }
@Override public P1<A> weakMemo() { return this; }
}
abstract static class ReferenceMemo<A> extends P1<A> {
private final F0<A> fa;
private volatile Reference<P1<A>> v = null;
ReferenceMemo(final F0<A> fa) { this.fa = fa; }
@Override public final A _1() {
Reference<P1<A>> v = this.v;
P1<A> p1 = v != null ? v.get() : null;
return p1 != null ? p1._1() : computeValue();
}
private synchronized A computeValue() {
Reference<P1<A>> v = this.v;
P1<A> p1 = v != null ? v.get() : null;
if (p1 == null) {
A a = fa.f();
this.v = newReference(p(a));
return a;
}
return p1._1();
}
abstract <B> Reference<B> newReference(B ref);
}
static final class WeakReferenceMemo<A> extends ReferenceMemo<A> {
WeakReferenceMemo(F0<A> fa) { super(fa); }
@Override
<B> Reference<B> newReference(final B ref) { return new WeakReference<>(ref); }
@Override public P1<A> weakMemo() { return this; }
}
static final class SoftReferenceMemo<A> extends ReferenceMemo<A> {
SoftReferenceMemo(F0<A> self) { super(self); }
@Override
<B> Reference<B> newReference(final B ref) { return new SoftReference<>(ref); }
@Override public P1<A> softMemo() { return this; }
@Override public P1<A> weakMemo() { return this; }
}
/**
* Returns a constant function that always uses this value.
*
* @return A constant function that always uses this value.
*/
public final <B> F<B, A> constant() { return Function.constant(_1()); }
@Override
public final String toString() {
return Show.p1Show(Show.<A>anyShow()).showS(this);
}
@Override
public final boolean equals(Object other) {
return Equal.equals0(P1.class, this, other, () -> Equal.p1Equal(Equal.anyEqual()));
}
@Override
public final int hashCode() {
return Hash.p1Hash(Hash.<A>anyHash()).hash(this);
}
}