Skip to content

Commit 8bc163a

Browse files
committed
Add support for the remove().
1 parent e4dd58f commit 8bc163a

File tree

2 files changed

+67
-6
lines changed
  • lodash-plugin/src

2 files changed

+67
-6
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,30 @@ public Chain<List<T>> dropRightWhile(final Predicate<T> pred) {
7272
return new Chain<List<T>>($.dropRightWhile(value(), pred));
7373
}
7474

75-
public Chain<List<Object>> fill(Object value) {
75+
public Chain<List<Object>> fill(final Object value) {
7676
return new Chain<List<Object>>($.fill((List<Object>) value(), value));
7777
}
7878

79-
public Chain<List<Object>> fill(Object value, Integer start, Integer end) {
79+
public Chain<List<Object>> fill(final Object value, final Integer start, final Integer end) {
8080
return new Chain<List<Object>>($.fill((List<Object>) value(), value, start, end));
8181
}
8282

8383
public Chain<List<?>> flattenDeep() {
8484
return new Chain<List<?>>($.flattenDeep((List<?>) value()));
8585
}
8686

87-
public Chain<List<Object>> pull(Object ... values) {
87+
public Chain<List<Object>> pull(final Object ... values) {
8888
return new Chain<List<Object>>($.pull((List<Object>) value(), values));
8989
}
9090

91-
public Chain<List<Object>> pullAt(Integer ... indexes) {
91+
public Chain<List<Object>> pullAt(final Integer ... indexes) {
9292
return new Chain<List<Object>>($.pullAt((List<Object>) value(), indexes));
9393
}
9494

95+
public Chain<List<T>> remove(final Predicate<T> pred) {
96+
return new Chain<List<T>>($.remove(value(), pred));
97+
}
98+
9599
public Chain<List<T>> xor(final List<T> list) {
96100
return new Chain<List<T>>($.xor(value(), list));
97101
}
@@ -221,7 +225,7 @@ public List<Object> pull(Object ... values) {
221225
return pull((List<Object>) getIterable(), values);
222226
}
223227

224-
public static List<Object> pullAt(final List<Object> list, Integer ... indexes) {
228+
public static List<Object> pullAt(final List<Object> list, final Integer ... indexes) {
225229
final List<Object> result = newArrayList();
226230
final List<Integer> indexesList = Arrays.asList(indexes);
227231
int index = 0;
@@ -236,10 +240,26 @@ public static List<Object> pullAt(final List<Object> list, Integer ... indexes)
236240
return result;
237241
}
238242

239-
public List<Object> pullAt(Integer ... indexes) {
243+
public List<Object> pullAt(final Integer ... indexes) {
240244
return pullAt((List<Object>) getIterable(), indexes);
241245
}
242246

247+
public static <T> List<T> remove(final List<T> list, final Predicate<T> pred) {
248+
final List<T> result = newArrayList();
249+
for (final Iterator<T> iterator = list.iterator(); iterator.hasNext(); ) {
250+
final T object = iterator.next();
251+
if (pred.apply(object)) {
252+
result.add(object);
253+
iterator.remove();
254+
}
255+
}
256+
return result;
257+
}
258+
259+
public List<T> remove(final Predicate<T> pred) {
260+
return remove((List<T>) getIterable(), pred);
261+
}
262+
243263
public static <T> List<T> xor(final List<T> ... lists) {
244264
int index = -1;
245265
int length = lists.length;

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,46 @@ public void pullAt() {
247247
assertEquals("[10, 20]", events.toString());
248248
}
249249

250+
/*
251+
var array = [1, 2, 3, 4];
252+
var evens = _.remove(array, function(n) {
253+
return n % 2 == 0;
254+
});
255+
256+
console.log(array);
257+
// → [1, 3]
258+
259+
console.log(evens);
260+
// → [2, 4]
261+
*/
262+
@Test
263+
public void remove() {
264+
List<Integer> array = new ArrayList<Integer>(asList(1, 2, 3, 4));
265+
List<Integer> evens = $.remove(array, new Predicate<Integer>() {
266+
public Boolean apply(final Integer n) {
267+
return n % 2 == 0;
268+
}
269+
});
270+
assertEquals("[1, 3]", array.toString());
271+
assertEquals("[2, 4]", evens.toString());
272+
array = new ArrayList<Integer>(asList(1, 2, 3, 4));
273+
evens = new $(array).remove(new Predicate<Integer>() {
274+
public Boolean apply(final Integer n) {
275+
return n % 2 == 0;
276+
}
277+
});
278+
assertEquals("[1, 3]", array.toString());
279+
assertEquals("[2, 4]", evens.toString());
280+
array = new ArrayList<Integer>(asList(1, 2, 3, 4));
281+
evens = $.chain(array).remove(new Predicate<Integer>() {
282+
public Boolean apply(final Integer n) {
283+
return n % 2 == 0;
284+
}
285+
}).value();
286+
assertEquals("[1, 3]", array.toString());
287+
assertEquals("[2, 4]", evens.toString());
288+
}
289+
250290
/*
251291
_.xor([1, 2], [4, 2]);
252292
// → [1, 4]
@@ -258,6 +298,7 @@ public void xor() {
258298
assertEquals("[1, 4]", $.chain(asList(1, 2)).xor(asList(4, 2)).toString());
259299
}
260300

301+
261302
@Test
262303
public void main() {
263304
$.main(new String[] {});

0 commit comments

Comments
 (0)