Skip to content

Commit 0eb9392

Browse files
authored
Add support for the $mapIndexed() and $.forEachIndexed() methods.
1 parent 9777c6b commit 0eb9392

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

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.BiConsumer { *; }</option>
143144
<option>-keep public class com.github.underscore.BiFunction { *; }</option>
144145
<option>-keep public class com.github.underscore.Consumer { *; }</option>
145146
<option>-keep public class com.github.underscore.Function { *; }</option>

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ public static <T> void each(final Iterable<T> iterable, final Consumer<? super T
223223
}
224224
}
225225

226+
public static <T> void eachIndexed(final Iterable<T> iterable, final BiConsumer<Integer, ? super T> func) {
227+
int index = 0;
228+
for (T element : iterable) {
229+
func.accept(index, element);
230+
index += 1;
231+
}
232+
}
233+
226234
public void each(final Consumer<? super T> func) {
227235
each(iterable, func);
228236
}
@@ -239,10 +247,18 @@ public static <T> void forEach(final Iterable<T> iterable, final Consumer<? supe
239247
each(iterable, func);
240248
}
241249

250+
public static <T> void forEachIndexed(final Iterable<T> iterable, final BiConsumer<Integer, ? super T> func) {
251+
eachIndexed(iterable, func);
252+
}
253+
242254
public void forEach(final Consumer<? super T> func) {
243255
each(iterable, func);
244256
}
245257

258+
public void forEachIndexed(final BiConsumer<Integer, ? super T> func) {
259+
eachIndexed(iterable, func);
260+
}
261+
246262
public static <T> void forEachRight(final Iterable<T> iterable, final Consumer<? super T> func) {
247263
eachRight(iterable, func);
248264
}
@@ -279,6 +295,20 @@ public static <T, E> Set<T> map(final Set<E> set, final Function<? super E, T> f
279295
return transformed;
280296
}
281297

298+
public static <T, E> List<T> mapIndexed(final List<E> list, final BiFunction<Integer, ? super E, T> func) {
299+
final List<T> transformed = newArrayListWithExpectedSize(list.size());
300+
int index = 0;
301+
for (E element : list) {
302+
transformed.add(func.apply(index, element));
303+
index += 1;
304+
}
305+
return transformed;
306+
}
307+
308+
public <F> List<F> mapIndexed(final BiFunction<Integer, ? super T, F> func) {
309+
return mapIndexed(newArrayList(iterable), func);
310+
}
311+
282312
public static <T, E> List<T> collect(final List<E> list, final Function<? super E, T> func) {
283313
return map(list, func);
284314
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.github.underscore;
2+
3+
public interface BiConsumer<F1, F2> {
4+
void accept(F1 arg1, F2 arg2);
5+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,29 @@ public void accept(final Map.Entry<String, Integer> item) {
106106
assertEquals("[a=1, b=2, c=3]", resultChain.toString());
107107
}
108108

109+
/*
110+
_.forEachIndexed([1, 2, 3], alert);
111+
=> alerts each number in turn...
112+
*/
113+
@Test
114+
@SuppressWarnings("unchecked")
115+
public void forEachIndexed() {
116+
final List<Integer> result = new ArrayList<Integer>();
117+
$.forEachIndexed(asList(1, 2, 3), new BiConsumer<Integer, Integer>() {
118+
public void accept(Integer index, Integer item) {
119+
result.add(item);
120+
}
121+
});
122+
assertEquals("[1, 2, 3]", result.toString());
123+
final List<Integer> resultObj = new ArrayList<Integer>();
124+
new $(asList(1, 2, 3)).forEachIndexed(new BiConsumer<Integer, Integer>() {
125+
public void accept(Integer index, Integer item) {
126+
resultObj.add(item);
127+
}
128+
});
129+
assertEquals("[1, 2, 3]", resultObj.toString());
130+
}
131+
109132
/*
110133
_.forEach([1, 2, 3], alert);
111134
=> alerts each number in turn from right to left...
@@ -220,6 +243,26 @@ public Integer apply(Map.Entry<Integer, String> item) {
220243
assertEquals("[3, 6, 9]", result.toString());
221244
}
222245

246+
/*
247+
_.mapIndexed([1, 2, 3], function(num){ return num * 3; });
248+
=> [3, 6, 9]
249+
*/
250+
@Test
251+
public void mapIndexed() {
252+
List<Integer> result = $.mapIndexed(asList(1, 2, 3), new BiFunction<Integer, Integer, Integer>() {
253+
public Integer apply(Integer index, Integer item) {
254+
return item * 3;
255+
}
256+
});
257+
assertEquals("[3, 6, 9]", result.toString());
258+
List<Integer> resultObject = new $<Integer>(asList(1, 2, 3)).mapIndexed(new BiFunction<Integer, Integer, Integer>() {
259+
public Integer apply(Integer index, Integer item) {
260+
return item * 3;
261+
}
262+
});
263+
assertEquals("[3, 6, 9]", resultObject.toString());
264+
}
265+
223266
/*
224267
_.collect([1, 2, 3], function(num){ return num * 3; });
225268
=> [3, 6, 9]

0 commit comments

Comments
 (0)