Skip to content

Commit 7a43f05

Browse files
authored
Add $.filterIndexed(), $.rejectIndexed(), chain filterIndexed and chain rejectIndexed methods.
1 parent 4c5d599 commit 7a43f05

File tree

10 files changed

+127
-1
lines changed

10 files changed

+127
-1
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.github.underscore.Function3;
2929
import com.github.underscore.FunctionAccum;
3030
import com.github.underscore.Predicate;
31+
import com.github.underscore.PredicateIndexed;
3132
import com.github.underscore.Tuple;
3233
import com.github.underscore.Optional;
3334
import java.util.*;
@@ -164,6 +165,14 @@ public Chain<T> filter(final Predicate<T> pred) {
164165
return new Chain<T>($.filter(value(), pred));
165166
}
166167

168+
public Chain<T> filterIndexed(final PredicateIndexed<T> pred) {
169+
return new Chain<T>($.filterIndexed(value(), pred));
170+
}
171+
172+
public Chain<T> rejectIndexed(final PredicateIndexed<T> pred) {
173+
return new Chain<T>($.rejectIndexed(value(), pred));
174+
}
175+
167176
public Chain<T> reject(final Predicate<T> pred) {
168177
return new Chain<T>($.reject(value(), pred));
169178
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github.underscore.Function1;
2828
import com.github.underscore.FunctionAccum;
2929
import com.github.underscore.Predicate;
30+
import com.github.underscore.PredicateIndexed;
3031
import com.github.underscore.Tuple;
3132
import java.util.*;
3233
import org.junit.Test;
@@ -605,8 +606,12 @@ public void chain() {
605606
public Integer apply(Integer value) { return value; } });
606607
$.chain(new String[] {""}).filter(new Predicate<String>() {
607608
public Boolean apply(String str) { return true; } });
609+
$.chain(new String[] {""}).filterIndexed(new PredicateIndexed<String>() {
610+
public boolean apply(int index, String str) { return true; } });
608611
$.chain(new String[] {""}).reject(new Predicate<String>() {
609612
public Boolean apply(String str) { return true; } });
613+
$.chain(new String[] {""}).rejectIndexed(new PredicateIndexed<String>() {
614+
public boolean apply(int index, String str) { return true; } });
610615
$.chain(new String[] {""}).reduce(new FunctionAccum<String, String>() {
611616
public String apply(String accum, String str) { return null; } }, "");
612617
$.chain(new String[] {""}).reduceRight(new FunctionAccum<String, String>() {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.github.underscore.Function1;
2828
import com.github.underscore.FunctionAccum;
2929
import com.github.underscore.Predicate;
30+
import com.github.underscore.PredicateIndexed;
3031
import com.github.underscore.Tuple;
3132
import com.github.underscore.Optional;
3233
import java.util.*;
@@ -118,6 +119,14 @@ public Chain<T> filter(final Predicate<T> pred) {
118119
return new Chain<T>($.filter(value(), pred));
119120
}
120121

122+
public Chain<T> filterIndexed(final PredicateIndexed<T> pred) {
123+
return new Chain<T>($.filterIndexed(value(), pred));
124+
}
125+
126+
public Chain<T> rejectIndexed(final PredicateIndexed<T> pred) {
127+
return new Chain<T>($.rejectIndexed(value(), pred));
128+
}
129+
121130
public Chain<T> reject(final Predicate<T> pred) {
122131
return new Chain<T>($.reject(value(), pred));
123132
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* The MIT License (MIT)
33
*
4-
* Copyright 2015, 2016 Valentyn Kolesnikov
4+
* Copyright 2015-2017 Valentyn Kolesnikov
55
*
66
* Permission is hereby granted, free of charge, to any person obtaining a copy
77
* of this software and associated documentation files (the "Software"), to deal
@@ -27,6 +27,7 @@
2727
import com.github.underscore.Function1;
2828
import com.github.underscore.FunctionAccum;
2929
import com.github.underscore.Predicate;
30+
import com.github.underscore.PredicateIndexed;
3031
import com.github.underscore.Tuple;
3132
import java.math.BigDecimal;
3233
import java.math.BigInteger;
@@ -264,8 +265,12 @@ public void chain() {
264265
public Integer apply(Integer value) { return value; } });
265266
$.chain(new String[] {""}).filter(new Predicate<String>() {
266267
public Boolean apply(String str) { return true; } });
268+
$.chain(new String[] {""}).filterIndexed(new PredicateIndexed<String>() {
269+
public boolean apply(int index, String str) { return true; } });
267270
$.chain(new String[] {""}).reject(new Predicate<String>() {
268271
public Boolean apply(String str) { return true; } });
272+
$.chain(new String[] {""}).rejectIndexed(new PredicateIndexed<String>() {
273+
public boolean apply(int index, String str) { return true; } });
269274
$.chain(new String[] {""}).reduce(new FunctionAccum<String, String>() {
270275
public String apply(String accum, String str) { return null; } }, "");
271276
$.chain(new String[] {""}).reduceRight(new FunctionAccum<String, String>() {

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<option>-keep public class com.github.underscore.MemoizeFunction1 { *; }</option>
151151
<option>-keep public class com.github.underscore.Optional { *; }</option>
152152
<option>-keep public class com.github.underscore.Predicate { *; }</option>
153+
<option>-keep public class com.github.underscore.PredicateIndexed { *; }</option>
153154
<option>-keep public class com.github.underscore.Template { *; }</option>
154155
<option>-keep public class com.github.underscore.Tuple { *; }</option>
155156
<option>-keep public class com.github.underscore.UnaryOperator { *; }</option>

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,18 @@ public static <E> List<E> filter(final List<E> list, final Predicate<E> pred) {
316316
return filtered;
317317
}
318318

319+
public static <E> List<E> filterIndexed(final List<E> list, final PredicateIndexed<E> pred) {
320+
final List<E> filtered = newArrayList();
321+
int index = 0;
322+
for (E element : list) {
323+
if (pred.apply(index, element)) {
324+
filtered.add(element);
325+
}
326+
index += 1;
327+
}
328+
return filtered;
329+
}
330+
319331
public static <E> Set<E> filter(final Set<E> set, final Predicate<E> pred) {
320332
final Set<E> filtered = newLinkedHashSet();
321333
for (E element : set) {
@@ -343,6 +355,15 @@ public Boolean apply(E input) {
343355
});
344356
}
345357

358+
public static <E> List<E> rejectIndexed(final List<E> list, final PredicateIndexed<E> pred) {
359+
return filterIndexed(list, new PredicateIndexed<E>() {
360+
@Override
361+
public boolean apply(int index, E input) {
362+
return !pred.apply(index, input);
363+
}
364+
});
365+
}
366+
346367
public static <E> Set<E> reject(final Set<E> set, final Predicate<E> pred) {
347368
return filter(set, new Predicate<E>() {
348369
@Override
@@ -2009,10 +2030,18 @@ public Chain<T> filter(final Predicate<T> pred) {
20092030
return new Chain<T>($.filter(list, pred));
20102031
}
20112032

2033+
public Chain<T> filterIndexed(final PredicateIndexed<T> pred) {
2034+
return new Chain<T>($.filterIndexed(list, pred));
2035+
}
2036+
20122037
public Chain<T> reject(final Predicate<T> pred) {
20132038
return new Chain<T>($.reject(list, pred));
20142039
}
20152040

2041+
public Chain<T> rejectIndexed(final PredicateIndexed<T> pred) {
2042+
return new Chain<T>($.rejectIndexed(list, pred));
2043+
}
2044+
20162045
public <F> Chain<F> reduce(final FunctionAccum<F, T> func, final F zeroElem) {
20172046
return new Chain<F>($.reduce(list, func, zeroElem));
20182047
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.underscore;
2+
3+
public interface PredicateIndexed<T> {
4+
boolean apply(int index, T arg);
5+
6+
@Override
7+
boolean equals(Object object);
8+
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,29 @@ public Boolean apply(Integer item) {
505505
assertEquals("[2, 4, 6]", result.toString());
506506
}
507507

508+
/*
509+
var evens = _.filterIndexed([1, 2, 3, 4, 5, 6], function(index, num){ return index !== 1 && num % 2 == 0; });
510+
=> [4, 6]
511+
*/
512+
@Test
513+
public void filterIndexed() {
514+
final List<Integer> result = $.filterIndexed(asList(1, 2, 3, 4, 5, 6),
515+
new PredicateIndexed<Integer>() {
516+
public boolean apply(int index, Integer item) {
517+
return index != 1 && item % 2 == 0;
518+
}
519+
});
520+
assertEquals("[4, 6]", result.toString());
521+
final List<Integer> resultChain = $.chain(asList(1, 2, 3, 4, 5, 6))
522+
.filterIndexed(
523+
new PredicateIndexed<Integer>() {
524+
public boolean apply(int index, Integer item) {
525+
return index != 1 && item % 2 == 0;
526+
}
527+
}).value();
528+
assertEquals("[4, 6]", resultChain.toString());
529+
}
530+
508531
/*
509532
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
510533
=> [2, 4, 6]
@@ -551,6 +574,29 @@ public Boolean apply(Integer item) {
551574
assertEquals("[1, 3, 5]", resultSet.toString());
552575
}
553576

577+
/*
578+
var evens = _.rejectIndexed([1, 2, 3, 4, 5, 6], function(index, num){ return index !== 1 && num % 2 == 0; });
579+
=> [1, 2, 3, 5]
580+
*/
581+
@Test
582+
public void rejectIndexed() {
583+
final List<Integer> result = $.rejectIndexed(asList(1, 2, 3, 4, 5, 6),
584+
new PredicateIndexed<Integer>() {
585+
public boolean apply(int index, Integer item) {
586+
return index != 1 && item % 2 == 0;
587+
}
588+
});
589+
assertEquals("[1, 2, 3, 5]", result.toString());
590+
final List<Integer> resultChain = $.chain(asList(1, 2, 3, 4, 5, 6))
591+
.rejectIndexed(
592+
new PredicateIndexed<Integer>() {
593+
public boolean apply(int index, Integer item) {
594+
return index != 1 && item % 2 == 0;
595+
}
596+
}).value();
597+
assertEquals("[1, 2, 3, 5]", resultChain.toString());
598+
}
599+
554600
/*
555601
_.every([1, 2, 3, 4], function(num) { return num % 2 === 0; }); // false
556602
_.every([1, 2, 3, 4], function(num) { return num < 5; }); // true

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.github.underscore.Function3;
2929
import com.github.underscore.FunctionAccum;
3030
import com.github.underscore.Predicate;
31+
import com.github.underscore.PredicateIndexed;
3132
import com.github.underscore.Tuple;
3233
import com.github.underscore.Optional;
3334
import java.util.*;
@@ -155,6 +156,14 @@ public Chain<T> filter(final Predicate<T> pred) {
155156
return new Chain<T>($.filter(value(), pred));
156157
}
157158

159+
public Chain<T> filterIndexed(final PredicateIndexed<T> pred) {
160+
return new Chain<T>($.filterIndexed(value(), pred));
161+
}
162+
163+
public Chain<T> rejectIndexed(final PredicateIndexed<T> pred) {
164+
return new Chain<T>($.rejectIndexed(value(), pred));
165+
}
166+
158167
public Chain<T> reject(final Predicate<T> pred) {
159168
return new Chain<T>($.reject(value(), pred));
160169
}

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
@@ -27,6 +27,7 @@
2727
import com.github.underscore.Function1;
2828
import com.github.underscore.FunctionAccum;
2929
import com.github.underscore.Predicate;
30+
import com.github.underscore.PredicateIndexed;
3031
import com.github.underscore.Tuple;
3132
import com.github.underscore.string.$.JsonStringBuilder;
3233
import com.github.underscore.string.$.XmlStringBuilder;
@@ -1979,8 +1980,12 @@ public void chain() {
19791980
public Integer apply(Integer value) { return value; } });
19801981
$.chain(new String[] {""}).filter(new Predicate<String>() {
19811982
public Boolean apply(String str) { return true; } });
1983+
$.chain(new String[] {""}).filterIndexed(new PredicateIndexed<String>() {
1984+
public boolean apply(int index, String str) { return true; } });
19821985
$.chain(new String[] {""}).reject(new Predicate<String>() {
19831986
public Boolean apply(String str) { return true; } });
1987+
$.chain(new String[] {""}).rejectIndexed(new PredicateIndexed<String>() {
1988+
public boolean apply(int index, String str) { return true; } });
19841989
$.chain(new String[] {""}).reduce(new FunctionAccum<String, String>() {
19851990
public String apply(String accum, String str) { return null; } }, "");
19861991
$.chain(new String[] {""}).reduceRight(new FunctionAccum<String, String>() {

0 commit comments

Comments
 (0)