This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
/
EnumerableArrays.java
executable file
·598 lines (519 loc) · 20.1 KB
/
EnumerableArrays.java
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package org.enumerable.lambda.enumerable;
import static java.util.Arrays.*;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
import org.enumerable.lambda.Fn0;
import org.enumerable.lambda.Fn1;
import org.enumerable.lambda.Fn2;
import org.enumerable.lambda.enumerable.collection.EList;
import org.enumerable.lambda.enumerable.collection.EMap;
import org.enumerable.lambda.enumerable.collection.ESet;
/**
* Ruby/Smalltalk style internal iterators for Java 5 using bytecode
* transformation to capture expressions as closures.
*
* <p>
* <a href="http://ruby-doc.org/core/classes/Enumerable.html"/>Ruby's Enumerable
* module in 1.8.6</a>
* </p>
* <p>
* <a href="http://ruby-doc.org/ruby-1.9/classes/Enumerable.html"/>Ruby's
* Enumerable module in 1.9</a>
* </p>
* <p>
* This class adapts the methods in {@link Enumerable} to work on arrays.
*/
public class EnumerableArrays {
/**
* Passes each element of the array to the given block. The method returns
* true if the block never returns false or null.
*/
public static <E> boolean all(E[] array, Fn1<? super E, ?> block) {
return Enumerable.all(asList(array), block);
}
/**
* Passes each element of the array to the an implicit block of
* {@link Fn1#identity()}. The method returns true if the block never
* returns false or null.
*/
public static <E> boolean all(E[] array) {
return Enumerable.all(asList(array));
}
/**
* Passes each element of the array to the given block. The method returns
* true if the block ever returns a value other than false or null.
*/
public static <E> boolean any(E[] array, Fn1<? super E, ?> block) {
return Enumerable.any(asList(array), block);
}
/**
* Passes each element of the array to the an implicit block of
* {@link Fn1#identity()}. The method returns true if the block ever returns
* a value other than false or null.
*/
public static <E> boolean any(E[] array) {
return Enumerable.any(asList(array));
}
/**
* Returns a new list with the results of running block once for every
* element in array.
*/
public static <E, R> Object[] collect(E[] array, Fn1<? super E, R> block) {
return Enumerable.collect(asList(array), block).toArray();
}
/**
* Returns a new list with the results of running block once for every
* element in array. Takes an extra type parameter to handle empty arrays.
*/
@SuppressWarnings("unchecked")
public static <E, R> R[] collect(E[] array, Fn1<? super E, R> block, Class<R> type) {
return Enumerable.collect(asList(array), block).toArray((R[]) Array.newInstance(type, 0));
}
/**
* Returns the count of all elements in array.
*/
public static <E> int count(E[] array) {
return array.length;
}
/**
* Returns the count of objects in arry that equal obj.
*/
public static <E> int count(E[] array, E obj) {
return Enumerable.count(asList(array), obj);
}
/**
* Returns the count of objects in array for which the block returns a true
* value.
*/
public static <E> int count(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.count(asList(array), block);
}
/**
* Returns null if array has no elements; otherwise, passes the elements,
* one at a time to the block. When it reaches the end, it repeats. The
* number of times it repeats is set by the parameter.
*/
public static <E, R> Object cycle(E[] array, int times, Fn1<? super E, R> block) {
return Enumerable.cycle(asList(array), times, block);
}
/**
* Passes each entry in array to block. Returns the first for which block is
* not false. If no object matches, it returns null.
*/
public static <E> E detect(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.detect(asList(array), block);
}
/**
* Passes each entry in array to block. Returns the first for which block is
* not false. If no object matches, it returns ifNone.
*/
public static <E> E detect(E[] array, Fn0<E> ifNone, Fn1<? super E, Boolean> block) {
return Enumerable.detect(asList(array), ifNone, block);
}
/**
* Calls block for each item in array.
*/
public static <E, R> E[] each(E[] array, Fn1<? super E, R> block) {
Enumerable.each(asList(array), block);
return array;
}
/**
* Iterates the given block for each list of consecutive n elements.
*/
public static <E, R> Object eachCons(E[] array, int n, Fn1<List<E>, R> block) {
return Enumerable.eachCons(asList(array), n, block);
}
/**
* Iterates the given block for each slice of n elements.
*/
public static <E, R> Object eachSlice(E[] array, int n, Fn1<List<E>, R> block) {
return Enumerable.eachSlice(asList(array), n, block);
}
/**
* Calls block with two arguments, the item and its index, for each item in
* array.
*/
public static <E, R> E[] eachWithIndex(E[] array, Fn2<? super E, Integer, R> block) {
Enumerable.eachWithIndex(asList(array), block);
return array;
}
/**
* @see #toList(Object...)
*/
public static <E> EList<E> entries(E... array) {
return Enumerable.toList(asList(array));
}
/**
* @see #detect(Object[], Fn1)
*/
public static <E> E find(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.find(asList(array), block);
}
/**
* @see #detect(Object[], Fn0, Fn1)
*/
public static <E> E find(E[] array, Fn0<E> ifNone, Fn1<? super E, Boolean> block) {
return Enumerable.find(asList(array), ifNone, block);
}
/**
* @see #select(Object[], Fn1)
*/
public static <E> E[] findAll(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.findAll(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Returns an array of every element in array for which pattern matches.
*/
public static <E> E[] grep(E[] array, Pattern pattern) {
return Enumerable.grep(asList(array), pattern).toArray(newEmptyArray(array));
}
/**
* Returns an array of every element in array for which pattern matches.
* Each matching element is passed to tje block, and its result is stored in
* the output list.
*/
public static <E, R> Object[] grep(E[] array, Pattern pattern, Fn1<? super E, R> block) {
return Enumerable.grep(asList(array), pattern, block).toArray();
}
/**
* @see #grep(Object[], Pattern)
*/
public static <E> E[] grep(E[] array, String pattern) {
return Enumerable.grep(asList(array), pattern).toArray(newEmptyArray(array));
}
/**
* @see #grep(Object[], Pattern, Fn1)
*/
public static <E, R> Object[] grep(E[] array, String pattern, Fn1<? super E, R> block) {
return Enumerable.grep(asList(array), pattern, block).toArray();
}
/**
* Partitions array by calling the block for each item and using the result
* returned by the block to group the items into buckets. Returns a map
* where the keys are the objects returned by the block, and the values for
* a key are those items for which the block returned that object.
*/
public static <E, R> EMap<R, EList<E>> groupBy(E[] array, Fn1<? super E, R> block) {
return Enumerable.groupBy(asList(array), block);
}
/**
* Named parameter for detect.
*
* @see #detect(Object[], Fn0, Fn1)
* @see #find(Object[], Fn0, Fn1)
*/
public static <R> Fn0<R> ifNone(R defaultValue) {
return Enumerable.ifNone(defaultValue);
}
/**
* @see #member(Object[], Object)
*/
public static <E> boolean include(E[] array, Object obj) {
return Enumerable.include(asList(array), obj);
}
/**
* Combines the elements of array by applying the block to an accumulator
* value (memo) and each element in turn. At each step, memo is set to the
* value returned by the block. This form uses the first element of the
* array as a the initial value (and skips that element while iterating).
*/
public static <E> E inject(E[] array, Fn2<E, E, E> block) {
return Enumerable.inject(asList(array), block);
}
/**
* Combines the elements of array by applying the block to an accumulator
* value (memo) and each element in turn. At each step, memo is set to the
* value returned by the block. This form lets you supply an initial value
* for memo.
*/
public static <E, R> R inject(E[] array, R initial, Fn2<R, E, R> block) {
return Enumerable.inject(asList(array), initial, block);
}
/**
* @see #collect(Object[], Fn1)
*/
public static <E, R> Object[] map(E[] array, Fn1<? super E, R> block) {
return Enumerable.map(asList(array), block).toArray();
}
/**
* @see #collect(Object[], Fn1, Class)
*/
public static <E, R> R[] map(E[] array, Fn1<E, R> block, Class<R> type) {
return collect(array, block, type);
}
/**
* Returns the object in array with the maximum value. This form assumes all
* objects implement {@link Comparable}
*/
public static <E extends Object & Comparable<? super E>> E max(E[] array) {
return Enumerable.max(asList(array));
}
/**
* Returns the object in array with the maximum value. This form uses the
* block to {@link Comparator#compare}.
*/
public static <E> E max(E[] array, Fn2<? super E, ? super E, Integer> block) {
return Enumerable.max(asList(array), block);
}
/**
* Passes each item in the array to the block. Returns the item
* corresponding to the largest value returned by the block.
*/
public static <E, R extends Object & Comparable<? super R>> E maxBy(E[] array, Fn1<? super E, R> block) {
return Enumerable.maxBy(asList(array), block);
}
/**
* Returns true if any member of array equals obj. Equality is tested using
* {@link Object#equals(Object)}.
*/
public static <E> boolean member(E[] array, Object obj) {
return Enumerable.member(asList(array), obj);
}
/**
* Returns the object in array with the minimum value. This form assumes all
* objects implement {@link Comparable}.
*/
public static <E extends Object & Comparable<? super E>> E min(E[] array) {
return Enumerable.min(asList(array));
}
/**
* Returns the object in array with the minimum value. This form uses the
* block to {@link Comparator#compare}.
*/
public static <E> E min(E[] array, Fn2<? super E, ? super E, Integer> block) {
return Enumerable.min(asList(array), block);
}
/**
* Passes each item in the array to the block. Returns the item
* corresponding to the smallest value returned by the block.
*/
public static <E, R extends Object & Comparable<? super R>> E minBy(E[] array, Fn1<? super E, R> block) {
return Enumerable.minBy(asList(array), block);
}
/**
* Compares the elements of array using {@link Comparable}, returning the
* minimum and maximum value.
*/
public static <E extends Object & Comparable<? super E>> E[] minMax(E[] array) {
return Enumerable.minMax(asList(array)).toArray(newEmptyArray(array));
}
/**
* Compares the elements of array using the given block, returning the
* minimum and maximum value.
*/
public static <E> E[] minMax(E[] array, Fn2<? super E, ? super E, Integer> block) {
return Enumerable.minMax(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Passes each item in the array to the block. Returns the items
* corresponding to the smallest and largest values returned by the block.
*/
public static <E, R extends Object & Comparable<? super R>> E[] minMaxBy(E[] array, Fn1<? super E, R> block) {
return Enumerable.minMaxBy(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Passes each element of the array to the given block. The method returns
* true if the block never returns a value other than false or null.
*/
public static <E> boolean none(E[] array, Fn1<? super E, ?> block) {
return Enumerable.none(asList(array), block);
}
/**
* Passes each element of the array to the an implicit block of
* {@link Fn1#identity()}. The method returns true if the block never
* returns a value other than false or null.
*/
public static <E> boolean none(E[] array) {
return Enumerable.none(asList(array));
}
/**
* Passes each element of the array to the given block. The method returns
* true if the block returns true exactly one time.
*/
public static <E> boolean one(E[] array, Fn1<? super E, ?> block) {
return Enumerable.one(asList(array), block);
}
/**
* Passes each element of the array to the an implicit block of
* {@link Fn1#identity()}. The method returns true if the block returns true
* exactly one time.
*/
public static <E> boolean one(E[] array) {
return Enumerable.one(asList(array));
}
/**
* Returns two lists, the first containing the elements of array for which
* the block evaluates to true, the second containing the rest.
*/
@SuppressWarnings("unchecked")
public static <E> E[][] partition(E[] array, Fn1<? super E, Boolean> block) {
List<EList<E>> partition = Enumerable.partition(asList(array), block);
E[][] result = (E[][]) Array.newInstance(array.getClass(), 2);
result[0] = partition.get(0).toArray(newEmptyArray(array));
result[1] = partition.get(1).toArray(newEmptyArray(array));
return result;
}
/**
* Constructs a range using the given start and end. The range will include
* the end object.
*/
public static Integer[] range(int start, int end) {
return Enumerable.range(start, end).toList().toArray(new Integer[0]);
}
/**
* Constructs a range using the given start and end. If the third parameter
* is false, the range will include the end object; otherwise, it will be
* excluded.
*/
public static Integer[] range(int start, int end, boolean exclusive) {
return Enumerable.range(start, end, exclusive).toList().toArray(new Integer[0]);
}
/**
* @see #inject(Object[], Fn2)
*/
public static <E> E reduce(E[] array, Fn2<E, E, E> block) {
return Enumerable.inject(asList(array), block);
}
/**
* @see #inject(Object[], Object, Fn2)
*/
public static <E, R> R reduce(E[] array, R initial, Fn2<R, E, R> block) {
return Enumerable.inject(asList(array), initial, block);
}
/**
* Returns an array containing all elements of array for which block is
* false.
*/
public static <E> E[] reject(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.reject(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Invokes the block with the elements of array in reverse order.
*/
public static <E, R> E[] reverseEach(E[] array, Fn1<? super E, R> block) {
Enumerable.each(new ReverseArrayIterable<E>(array), block);
return array;
}
/**
* Returns an array containing all elements of array for which block is not
* false.
*/
public static <E> E[] select(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.select(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Returns an array containing the items in array sorted, according to their
* own compareTo method.
*/
public static <E extends Object & Comparable<? super E>> E[] sort(E[] array) {
return Enumerable.sort(asList(array)).toArray(newEmptyArray(array));
}
/**
* Returns an array containing the items in array sorted by using the
* results of the supplied block.
*/
public static <E> E[] sort(E[] array, Fn2<? super E, ? super E, Integer> block) {
return Enumerable.sort(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Sorts array using a set of keys generated by mapping the values in array
* through the given block.
*/
public static <E, R extends Object & Comparable<? super R>> E[] sortBy(E[] array, Fn1<? super E, R> block) {
return Enumerable.sortBy(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Returns an array containing the first n items from array.
*/
public static <E> E[] take(E[] array, int n) {
return Enumerable.take(asList(array), n).toArray(newEmptyArray(array));
}
/**
* Passes successive items to the block, adding them to the result array
* until the block returns false or null.
*/
public static <E> E[] takeWhile(E[] array, Fn1<? super E, Boolean> block) {
return Enumerable.takeWhile(asList(array), block).toArray(newEmptyArray(array));
}
/**
* Returns a list containing the items in array.
*/
public static <E> EList<E> toList(E... array) {
return Enumerable.toList(asList(array));
}
/**
* Creates a new Set containing the elements of the given array.
*/
public static <E> ESet<E> toSet(E... array) {
return Enumerable.toSet(asList(array));
}
/**
* Creates a new Set containing the elements of the given array, the
* elements are preprocessed by the given block.
*/
public static <E, R> ESet<R> toSet(E[] array, Fn1<? super E, R> block) {
return Enumerable.toSet(asList(array), block);
}
/**
* Converts any arguments to iterators, then merges elements of collection
* with corresponding elements from each argument. This generates a sequence
* of collection#size n-element list, where n is one more that the count of
* arguments. If the size of any argument is less than collection#size, null
* values are supplied. The block is invoked for each output array. Returns
* null.
*/
public static <E, R> Object zip(E[] array, Object[] args, Fn1<? super EList<?>, R> block) {
List<Iterable<?>> lists = new ArrayList<Iterable<?>>();
for (int i = 0; i < args.length; i++)
lists.add(asList(args[i]));
return Enumerable.zip(asList(array), lists, block);
}
/**
* Converts any arguments to iterators, then merges elements of array with
* corresponding elements from each argument. This generates a sequence of
* array#size n-element list, where n is one more that the count of
* arguments. If the size of any argument is less than array#length, null
* values are supplied.
*/
public static <E> Object[][] zip(E[] array, Object[]... args) {
Iterable<?>[] lists = new Iterable<?>[args.length];
for (int i = 0; i < args.length; i++)
lists[i] = asList(args[i]);
List<EList<?>> zip = Enumerable.zip(asList(array), lists);
Object[][] result = new Object[zip.size()][];
for (int i = 0; i < zip.size(); i++)
result[i] = zip.get(i).toArray();
return result;
}
@SuppressWarnings("unchecked")
static <T> T[] newEmptyArray(T[] array) {
return (T[]) Array.newInstance(array.getClass().getComponentType(), 0);
}
static class ReverseArrayIterable<T> implements Iterable<T> {
T[] array;
ReverseArrayIterable(T... elements) {
this.array = elements;
}
public Iterator<T> iterator() {
return new Iterator<T>() {
int i = array.length - 1;
public boolean hasNext() {
return i >= 0;
}
public T next() {
if (i < 0)
throw new NoSuchElementException();
return array[i--];
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
}
}