Skip to content

Commit 2b9f230

Browse files
authored
Add support for $reduce(iterable, function) and $.reduceRigth(iterable, function).
1 parent 405143d commit 2b9f230

File tree

10 files changed

+129
-0
lines changed

10 files changed

+129
-0
lines changed

math-plugin/src/main/java/com/github/underscore/math/$.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.math;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -143,10 +144,18 @@ public <F> Chain<F> reduce(final BiFunction<F, T, F> func, final F zeroElem) {
143144
return new Chain<F>($.reduce(value(), func, zeroElem));
144145
}
145146

147+
public Chain<Optional<T>> reduce(final BinaryOperator<T> func) {
148+
return new Chain<Optional<T>>($.reduce(value(), func));
149+
}
150+
146151
public <F> Chain<F> reduceRight(final BiFunction<F, T, F> func, final F zeroElem) {
147152
return new Chain<F>($.reduceRight(value(), func, zeroElem));
148153
}
149154

155+
public Chain<Optional<T>> reduceRight(final BinaryOperator<T> func) {
156+
return new Chain<Optional<T>>($.reduceRight(value(), func));
157+
}
158+
150159
public Chain<Optional<T>> find(final Predicate<T> pred) {
151160
return new Chain<Optional<T>>($.find(value(), pred));
152161
}

math-plugin/src/test/java/com/github/underscore/math/MathTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.math;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -308,8 +309,12 @@ public void chain() {
308309
public boolean test(String str) { return true; } });
309310
$.chain(new String[] {""}).reduce(new BiFunction<String, String, String>() {
310311
public String apply(String accum, String str) { return null; } }, "");
312+
$.chain(new String[] {""}).reduce(new BinaryOperator<String>() {
313+
public String apply(String accum, String str) { return null; } });
311314
$.chain(new String[] {""}).reduceRight(new BiFunction<String, String, String>() {
312315
public String apply(String accum, String str) { return null; } }, "");
316+
$.chain(new String[] {""}).reduceRight(new BinaryOperator<String>() {
317+
public String apply(String accum, String str) { return null; } });
313318
$.chain(new String[] {""}).find(new Predicate<String>() {
314319
public boolean test(String str) { return true; } });
315320
$.chain(new String[] {""}).findLast(new Predicate<String>() {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
<option>-keepattributes Signature,InnerClasses</option>
141141
<option>-keep public class com.github.underscore.$ { *; }</option>
142142
<option>-keep public class com.github.underscore.*$Chain { *; }</option>
143+
<option>-keep public class com.github.underscore.BinaryOperator { *; }</option>
143144
<option>-keep public class com.github.underscore.BiConsumer { *; }</option>
144145
<option>-keep public class com.github.underscore.BiFunction { *; }</option>
145146
<option>-keep public class com.github.underscore.Consumer { *; }</option>

src/main/java/com/github/underscore/$.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ public static <T, E> E reduce(final Iterable<T> iterable, final BiFunction<E, T,
325325
return accum;
326326
}
327327

328+
public static <T> Optional<T> reduce(final Iterable<T> iterable, final BinaryOperator<T> func) {
329+
boolean foundAny = false;
330+
T accum = null;
331+
for (T element : iterable) {
332+
if (!foundAny) {
333+
foundAny = true;
334+
accum = element;
335+
} else {
336+
accum = func.apply(accum, element);
337+
}
338+
}
339+
return foundAny ? Optional.of(accum) : Optional.<T>absent();
340+
}
341+
328342
public static <E> E reduce(final int[] array, final BiFunction<E, ? super Integer, E> func, final E zeroElem) {
329343
E accum = zeroElem;
330344
for (int element : array) {
@@ -353,6 +367,10 @@ public static <T, E> E reduceRight(final Iterable<T> iterable, final BiFunction<
353367
return reduce(reverse(iterable), func, zeroElem);
354368
}
355369

370+
public static <T> Optional<T> reduceRight(final Iterable<T> iterable, final BinaryOperator<T> func) {
371+
return reduce(reverse(iterable), func);
372+
}
373+
356374
public static <E> E reduceRight(final int[] array, final BiFunction<E, ? super Integer, E> func, final E zeroElem) {
357375
E accum = zeroElem;
358376
for (Integer element : reverse(array)) {
@@ -2165,10 +2183,18 @@ public <F> Chain<F> reduce(final BiFunction<F, T, F> func, final F zeroElem) {
21652183
return new Chain<F>($.reduce(list, func, zeroElem));
21662184
}
21672185

2186+
public Chain<Optional<T>> reduce(final BinaryOperator<T> func) {
2187+
return new Chain<Optional<T>>($.reduce(list, func));
2188+
}
2189+
21682190
public <F> Chain<F> reduceRight(final BiFunction<F, T, F> func, final F zeroElem) {
21692191
return new Chain<F>($.reduceRight(list, func, zeroElem));
21702192
}
21712193

2194+
public Chain<Optional<T>> reduceRight(final BinaryOperator<T> func) {
2195+
return new Chain<Optional<T>>($.reduceRight(list, func));
2196+
}
2197+
21722198
public Chain<Optional<T>> find(final Predicate<T> pred) {
21732199
return new Chain<Optional<T>>($.find(list, pred));
21742200
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.github.underscore;
2+
3+
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
4+
}

src/main/java/com/github/underscore/lodash/$.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.lodash;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -189,10 +190,18 @@ public <F> Chain<F> reduce(final BiFunction<F, T, F> func, final F zeroElem) {
189190
return new Chain<F>($.reduce(value(), func, zeroElem));
190191
}
191192

193+
public Chain<Optional<T>> reduce(final BinaryOperator<T> func) {
194+
return new Chain<Optional<T>>($.reduce(value(), func));
195+
}
196+
192197
public <F> Chain<F> reduceRight(final BiFunction<F, T, F> func, final F zeroElem) {
193198
return new Chain<F>($.reduceRight(value(), func, zeroElem));
194199
}
195200

201+
public Chain<Optional<T>> reduceRight(final BinaryOperator<T> func) {
202+
return new Chain<Optional<T>>($.reduceRight(value(), func));
203+
}
204+
196205
public Chain<Optional<T>> find(final Predicate<T> pred) {
197206
return new Chain<Optional<T>>($.find(value(), pred));
198207
}

src/test/java/com/github/underscore/CollectionsTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,62 @@ public Integer apply(Integer item1, Integer item2) {
307307
assertEquals("6", result.toString());
308308
}
309309

310+
/*
311+
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; });
312+
=> 6
313+
*/
314+
@Test
315+
public void reduceWithoutInit() {
316+
final Integer result =
317+
$.reduce(asList(1, 2, 3),
318+
new BinaryOperator<Integer>() {
319+
public Integer apply(Integer item1, Integer item2) {
320+
return item1 + item2;
321+
}
322+
}).get();
323+
assertEquals("6", result.toString());
324+
final Integer resultChain =
325+
$.chain(asList(1, 2, 3))
326+
.reduce(
327+
new BinaryOperator<Integer>() {
328+
public Integer apply(Integer item1, Integer item2) {
329+
return item1 + item2;
330+
}
331+
}).item().get();
332+
assertEquals("6", resultChain.toString());
333+
$.reduce(new ArrayList<Integer>(),
334+
new BinaryOperator<Integer>() {
335+
public Integer apply(Integer item1, Integer item2) {
336+
return item1 + item2;
337+
}
338+
});
339+
}
340+
341+
/*
342+
var sum = _.reduceRight([1, 2, 3], function(memo, num){ return memo + num; });
343+
=> 6
344+
*/
345+
@Test
346+
public void reduceRightWithoutInit() {
347+
final Integer result =
348+
$.reduceRight(asList(1, 2, 3),
349+
new BinaryOperator<Integer>() {
350+
public Integer apply(Integer item1, Integer item2) {
351+
return item1 + item2;
352+
}
353+
}).get();
354+
assertEquals("6", result.toString());
355+
final Integer resultChain =
356+
$.chain(asList(1, 2, 3))
357+
.reduceRight(
358+
new BinaryOperator<Integer>() {
359+
public Integer apply(Integer item1, Integer item2) {
360+
return item1 + item2;
361+
}
362+
}).item().get();
363+
assertEquals("6", resultChain.toString());
364+
}
365+
310366
/*
311367
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
312368
=> 6

src/test/java/com/github/underscore/lodash/LodashTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.lodash;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -617,8 +618,12 @@ public void chain() {
617618
public boolean test(String str) { return true; } });
618619
$.chain(new String[] {""}).reduce(new BiFunction<String, String, String>() {
619620
public String apply(String accum, String str) { return null; } }, "");
621+
$.chain(new String[] {""}).reduce(new BinaryOperator<String>() {
622+
public String apply(String accum, String str) { return null; } });
620623
$.chain(new String[] {""}).reduceRight(new BiFunction<String, String, String>() {
621624
public String apply(String accum, String str) { return null; } }, "");
625+
$.chain(new String[] {""}).reduceRight(new BinaryOperator<String>() {
626+
public String apply(String accum, String str) { return null; } });
622627
$.chain(new String[] {""}).find(new Predicate<String>() {
623628
public boolean test(String str) { return true; } });
624629
$.chain(new String[] {""}).findLast(new Predicate<String>() {

string-plugin/src/main/java/com/github/underscore/string/$.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.string;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -180,10 +181,18 @@ public <F> Chain<F> reduce(final BiFunction<F, T, F> func, final F zeroElem) {
180181
return new Chain<F>($.reduce(value(), func, zeroElem));
181182
}
182183

184+
public Chain<Optional<T>> reduce(final BinaryOperator<T> func) {
185+
return new Chain<Optional<T>>($.reduce(value(), func));
186+
}
187+
183188
public <F> Chain<F> reduceRight(final BiFunction<F, T, F> func, final F zeroElem) {
184189
return new Chain<F>($.reduceRight(value(), func, zeroElem));
185190
}
186191

192+
public Chain<Optional<T>> reduceRight(final BinaryOperator<T> func) {
193+
return new Chain<Optional<T>>($.reduceRight(value(), func));
194+
}
195+
187196
public Chain<Optional<T>> find(final Predicate<T> pred) {
188197
return new Chain<Optional<T>>($.find(value(), pred));
189198
}

string-plugin/src/test/java/com/github/underscore/string/StringTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
*/
2424
package com.github.underscore.string;
2525

26+
import com.github.underscore.BinaryOperator;
2627
import com.github.underscore.BiFunction;
2728
import com.github.underscore.Consumer;
2829
import com.github.underscore.Function;
@@ -2006,8 +2007,12 @@ public void chain() {
20062007
public boolean test(String str) { return true; } });
20072008
$.chain(new String[] {""}).reduce(new BiFunction<String, String, String>() {
20082009
public String apply(String accum, String str) { return null; } }, "");
2010+
$.chain(new String[] {""}).reduce(new BinaryOperator<String>() {
2011+
public String apply(String accum, String str) { return null; } });
20092012
$.chain(new String[] {""}).reduceRight(new BiFunction<String, String, String>() {
20102013
public String apply(String accum, String str) { return null; } }, "");
2014+
$.chain(new String[] {""}).reduceRight(new BinaryOperator<String>() {
2015+
public String apply(String accum, String str) { return null; } });
20112016
$.chain(new String[] {""}).find(new Predicate<String>() {
20122017
public boolean test(String str) { return true; } });
20132018
$.chain(new String[] {""}).findLast(new Predicate<String>() {

0 commit comments

Comments
 (0)