Skip to content

Commit 5c3e7c5

Browse files
committed
Simplify eachRight() and findLast() methods.
1 parent 5612387 commit 5c3e7c5

File tree

1 file changed

+6
-18
lines changed
  • src/main/java/com/github/underscore

1 file changed

+6
-18
lines changed

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,7 @@ public void each(final Block<? super T> func) {
171171
}
172172

173173
public static <T> void eachRight(final Iterable<T> iterable, final Block<? super T> func) {
174-
for (T element : reverse(iterable)) {
175-
func.apply(element);
176-
}
174+
each(reverse(iterable), func);
177175
}
178176

179177
public void eachRight(final Block<? super T> func) {
@@ -258,17 +256,10 @@ public static <E> Optional<E> detect(final Iterable<E> iterable, final Predicate
258256
}
259257

260258
public static <E> Optional<E> findLast(final Iterable<E> iterable, final Predicate<E> pred) {
261-
final List<E> list = newArrayList(iterable);
262-
for (int index = list.size() - 1; index >= 0; index--) {
263-
if (pred.apply(list.get(index))) {
264-
return Optional.of(list.get(index));
265-
}
266-
}
267-
return Optional.absent();
259+
return find(reverse(iterable), pred);
268260
}
269261

270-
public static <E> List<E> filter(final List<E> list,
271-
final Predicate<E> pred) {
262+
public static <E> List<E> filter(final List<E> list, final Predicate<E> pred) {
272263
final List<E> filtered = newArrayList();
273264
for (E element : list) {
274265
if (pred.apply(element)) {
@@ -278,8 +269,7 @@ public static <E> List<E> filter(final List<E> list,
278269
return filtered;
279270
}
280271

281-
public static <E> Set<E> filter(final Set<E> set,
282-
final Predicate<E> pred) {
272+
public static <E> Set<E> filter(final Set<E> set, final Predicate<E> pred) {
283273
final Set<E> filtered = newLinkedHashSet();
284274
for (E element : set) {
285275
if (pred.apply(element)) {
@@ -289,13 +279,11 @@ public static <E> Set<E> filter(final Set<E> set,
289279
return filtered;
290280
}
291281

292-
public static <E> List<E> select(final List<E> list,
293-
final Predicate<E> pred) {
282+
public static <E> List<E> select(final List<E> list, final Predicate<E> pred) {
294283
return filter(list, pred);
295284
}
296285

297-
public static <E> Set<E> select(final Set<E> set,
298-
final Predicate<E> pred) {
286+
public static <E> Set<E> select(final Set<E> set, final Predicate<E> pred) {
299287
return filter(set, pred);
300288
}
301289

0 commit comments

Comments
 (0)