Skip to content

Commit 7f3bca6

Browse files
committed
Add forEach and forEachRight for chain.
1 parent 448baae commit 7f3bca6

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,9 +1631,7 @@ public static <E> E[] clone(final E ... iterable) {
16311631
}
16321632

16331633
public static <T> void tap(final Iterable<T> iterable, final Block<? super T> func) {
1634-
for (T element : iterable) {
1635-
func.apply(element);
1636-
}
1634+
each(iterable, func);
16371635
}
16381636

16391637
public static <K, V> boolean isMatch(final Map<K, V> object, final Map<K, V> properties) {
@@ -1976,7 +1974,17 @@ public Chain<T> sample(final int howMany) {
19761974
}
19771975

19781976
public Chain<T> tap(final Block<T> func) {
1979-
$.tap(list, func);
1977+
$.each(list, func);
1978+
return new Chain<T>(list);
1979+
}
1980+
1981+
public Chain<T> forEach(final Block<T> func) {
1982+
$.each(list, func);
1983+
return new Chain<T>(list);
1984+
}
1985+
1986+
public Chain<T> forEachRight(final Block<T> func) {
1987+
$.eachRight(list, func);
19801988
return new Chain<T>(list);
19811989
}
19821990

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ public void apply(Integer item) {
9696
}
9797
});
9898
assertEquals("[1, 2, 3]", result.toString());
99+
final List<Map.Entry<String, Integer>> resultChain = new ArrayList<Map.Entry<String, Integer>>();
100+
$.chain((new LinkedHashMap<String, Integer>() { { put("a", 1); put("b", 2); put("c", 3); } }).entrySet())
101+
.forEach(new Block<Map.Entry<String, Integer>>() {
102+
public void apply(final Map.Entry<String, Integer> item) {
103+
resultChain.add(item);
104+
}
105+
});
106+
assertEquals("[a=1, b=2, c=3]", resultChain.toString());
99107
}
100108

101109
/*
@@ -119,6 +127,14 @@ public void apply(Integer item) {
119127
}
120128
});
121129
assertEquals("[3, 2, 1]", result2.toString());
130+
final List<Map.Entry<String, Integer>> resultChain = new ArrayList<Map.Entry<String, Integer>>();
131+
$.chain((new LinkedHashMap<String, Integer>() { { put("a", 1); put("b", 2); put("c", 3); } }).entrySet())
132+
.forEachRight(new Block<Map.Entry<String, Integer>>() {
133+
public void apply(final Map.Entry<String, Integer> item) {
134+
resultChain.add(item);
135+
}
136+
});
137+
assertEquals("[c=3, b=2, a=1]", resultChain.toString());
122138
}
123139

124140
/*

0 commit comments

Comments
 (0)