forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonoid.java
More file actions
387 lines (340 loc) · 10.5 KB
/
Monoid.java
File metadata and controls
387 lines (340 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package fj;
import static fj.Function.curry;
import static fj.Function.compose;
import static fj.Function.flip;
import fj.data.Array;
import fj.data.List;
import fj.data.Natural;
import fj.data.Option;
import fj.data.Set;
import fj.data.Stream;
import static fj.data.Stream.iterableStream;
import java.math.BigInteger;
import java.math.BigDecimal;
/**
* A monoid abstraction to be defined across types of the given type argument. Implementations must
* follow the monoidal laws:
* <ul>
* <li><em>Left Identity</em>; forall x. sum(zero(), x) == x</li>
* <li><em>Right Identity</em>; forall x. sum(x, zero()) == x</li>
* <li><em>Associativity</em>; forall x. forall y. forall z. sum(sum(x, y), z) == sum(x, sum(y, z))</li>
* </ul>
*
* @version %build.number%
*/
public final class Monoid<A> {
private final F<A, F<A, A>> sum;
private final A zero;
private Monoid(final F<A, F<A, A>> sum, final A zero) {
this.sum = sum;
this.zero = zero;
}
/**
* Returns a semigroup projection of this monoid.
*
* @return A semigroup projection of this monoid.
*/
public Semigroup<A> semigroup() {
return Semigroup.semigroup(sum);
}
/**
* Sums the two given arguments.
*
* @param a1 A value to sum with another.
* @param a2 A value to sum with another.
* @return The of the two given arguments.
*/
public A sum(final A a1, final A a2) {
return sum.f(a1).f(a2);
}
/**
* Returns a function that sums the given value according to this monoid.
*
* @param a1 The value to sum.
* @return A function that sums the given value according to this monoid.
*/
public F<A, A> sum(final A a1) {
return sum.f(a1);
}
/**
* Returns a function that sums according to this monoid.
*
* @return A function that sums according to this monoid.
*/
public F<A, F<A, A>> sum() {
return sum;
}
/**
* The zero value for this monoid.
*
* @return The zero value for this monoid.
*/
public A zero() {
return zero;
}
/**
* Sums the given values with right-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumRight(final List<A> as) {
return as.foldRight(sum, zero);
}
/**
* Sums the given values with right-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumRight(final Stream<A> as) {
return as.foldRight(new F2<A, P1<A>, A>() {
public A f(final A a, final P1<A> ap1) {
return sum(a, ap1._1());
}
}, zero);
}
/**
* Sums the given values with left-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumLeft(final List<A> as) {
return as.foldLeft(sum, zero);
}
/**
* Sums the given values with left-fold.
*
* @param as The values to sum.
* @return The sum of the given values.
*/
public A sumLeft(final Stream<A> as) {
return as.foldLeft(sum, zero);
}
/**
* Returns a function that sums the given values with left-fold.
*
* @return a function that sums the given values with left-fold.
*/
public F<List<A>, A> sumLeft() {
return new F<List<A>, A>() {
public A f(final List<A> as) {
return sumLeft(as);
}
};
}
/**
* Returns a function that sums the given values with right-fold.
*
* @return a function that sums the given values with right-fold.
*/
public F<List<A>, A> sumRight() {
return new F<List<A>, A>() {
public A f(final List<A> as) {
return sumRight(as);
}
};
}
/**
* Returns a function that sums the given values with left-fold.
*
* @return a function that sums the given values with left-fold.
*/
public F<Stream<A>, A> sumLeftS() {
return new F<Stream<A>, A>() {
public A f(final Stream<A> as) {
return sumLeft(as);
}
};
}
/**
* Intersperses the given value between each two elements of the iterable, and sums the result.
*
* @param as An iterable of values to sum.
* @param a The value to intersperse between values of the given iterable.
* @return The sum of the given values and the interspersed value.
*/
public A join(final Iterable<A> as, final A a) {
final Stream<A> s = iterableStream(as);
return s.isEmpty() ?
zero :
s.foldLeft1(compose(sum, flip(sum).f(a)));
}
/**
* Constructs a monoid from the given sum function and zero value, which must follow the monoidal
* laws.
*
* @param sum The sum function for the monoid.
* @param zero The zero for the monoid.
* @return A monoid instance that uses the given sun function and zero value.
*/
public static <A> Monoid<A> monoid(final F<A, F<A, A>> sum, final A zero) {
return new Monoid<A>(sum, zero);
}
/**
* Constructs a monoid from the given sum function and zero value, which must follow the monoidal
* laws.
*
* @param sum The sum function for the monoid.
* @param zero The zero for the monoid.
* @return A monoid instance that uses the given sun function and zero value.
*/
public static <A> Monoid<A> monoid(final F2<A, A, A> sum, final A zero) {
return new Monoid<A>(curry(sum), zero);
}
/**
* Constructs a monoid from the given semigroup and zero value, which must follow the monoidal laws.
*
* @param s The semigroup for the monoid.
* @param zero The zero for the monoid.
* @return A monoid instance that uses the given sun function and zero value.
*/
public static <A> Monoid<A> monoid(final Semigroup<A> s, final A zero) {
return new Monoid<A>(s.sum(), zero);
}
/**
* A monoid that adds integers.
*/
public static final Monoid<Integer> intAdditionMonoid = monoid(Semigroup.intAdditionSemigroup, 0);
/**
* A monoid that multiplies integers.
*/
public static final Monoid<Integer> intMultiplicationMonoid = monoid(Semigroup.intMultiplicationSemigroup, 1);
/**
* A monoid that adds doubles.
*/
public static final Monoid<Double> doubleAdditionMonoid = monoid(Semigroup.doubleAdditionSemigroup, 0.0);
/**
* A monoid that multiplies doubles.
*/
public static final Monoid<Double> doubleMultiplicationMonoid = monoid(Semigroup.doubleMultiplicationSemigroup, 1.0);
/**
* A monoid that adds big integers.
*/
public static final Monoid<BigInteger> bigintAdditionMonoid = monoid(Semigroup.bigintAdditionSemigroup, BigInteger.ZERO);
/**
* A monoid that multiplies big integers.
*/
public static final Monoid<BigInteger> bigintMultiplicationMonoid =
monoid(Semigroup.bigintMultiplicationSemigroup, BigInteger.ONE);
/**
* A monoid that adds big decimals.
*/
public static final Monoid<BigDecimal> bigdecimalAdditionMonoid =
monoid(Semigroup.bigdecimalAdditionSemigroup, BigDecimal.ZERO);
/**
* A monoid that multiplies big decimals.
*/
public static final Monoid<BigDecimal> bigdecimalMultiplicationMonoid =
monoid(Semigroup.bigdecimalMultiplicationSemigroup, BigDecimal.ONE);
/**
* A monoid that adds natural numbers.
*/
public static final Monoid<Natural> naturalAdditionMonoid =
monoid(Semigroup.naturalAdditionSemigroup, Natural.ZERO);
/**
* A monoid that multiplies natural numbers.
*/
public static final Monoid<Natural> naturalMultiplicationMonoid =
monoid(Semigroup.naturalMultiplicationSemigroup, Natural.ONE);
/**
* A monoid that adds longs.
*/
public static final Monoid<Long> longAdditionMonoid = monoid(Semigroup.longAdditionSemigroup, 0L);
/**
* A monoid that multiplies longs.
*/
public static final Monoid<Long> longMultiplicationMonoid = monoid(Semigroup.longMultiplicationSemigroup, 1L);
/**
* A monoid that ORs booleans.
*/
public static final Monoid<Boolean> disjunctionMonoid = monoid(Semigroup.disjunctionSemigroup, false);
/**
* A monoid that XORs booleans.
*/
public static final Monoid<Boolean> exclusiveDisjunctionMonoid = monoid(Semigroup.exclusiveDisjunctionSemiGroup, false);
/**
* A monoid that ANDs booleans.
*/
public static final Monoid<Boolean> conjunctionMonoid = monoid(Semigroup.conjunctionSemigroup, true);
/**
* A monoid that appends strings.
*/
public static final Monoid<String> stringMonoid = monoid(Semigroup.stringSemigroup, "");
/**
* A monoid that appends string buffers.
*/
public static final Monoid<StringBuffer> stringBufferMonoid = monoid(Semigroup.stringBufferSemigroup, new StringBuffer());
/**
* A monoid that appends string builders.
*/
public static final Monoid<StringBuilder> stringBuilderMonoid = monoid(Semigroup.stringBuilderSemigroup, new StringBuilder());
/**
* A monoid for functions.
*
* @param mb The monoid for the function codomain.
* @return A monoid for functions.
*/
public static <A, B> Monoid<F<A, B>> functionMonoid(final Monoid<B> mb) {
return monoid(Semigroup.<A, B>functionSemigroup(mb.semigroup()), Function.<A, B>constant(mb.zero));
}
/**
* A monoid for lists.
*
* @return A monoid for lists.
*/
public static <A> Monoid<List<A>> listMonoid() {
return monoid(Semigroup.<A>listSemigroup(), List.<A>nil());
}
/**
* A monoid for options.
*
* @return A monoid for options.
*/
public static <A> Monoid<Option<A>> optionMonoid() {
return monoid(Semigroup.<A>optionSemigroup(), Option.<A>none());
}
/**
* A monoid for options that take the first available value.
*
* @return A monoid for options that take the first available value.
*/
public static <A> Monoid<Option<A>> firstOptionMonoid() {
return monoid(Semigroup.<A>firstOptionSemigroup(), Option.<A>none());
}
/**
* A monoid for options that take the last available value.
*
* @return A monoid for options that take the last available value.
*/
public static <A> Monoid<Option<A>> lastOptionMonoid() {
return monoid(Semigroup.<A>lastOptionSemigroup(), Option.<A>none());
}
/**
* A monoid for streams.
*
* @return A monoid for streams.
*/
public static <A> Monoid<Stream<A>> streamMonoid() {
return monoid(Semigroup.<A>streamSemigroup(), Stream.<A>nil());
}
/**
* A monoid for arrays.
*
* @return A monoid for arrays.
*/
@SuppressWarnings({"unchecked"})
public static <A> Monoid<Array<A>> arrayMonoid() {
return monoid(Semigroup.<A>arraySemigroup(), Array.<A>empty());
}
/**
* A monoid for sets.
*
* @param o An order for set elements.
* @return A monoid for sets whose elements have the given order.
*/
public static <A> Monoid<Set<A>> setMonoid(final Ord<A> o) {
return monoid(Semigroup.<A>setSemigroup(), Set.empty(o));
}
}