Skip to content

Commit 3966f5c

Browse files
committed
Add support for the chunk method.
1 parent 5eeb9f1 commit 3966f5c

File tree

2 files changed

+11
-4
lines changed
  • lodash-plugin/src

2 files changed

+11
-4
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ public static <T> Chain chain(final T ... list) {
6060
return new $.Chain<T>(Arrays.asList(list));
6161
}
6262

63-
public static <T> List<List<T>> chunk(final List<T> list, final Integer count) {
64-
return Collections.emptyList();
63+
public static <T> List<List<T>> chunk(final List<T> list, final Integer size) {
64+
int index = 0;
65+
int length = list.size();
66+
final List<List<T>> result = new ArrayList<List<T>>(length / size);
67+
while (index < length) {
68+
result.add(list.subList(index, Math.min(length, index + size)));
69+
index += size;
70+
}
71+
return result;
6572
}
6673

6774
public static void main(String ... args) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ public class LodashTest {
4444
*/
4545
@Test
4646
public void chunk() {
47-
assertEquals("[]", $.chunk(asList("a", "b", "c", "d"), 2).toString());
48-
assertEquals("[]", $.chunk(asList("a", "b", "c", "d"), 3).toString());
47+
assertEquals("[[a, b], [c, d]]", $.chunk(asList("a", "b", "c", "d"), 2).toString());
48+
assertEquals("[[a, b, c], [d]]", $.chunk(asList("a", "b", "c", "d"), 3).toString());
4949
}
5050

5151
@Test

0 commit comments

Comments
 (0)