Skip to content

Commit

Permalink
Add support for the $mapIndexed() and $.forEachIndexed() methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev committed Apr 20, 2018
1 parent 9777c6b commit 0eb9392
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -140,6 +140,7 @@
<option>-keepattributes Signature,InnerClasses</option>
<option>-keep public class com.github.underscore.$ { *; }</option>
<option>-keep public class com.github.underscore.*$Chain { *; }</option>
<option>-keep public class com.github.underscore.BiConsumer { *; }</option>
<option>-keep public class com.github.underscore.BiFunction { *; }</option>
<option>-keep public class com.github.underscore.Consumer { *; }</option>
<option>-keep public class com.github.underscore.Function { *; }</option>
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/underscore/$.java
Expand Up @@ -223,6 +223,14 @@ public static <T> void each(final Iterable<T> iterable, final Consumer<? super T
}
}

public static <T> void eachIndexed(final Iterable<T> iterable, final BiConsumer<Integer, ? super T> func) {
int index = 0;
for (T element : iterable) {
func.accept(index, element);
index += 1;
}
}

public void each(final Consumer<? super T> func) {
each(iterable, func);
}
Expand All @@ -239,10 +247,18 @@ public static <T> void forEach(final Iterable<T> iterable, final Consumer<? supe
each(iterable, func);
}

public static <T> void forEachIndexed(final Iterable<T> iterable, final BiConsumer<Integer, ? super T> func) {
eachIndexed(iterable, func);
}

public void forEach(final Consumer<? super T> func) {
each(iterable, func);
}

public void forEachIndexed(final BiConsumer<Integer, ? super T> func) {
eachIndexed(iterable, func);
}

public static <T> void forEachRight(final Iterable<T> iterable, final Consumer<? super T> func) {
eachRight(iterable, func);
}
Expand Down Expand Up @@ -279,6 +295,20 @@ public static <T, E> Set<T> map(final Set<E> set, final Function<? super E, T> f
return transformed;
}

public static <T, E> List<T> mapIndexed(final List<E> list, final BiFunction<Integer, ? super E, T> func) {
final List<T> transformed = newArrayListWithExpectedSize(list.size());
int index = 0;
for (E element : list) {
transformed.add(func.apply(index, element));
index += 1;
}
return transformed;
}

public <F> List<F> mapIndexed(final BiFunction<Integer, ? super T, F> func) {
return mapIndexed(newArrayList(iterable), func);
}

public static <T, E> List<T> collect(final List<E> list, final Function<? super E, T> func) {
return map(list, func);
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/github/underscore/BiConsumer.java
@@ -0,0 +1,5 @@
package com.github.underscore;

public interface BiConsumer<F1, F2> {
void accept(F1 arg1, F2 arg2);
}
43 changes: 43 additions & 0 deletions src/test/java/com/github/underscore/CollectionsTest.java
Expand Up @@ -106,6 +106,29 @@ public void accept(final Map.Entry<String, Integer> item) {
assertEquals("[a=1, b=2, c=3]", resultChain.toString());
}

/*
_.forEachIndexed([1, 2, 3], alert);
=> alerts each number in turn...
*/
@Test
@SuppressWarnings("unchecked")
public void forEachIndexed() {
final List<Integer> result = new ArrayList<Integer>();
$.forEachIndexed(asList(1, 2, 3), new BiConsumer<Integer, Integer>() {
public void accept(Integer index, Integer item) {
result.add(item);
}
});
assertEquals("[1, 2, 3]", result.toString());
final List<Integer> resultObj = new ArrayList<Integer>();
new $(asList(1, 2, 3)).forEachIndexed(new BiConsumer<Integer, Integer>() {
public void accept(Integer index, Integer item) {
resultObj.add(item);
}
});
assertEquals("[1, 2, 3]", resultObj.toString());
}

/*
_.forEach([1, 2, 3], alert);
=> alerts each number in turn from right to left...
Expand Down Expand Up @@ -220,6 +243,26 @@ public Integer apply(Map.Entry<Integer, String> item) {
assertEquals("[3, 6, 9]", result.toString());
}

/*
_.mapIndexed([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
*/
@Test
public void mapIndexed() {
List<Integer> result = $.mapIndexed(asList(1, 2, 3), new BiFunction<Integer, Integer, Integer>() {
public Integer apply(Integer index, Integer item) {
return item * 3;
}
});
assertEquals("[3, 6, 9]", result.toString());
List<Integer> resultObject = new $<Integer>(asList(1, 2, 3)).mapIndexed(new BiFunction<Integer, Integer, Integer>() {
public Integer apply(Integer index, Integer item) {
return item * 3;
}
});
assertEquals("[3, 6, 9]", resultObject.toString());
}

/*
_.collect([1, 2, 3], function(num){ return num * 3; });
=> [3, 6, 9]
Expand Down

0 comments on commit 0eb9392

Please sign in to comment.