forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP2.java
More file actions
346 lines (311 loc) · 9.51 KB
/
P2.java
File metadata and controls
346 lines (311 loc) · 9.51 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
package fj;
import static fj.Function.*;
import fj.data.List;
import fj.data.Stream;
/**
* A product-2.
*
* @version %build.number%
*/
public abstract class P2<A, B> {
/**
* Access the first element of the product.
*
* @return The first element of the product.
*/
public abstract A _1();
/**
* Access the second element of the product.
*
* @return The second element of the product.
*/
public abstract B _2();
/**
* Swaps the elements around in this product.
*
* @return A new product-2 with the elements swapped.
*/
public final P2<B, A> swap() {
return new P2<B, A>() {
public B _1() {
return P2.this._2();
}
public A _2() {
return P2.this._1();
}
};
}
/**
* Map the first element of the product.
*
* @param f The function to map with.
* @return A product with the given function applied.
*/
public final <X> P2<X, B> map1(final F<A, X> f) {
return new P2<X, B>() {
public X _1() {
return f.f(P2.this._1());
}
public B _2() {
return P2.this._2();
}
};
}
/**
* Map the second element of the product.
*
* @param f The function to map with.
* @return A product with the given function applied.
*/
public final <X> P2<A, X> map2(final F<B, X> f) {
return new P2<A, X>() {
public A _1() {
return P2.this._1();
}
public X _2() {
return f.f(P2.this._2());
}
};
}
/**
* Split this product between two argument functions and combine their output.
*
* @param f A function that will map the first element of this product.
* @param g A function that will map the second element of this product.
* @return A new product with the first function applied to the second element
* and the second function applied to the second element.
*/
public final <C, D> P2<C, D> split(final F<A, C> f, final F<B, D> g) {
final F<P2<A, D>, P2<C, D>> ff = map1_(f);
final F<P2<A, B>, P2<A, D>> gg = map2_(g);
return compose(ff, gg).f(this);
}
/**
* Duplicates this product on the first element, and maps the given function across the duplicate (Comonad pattern).
*
* @param k A function to map over the duplicated product.
* @return A new product with the result of the given function applied to this product as the first element,
* and with the second element intact.
*/
public final <C> P2<C, B> cobind(final F<P2<A, B>, C> k) {
return new P2<C, B>() {
public C _1() {
return k.f(P2.this);
}
public B _2() {
return P2.this._2();
}
};
}
/**
* Duplicates this product into the first element (Comonad pattern).
*
* @return A new product with this product in its first element and with the second element intact.
*/
public final P2<P2<A, B>, B> duplicate() {
final F<P2<A, B>, P2<A, B>> id = identity();
return cobind(id);
}
/**
* Replaces the first element of this product with the given value.
*
* @param c The value with which to replace the first element of this product.
* @return A new product with the first element replaced with the given value.
*/
public final <C> P2<C, B> inject(final C c) {
final F<P2<A, B>, C> co = constant(c);
return cobind(co);
}
/**
* Applies a list of comonadic functions to this product, returning a list of values.
*
* @param fs A list of functions to apply to this product.
* @return A list of the results of applying the given list of functions to this product.
*/
public final <C> List<C> sequenceW(final List<F<P2<A, B>, C>> fs) {
List.Buffer<C> cs = List.Buffer.empty();
for (final F<P2<A, B>, C> f : fs)
cs = cs.snoc(f.f(this));
return cs.toList();
}
/**
* Applies a stream of comonadic functions to this product, returning a stream of values.
*
* @param fs A stream of functions to apply to this product.
* @return A stream of the results of applying the given stream of functions to this product.
*/
public final <C> Stream<C> sequenceW(final Stream<F<P2<A, B>, C>> fs) {
return fs.isEmpty()
? Stream.<C>nil()
: Stream.cons(fs.head().f(this), new P1<Stream<C>>() {
public Stream<C> _1() {
return sequenceW(fs.tail()._1());
}
});
}
/**
* Returns the 1-product projection over the first element.
*
* @return the 1-product projection over the first element.
*/
public final P1<A> _1_() {
return P2.<A, B>__1().lazy().f(this);
}
/**
* Returns the 1-product projection over the second element.
*
* @return the 1-product projection over the second element.
*/
public final P1<B> _2_() {
return P2.<A, B>__2().lazy().f(this);
}
/**
* Provides a memoising P2 that remembers its values.
*
* @return A P2 that calls this P2 once for any given element and remembers the value for subsequent calls.
*/
public final P2<A, B> memo() {
return new P2<A, B>() {
private final P1<A> a = _1_().memo();
private final P1<B> b = _2_().memo();
public A _1() {
return a._1();
}
public B _2() {
return b._1();
}
};
}
/**
* A first-class version of the split function.
*
* @param f A function that will map the first element of the given product.
* @param g A function that will map the second element of the given product.
* @return A function that splits a given product between the two given functions and combines their output.
*/
public static <A, B, C, D> F<P2<A, B>, P2<C, D>> split_(final F<A, C> f, final F<B, D> g) {
return new F<P2<A, B>, P2<C, D>>() {
public P2<C, D> f(final P2<A, B> p) {
return p.split(f, g);
}
};
}
/**
* Promotes a function so that it maps the first element of a product.
*
* @param f The function to promote.
* @return The given function, promoted to map the first element of products.
*/
public static <A, B, X> F<P2<A, B>, P2<X, B>> map1_(final F<A, X> f) {
return new F<P2<A, B>, P2<X, B>>() {
public P2<X, B> f(final P2<A, B> p) {
return p.map1(f);
}
};
}
/**
* Promotes a function so that it maps the second element of a product.
*
* @param f The function to promote.
* @return The given function, promoted to map the second element of products.
*/
public static <A, B, X> F<P2<A, B>, P2<A, X>> map2_(final F<B, X> f) {
return new F<P2<A, B>, P2<A, X>>() {
public P2<A, X> f(final P2<A, B> p) {
return p.map2(f);
}
};
}
/**
* Sends the given input value to both argument functions and combines their output.
*
* @param f A function to receive an input value.
* @param g A function to receive an input value.
* @param b An input value to send to both functions.
* @return The product of the two functions applied to the input value.
*/
public static <B, C, D> P2<C, D> fanout(final F<B, C> f, final F<B, D> g, final B b) {
return join(P.<B, B>p2()).f(b).split(f, g);
}
/**
* Maps the given function across both the elements of the given product.
*
* @param f A function to map over a product.
* @param p A product over which to map.
* @return A new product with the given function applied to both elements.
*/
public static <A, B> P2<B, B> map(final F<A, B> f, final P2<A, A> p) {
return p.split(f, f);
}
/**
* Returns a curried form of {@link #swap()}.
*
* @return A curried form of {@link #swap()}.
*/
public static <A, B> F<P2<A, B>, P2<B, A>> swap_() {
return new F<P2<A, B>, P2<B, A>>() {
public P2<B, A> f(final P2<A, B> p) {
return p.swap();
}
};
}
/**
* 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, B> F<P2<A, B>, A> __1() {
return new F<P2<A, B>, A>() {
public A f(final P2<A, B> p) {
return p._1();
}
};
}
/**
* Returns a function that returns the second element of a product.
*
* @return A function that returns the second element of a product.
*/
public static <A, B> F<P2<A, B>, B> __2() {
return new F<P2<A, B>, B>() {
public B f(final P2<A, B> p) {
return p._2();
}
};
}
/**
* Transforms a curried function of arity-2 to a function of a product-2
*
* @param f a curried function of arity-2 to transform into a function of a product-2
* @return The function, transformed to operate on on a product-2
*/
public static <A, B, C> F<P2<A, B>, C> tuple(final F<A, F<B, C>> f) {
return new F<P2<A, B>, C>() {
public C f(final P2<A, B> p) {
return f.f(p._1()).f(p._2());
}
};
}
/**
* Transforms an uncurried function of arity-2 to a function of a product-2
*
* @param f an uncurried function of arity-2 to transform into a function of a product-2
* @return The function, transformed to operate on on a product-2
*/
public static <A, B, C> F<P2<A, B>, C> tuple(final F2<A, B, C> f) {
return tuple(curry(f));
}
/**
* Transforms a function of a product-2 to an uncurried function or arity-2.
*
* @param f A function of a product-2 to transform into an uncurried function.
* @return The function, transformed to an uncurried function of arity-2.
*/
public static <A, B, C> F2<A, B, C> untuple(final F<P2<A, B>, C> f) {
return new F2<A, B, C>() {
public C f(final A a, final B b) {
return f.f(P.p(a, b));
}
};
}
}