Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/org/jayield/advs/AdvancerList.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
public class AdvancerList<U> implements Advancer<U> {
private final List<U> data;
private final Iterator<U> current;
private int index;

public AdvancerList(List<U> data) {
this.data = data;
this.current = data.iterator();
index = 0;
}

@Override
public U next() {
index++;
return current.next();
}

Expand All @@ -43,6 +46,8 @@ public boolean hasNext() {

@Override
public void traverse(Yield<? super U> yield) {
data.forEach(yield::ret);
for (int i = index; i < data.size(); i++) {
yield.ret(data.get(i));
}
}
}
24 changes: 24 additions & 0 deletions src/test/java/org/jayield/QueryTraverseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.jayield;

import static java.util.Arrays.asList;
import static org.jayield.Query.fromList;
import static org.jayield.Query.fromStream;
import static org.jayield.Query.iterate;
import static org.jayield.Query.of;
Expand Down Expand Up @@ -293,6 +294,29 @@ public void testReduce() {
assertEquals(actual, expected);
}

@Test
public void testFlatMapAndReduce() {
List<Query<String>> input = new ArrayList<>();
input.add(Query.of("a"));
input.add(Query.of("b"));
input.add(Query.of("c"));
String expected = "abc";
String actual = fromList(input).flatMap(s -> s).reduce((p, c) -> p + c).orElseThrow();
assertEquals(actual, expected);
}


@Test
public void testFromListFlatMapAndReduce() {
List<Query<String>> input = new ArrayList<>();
input.add(fromList(List.of("a")));
input.add(fromList(List.of("b")));
input.add(fromList(List.of("c")));
String expected = "abc";
String actual = fromList(input).flatMap(s -> s).reduce((p, c) -> p + c).orElseThrow();
assertEquals(actual, expected);
}

@Test
public void testReduceOnEmpty() {
String[] input = {};
Expand Down