Skip to content

Commit 0781780

Browse files
committed
Permutation.from(int)
1 parent d8f05ee commit 0781780

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

mug/src/test/java/com/google/mu/util/stream/PermutationTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,24 @@ public class PermutationTest {
3737

3838
static <T> Stream<ImmutableList<T>> permute(Collection<T> elements) {
3939
class Permutation extends Iteration<ImmutableList<T>> {
40-
Permutation() {
41-
lazily(() -> next(new ArrayList<>(elements), 0));
42-
}
40+
final List<T> buffer = new ArrayList<>(elements);
4341

44-
void next(List<T> buffer, int i) {
42+
Permutation from(int i) {
4543
if (i == buffer.size()) {
4644
emit(ImmutableList.copyOf(buffer));
47-
return;
45+
return this;
4846
}
49-
lazily(() -> next(buffer, i + 1));
47+
lazily(() -> from(i + 1));
5048
forEachLazily(
5149
IntStream.range(i + 1, buffer.size()),
5250
j -> {
5351
Collections.swap(buffer, i, j);
54-
next(buffer, i + 1);
52+
from(i + 1);
5553
lazily(() -> Collections.swap(buffer, i, j));
5654
});
55+
return this;
5756
}
5857
}
59-
return new Permutation().iterate();
58+
return new Permutation().from(0).iterate();
6059
}
6160
}

0 commit comments

Comments
 (0)