Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Exposed the N-Ary cartesian product.

  • Loading branch information...
commit 8078b2e7e027dde1c79a88ad3283461dbe2de148 1 parent e89fa13
@akiellor akiellor authored
View
2  src/main/java/org/javafunk/funk/Lazily.java
@@ -216,7 +216,7 @@ public Boolean map(Pair<T, T> input) {
return map(cartesianProduct(listWith(first, second, third, fourth)), Mappers.<S, T, U, V>toQuadruple());
}
- private static Iterable<? extends Iterable<?>> cartesianProduct(final Iterable<? extends Iterable<?>> iterables) {
+ public static Iterable<? extends Iterable<?>> cartesianProduct(final Iterable<? extends Iterable<?>> iterables) {
return cartesianProduct(listFrom(iterables));
}
View
27 src/test/java/org/javafunk/funk/LazilyCartesianProductTest.java
@@ -97,6 +97,33 @@ public void shouldReturnTheCartesianProductOfFourSuppliedIterablesAsAnIterableOf
}
@Test
+ @SuppressWarnings("unchecked")
+ public void shouldReturnTheCartesianProductOfNSuppliedIterablesAsAnIterableOfIterables() throws Exception {
+ // Given
+ Iterable<Integer> input1 = listWith(1, 2);
+ Iterable<String> input2 = listWith("a", "b", "c");
+ Iterable<Long> input3 = listWith(1L, 2L, 3L);
+
+ Collection expectedCartesianProduct = Literals.listWith(
+ listWith(1, "a", 1L), listWith(2, "a", 1L),
+ listWith(1, "b", 1L), listWith(2, "b", 1L),
+ listWith(1, "c", 1L), listWith(2, "c", 1L),
+ listWith(1, "a", 2L), listWith(2, "a", 2L),
+ listWith(1, "b", 2L), listWith(2, "b", 2L),
+ listWith(1, "c", 2L), listWith(2, "c", 2L),
+ listWith(1, "a", 3L), listWith(2, "a", 3L),
+ listWith(1, "b", 3L), listWith(2, "b", 3L),
+ listWith(1, "c", 3L), listWith(2, "c", 3L)
+ );
+
+ // When
+ Collection actualCartesianProduct = materialize(Lazily.cartesianProduct(listWith(input1, input2, input3)));
+
+ // Then
+ assertThat(actualCartesianProduct, hasOnlyItemsInAnyOrder(expectedCartesianProduct));
+ }
+
+ @Test
public void shouldAllowIteratorToBeCalledMultipleTimesReturningDifferentIterators() throws Exception {
// Given
Iterable<Integer> input1 = listWith(1, 2, 3);
Please sign in to comment.
Something went wrong with that request. Please try again.