From 161e1624aab4aa7d1540a106659076a498230927 Mon Sep 17 00:00:00 2001 From: Igor <431522+CauchyPeano@users.noreply.github.com> Date: Sat, 4 Aug 2018 16:01:10 +0200 Subject: [PATCH] Initial implementation of new For() constructs (#1629) (#2281) --- .../test/java/io/vavr/idiom/ForBenchmark.java | 4 +- vavr/generator/Generator.scala | 67 +- vavr/src-gen/main/java/io/vavr/API.java | 2161 ++++++++++++++--- vavr/src-gen/test/java/io/vavr/APITest.java | 295 ++- 4 files changed, 2132 insertions(+), 395 deletions(-) diff --git a/vavr-benchmark/src/test/java/io/vavr/idiom/ForBenchmark.java b/vavr-benchmark/src/test/java/io/vavr/idiom/ForBenchmark.java index 71f711c1ea..21845abb08 100644 --- a/vavr-benchmark/src/test/java/io/vavr/idiom/ForBenchmark.java +++ b/vavr-benchmark/src/test/java/io/vavr/idiom/ForBenchmark.java @@ -59,13 +59,13 @@ public static void main(String... args) { public static class Base { int CONTAINER_SIZE = 1000; int AGGREGATE; - List ELEMENTS; + io.vavr.collection.List ELEMENTS; final BiFunction AGGREGATOR = (i, j) -> i ^ j; @Setup public void setup() { - ELEMENTS = Arrays.asList(getRandomValues(CONTAINER_SIZE, 0, true)); + ELEMENTS = io.vavr.collection.List.ofAll(Arrays.stream(getRandomValues(CONTAINER_SIZE, 0, true))); AGGREGATE = 0; for (Integer i : ELEMENTS) { diff --git a/vavr/generator/Generator.scala b/vavr/generator/Generator.scala index 47713c28b7..0a0ebe608d 100644 --- a/vavr/generator/Generator.scala +++ b/vavr/generator/Generator.scala @@ -100,6 +100,8 @@ def generateMainClasses(): Unit = { val PredicateType = im.getType("java.util.function.Predicate") val SupplierType = im.getType("java.util.function.Supplier") + val monadicTypesFor = List(OptionType, FutureType, TryType, ListType) + def genTraversableAliases(traversableType: String, returnType: String, name: String) = xs""" // -- $name @@ -709,24 +711,24 @@ def generateMainClasses(): Unit = { return $IteratorType.ofAll(ts).flatMap(f); } - ${(1 to N).gen(i => { + ${monadicTypesFor.gen(mtype => (1 to N).gen(i => { val generics = (1 to i).gen(j => s"T$j")(", ") - val params = (1 to i).gen(j => s"Iterable ts$j")(", ") + val params = (1 to i).gen(j => s"$mtype ts$j")(", ") xs""" /$javadoc - * Creates a {@code For}-comprehension of ${i.numerus("Iterable")}. - ${(0 to i).gen(j => if (j == 0) "*" else s"* @param ts$j the ${j.ordinal} Iterable")("\n")} - ${(1 to i).gen(j => s"* @param component type of the ${j.ordinal} Iterable")("\n")} + * Creates a {@code For}-comprehension of ${i.numerus(mtype)}. + ${(0 to i).gen(j => if (j == 0) "*" else s"* @param ts$j the ${j.ordinal} $mtype")("\n")} + ${(1 to i).gen(j => s"* @param component type of the ${j.ordinal} $mtype")("\n")} * @return a new {@code For}-comprehension of arity $i */ - public static <$generics> For$i<$generics> For($params) { + public static <$generics> For$i$mtype<$generics> For($params) { ${(1 to i).gen(j => xs"""$Objects.requireNonNull(ts$j, "ts$j is null");""")("\n")} - return new For$i<>(${(1 to i).gen(j => s"ts$j")(", ")}); + return new For$i$mtype<>(${(1 to i).gen(j => s"ts$j")(", ")}); } """ - })("\n\n")} + })("\n\n"))("\n\n")} - ${(1 to N).gen(i => { + ${monadicTypesFor.gen(mtype => (1 to N).gen(i => { val generics = (1 to i).gen(j => s"T$j")(", ") val functionType = i match { case 1 => FunctionType @@ -736,13 +738,13 @@ def generateMainClasses(): Unit = { val args = (1 to i).gen(j => s"? super T$j")(", ") xs""" /$javadoc - * For-comprehension with ${i.numerus("Iterable")}. + * For-comprehension with ${i.numerus(IteratorType)}. */ - public static class For$i<$generics> { + public static class For$i$mtype<$generics> { - ${(1 to i).gen(j => xs"""private final Iterable ts$j;""")("\n")} + ${(1 to i).gen(j => xs"""private final $mtype ts$j;""")("\n")} - private For$i(${(1 to i).gen(j => s"Iterable ts$j")(", ")}) { + private For$i$mtype(${(1 to i).gen(j => s"$mtype ts$j")(", ")}) { ${(1 to i).gen(j => xs"""this.ts$j = ts$j;""")("\n")} } @@ -753,14 +755,14 @@ def generateMainClasses(): Unit = { * @param type of the resulting {@code Iterator} elements * @return an {@code Iterator} of mapped results */ - public $IteratorType yield($functionType<$args, ? extends R> f) { + public $mtype yield($functionType<$args, ? extends R> f) { $Objects.requireNonNull(f, "f is null"); ${if (i == 1) xs""" - return $IteratorType.ofAll(ts1).map(f); + return ts1.map(f); """ else xs""" return - ${(1 until i).gen(j => s"$IteratorType.ofAll(ts$j).flatMap(t$j ->")("\n")} - $IteratorType.ofAll(ts$i).map(t$i -> f.apply(${(1 to i).gen(j => s"t$j")(", ")}))${")" * (i - 1)}; + ${(1 until i).gen(j => s"ts$j.flatMap(t$j ->")("\n")} + ts$i.map(t$i -> f.apply(${(1 to i).gen(j => s"t$j")(", ")}))${")" * (i - 1)}; """} } @@ -770,13 +772,13 @@ def generateMainClasses(): Unit = { * * @return an {@code Iterator} of mapped results */ - public $IteratorType yield() { + public $mtype yield() { return yield(Function.identity()); } """)} } """ - })("\n\n")} + })("\n\n"))("\n\n")} """ } @@ -2382,6 +2384,9 @@ def generateTestClasses(): Unit = { val TryType = im.getType("io.vavr.control.Try") val JavaComparatorType = im.getType("java.util.Comparator") + val monadicTypesFor = List(OptionType) + val monadicFunctionTypesFor = List(FutureType, TryType) + val d = "$" im.getStatic("io.vavr.API.*") @@ -2662,7 +2667,7 @@ def generateTestClasses(): Unit = { ${(1 to N).gen(i => xs""" @$test - public void shouldIterateFor$i() { + public void shouldIterateFor$ListType$i() { final $ListType result = For( ${(1 to i).gen(j => s"$ListType.of(1, 2, 3)")(",\n")} ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}).toList(); @@ -2672,11 +2677,31 @@ def generateTestClasses(): Unit = { } """)("\n\n")} + ${monadicTypesFor.gen(mtype => (1 to N).gen(i => { xs""" + @$test + public void shouldIterateFor$mtype$i() { + final $mtype result = For( + ${(1 to i).gen(j => s"$mtype.of($j)")(",\n")} + ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); + $assertThat(result.get()).isEqualTo(${(1 to i).sum}); + } + """})("\n\n"))("\n\n")} + + ${monadicFunctionTypesFor.gen(mtype => (1 to N).gen(i => { xs""" + @$test + public void shouldIterateFor$mtype$i() { + final $mtype result = For( + ${(1 to i).gen(j => s"$mtype.of(() -> $j)")(",\n")} + ).yield(${(i > 1).gen("(")}${(1 to i).gen(j => s"i$j")(", ")}${(i > 1).gen(")")} -> ${(1 to i).gen(j => s"i$j")(" + ")}); + $assertThat(result.get()).isEqualTo(${(1 to i).sum}); + } + """})("\n\n"))("\n\n")} + @$test public void shouldIterateNestedFor() { final $ListType result = For(${im.getType("java.util.Arrays")}.asList(1, 2), i -> - For(${im.getType("io.vavr.collection.CharSeq")}.of('a', 'b')).yield(c -> i + ":" + c)).toList(); + For(${im.getType("io.vavr.collection.List")}.of('a', 'b')).yield(c -> i + ":" + c)).toList(); assertThat(result).isEqualTo($ListType.of("1:a", "1:b", "2:a", "2:b")); } diff --git a/vavr/src-gen/main/java/io/vavr/API.java b/vavr/src-gen/main/java/io/vavr/API.java index bb1eb1971f..3ebc25aac4 100644 --- a/vavr/src-gen/main/java/io/vavr/API.java +++ b/vavr/src-gen/main/java/io/vavr/API.java @@ -2254,142 +2254,142 @@ public static Iterator For(Iterable ts, Function component type of the 1st Iterable + * @param ts1 the 1st Option + * @param component type of the 1st Option * @return a new {@code For}-comprehension of arity 1 */ - public static For1 For(Iterable ts1) { + public static For1Option For(Option ts1) { Objects.requireNonNull(ts1, "ts1 is null"); - return new For1<>(ts1); + return new For1Option<>(ts1); } /** - * Creates a {@code For}-comprehension of two Iterables. + * Creates a {@code For}-comprehension of two Options. * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param component type of the 1st Option + * @param component type of the 2nd Option * @return a new {@code For}-comprehension of arity 2 */ - public static For2 For(Iterable ts1, Iterable ts2) { + public static For2Option For(Option ts1, Option ts2) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); - return new For2<>(ts1, ts2); + return new For2Option<>(ts1, ts2); } /** - * Creates a {@code For}-comprehension of three Iterables. + * Creates a {@code For}-comprehension of three Options. * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option * @return a new {@code For}-comprehension of arity 3 */ - public static For3 For(Iterable ts1, Iterable ts2, Iterable ts3) { + public static For3Option For(Option ts1, Option ts2, Option ts3) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); - return new For3<>(ts1, ts2, ts3); + return new For3Option<>(ts1, ts2, ts3); } /** - * Creates a {@code For}-comprehension of 4 Iterables. + * Creates a {@code For}-comprehension of 4 Options. * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param ts4 the 4th Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable - * @param component type of the 4th Iterable + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param ts4 the 4th Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option + * @param component type of the 4th Option * @return a new {@code For}-comprehension of arity 4 */ - public static For4 For(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4) { + public static For4Option For(Option ts1, Option ts2, Option ts3, Option ts4) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); Objects.requireNonNull(ts4, "ts4 is null"); - return new For4<>(ts1, ts2, ts3, ts4); + return new For4Option<>(ts1, ts2, ts3, ts4); } /** - * Creates a {@code For}-comprehension of 5 Iterables. + * Creates a {@code For}-comprehension of 5 Options. * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param ts4 the 4th Iterable - * @param ts5 the 5th Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable - * @param component type of the 4th Iterable - * @param component type of the 5th Iterable + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param ts4 the 4th Option + * @param ts5 the 5th Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option + * @param component type of the 4th Option + * @param component type of the 5th Option * @return a new {@code For}-comprehension of arity 5 */ - public static For5 For(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5) { + public static For5Option For(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); Objects.requireNonNull(ts4, "ts4 is null"); Objects.requireNonNull(ts5, "ts5 is null"); - return new For5<>(ts1, ts2, ts3, ts4, ts5); + return new For5Option<>(ts1, ts2, ts3, ts4, ts5); } /** - * Creates a {@code For}-comprehension of 6 Iterables. + * Creates a {@code For}-comprehension of 6 Options. * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param ts4 the 4th Iterable - * @param ts5 the 5th Iterable - * @param ts6 the 6th Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable - * @param component type of the 4th Iterable - * @param component type of the 5th Iterable - * @param component type of the 6th Iterable + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param ts4 the 4th Option + * @param ts5 the 5th Option + * @param ts6 the 6th Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option + * @param component type of the 4th Option + * @param component type of the 5th Option + * @param component type of the 6th Option * @return a new {@code For}-comprehension of arity 6 */ - public static For6 For(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6) { + public static For6Option For(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); Objects.requireNonNull(ts4, "ts4 is null"); Objects.requireNonNull(ts5, "ts5 is null"); Objects.requireNonNull(ts6, "ts6 is null"); - return new For6<>(ts1, ts2, ts3, ts4, ts5, ts6); - } - - /** - * Creates a {@code For}-comprehension of 7 Iterables. - * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param ts4 the 4th Iterable - * @param ts5 the 5th Iterable - * @param ts6 the 6th Iterable - * @param ts7 the 7th Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable - * @param component type of the 4th Iterable - * @param component type of the 5th Iterable - * @param component type of the 6th Iterable - * @param component type of the 7th Iterable + return new For6Option<>(ts1, ts2, ts3, ts4, ts5, ts6); + } + + /** + * Creates a {@code For}-comprehension of 7 Options. + * + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param ts4 the 4th Option + * @param ts5 the 5th Option + * @param ts6 the 6th Option + * @param ts7 the 7th Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option + * @param component type of the 4th Option + * @param component type of the 5th Option + * @param component type of the 6th Option + * @param component type of the 7th Option * @return a new {@code For}-comprehension of arity 7 */ - public static For7 For(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6, Iterable ts7) { + public static For7Option For(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6, Option ts7) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); @@ -2397,31 +2397,31 @@ public static For7 For( Objects.requireNonNull(ts5, "ts5 is null"); Objects.requireNonNull(ts6, "ts6 is null"); Objects.requireNonNull(ts7, "ts7 is null"); - return new For7<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7); - } - - /** - * Creates a {@code For}-comprehension of 8 Iterables. - * - * @param ts1 the 1st Iterable - * @param ts2 the 2nd Iterable - * @param ts3 the 3rd Iterable - * @param ts4 the 4th Iterable - * @param ts5 the 5th Iterable - * @param ts6 the 6th Iterable - * @param ts7 the 7th Iterable - * @param ts8 the 8th Iterable - * @param component type of the 1st Iterable - * @param component type of the 2nd Iterable - * @param component type of the 3rd Iterable - * @param component type of the 4th Iterable - * @param component type of the 5th Iterable - * @param component type of the 6th Iterable - * @param component type of the 7th Iterable - * @param component type of the 8th Iterable + return new For7Option<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7); + } + + /** + * Creates a {@code For}-comprehension of 8 Options. + * + * @param ts1 the 1st Option + * @param ts2 the 2nd Option + * @param ts3 the 3rd Option + * @param ts4 the 4th Option + * @param ts5 the 5th Option + * @param ts6 the 6th Option + * @param ts7 the 7th Option + * @param ts8 the 8th Option + * @param component type of the 1st Option + * @param component type of the 2nd Option + * @param component type of the 3rd Option + * @param component type of the 4th Option + * @param component type of the 5th Option + * @param component type of the 6th Option + * @param component type of the 7th Option + * @param component type of the 8th Option * @return a new {@code For}-comprehension of arity 8 */ - public static For8 For(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6, Iterable ts7, Iterable ts8) { + public static For8Option For(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6, Option ts7, Option ts8) { Objects.requireNonNull(ts1, "ts1 is null"); Objects.requireNonNull(ts2, "ts2 is null"); Objects.requireNonNull(ts3, "ts3 is null"); @@ -2430,308 +2430,1745 @@ public static For8(ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8); + return new For8Option<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8); } /** - * For-comprehension with one Iterable. + * Creates a {@code For}-comprehension of one Future. + * + * @param ts1 the 1st Future + * @param component type of the 1st Future + * @return a new {@code For}-comprehension of arity 1 */ - public static class For1 { - - private final Iterable ts1; - - private For1(Iterable ts1) { - this.ts1 = ts1; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function f) { - Objects.requireNonNull(f, "f is null"); - return Iterator.ofAll(ts1).map(f); - } - - /** - * A shortcut for {@code yield(Function.identity())}. - * - * @return an {@code Iterator} of mapped results - */ - public Iterator yield() { - return yield(Function.identity()); - } + public static For1Future For(Future ts1) { + Objects.requireNonNull(ts1, "ts1 is null"); + return new For1Future<>(ts1); } /** - * For-comprehension with two Iterables. + * Creates a {@code For}-comprehension of two Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @return a new {@code For}-comprehension of arity 2 */ - public static class For2 { - - private final Iterable ts1; - private final Iterable ts2; - - private For2(Iterable ts1, Iterable ts2) { - this.ts1 = ts1; - this.ts2 = ts2; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(BiFunction f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).map(t2 -> f.apply(t1, t2))); - } - + public static For2Future For(Future ts1, Future ts2) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + return new For2Future<>(ts1, ts2); } /** - * For-comprehension with three Iterables. + * Creates a {@code For}-comprehension of three Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @return a new {@code For}-comprehension of arity 3 */ - public static class For3 { - - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - - private For3(Iterable ts1, Iterable ts2, Iterable ts3) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function3 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).map(t3 -> f.apply(t1, t2, t3)))); - } - + public static For3Future For(Future ts1, Future ts2, Future ts3) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + return new For3Future<>(ts1, ts2, ts3); } /** - * For-comprehension with 4 Iterables. + * Creates a {@code For}-comprehension of 4 Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param ts4 the 4th Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @param component type of the 4th Future + * @return a new {@code For}-comprehension of arity 4 */ - public static class For4 { - - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - private final Iterable ts4; - - private For4(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - this.ts4 = ts4; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function4 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).flatMap(t3 -> - Iterator.ofAll(ts4).map(t4 -> f.apply(t1, t2, t3, t4))))); - } - + public static For4Future For(Future ts1, Future ts2, Future ts3, Future ts4) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + return new For4Future<>(ts1, ts2, ts3, ts4); } /** - * For-comprehension with 5 Iterables. + * Creates a {@code For}-comprehension of 5 Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param ts4 the 4th Future + * @param ts5 the 5th Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @param component type of the 4th Future + * @param component type of the 5th Future + * @return a new {@code For}-comprehension of arity 5 */ - public static class For5 { - - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - private final Iterable ts4; - private final Iterable ts5; - - private For5(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - this.ts4 = ts4; - this.ts5 = ts5; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function5 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).flatMap(t3 -> - Iterator.ofAll(ts4).flatMap(t4 -> - Iterator.ofAll(ts5).map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); - } - + public static For5Future For(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + return new For5Future<>(ts1, ts2, ts3, ts4, ts5); } /** - * For-comprehension with 6 Iterables. + * Creates a {@code For}-comprehension of 6 Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param ts4 the 4th Future + * @param ts5 the 5th Future + * @param ts6 the 6th Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @param component type of the 4th Future + * @param component type of the 5th Future + * @param component type of the 6th Future + * @return a new {@code For}-comprehension of arity 6 */ - public static class For6 { - - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - private final Iterable ts4; - private final Iterable ts5; - private final Iterable ts6; - - private For6(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - this.ts4 = ts4; - this.ts5 = ts5; - this.ts6 = ts6; - } - - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function6 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).flatMap(t3 -> - Iterator.ofAll(ts4).flatMap(t4 -> - Iterator.ofAll(ts5).flatMap(t5 -> - Iterator.ofAll(ts6).map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); - } + public static For6Future For(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + return new For6Future<>(ts1, ts2, ts3, ts4, ts5, ts6); + } + + /** + * Creates a {@code For}-comprehension of 7 Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param ts4 the 4th Future + * @param ts5 the 5th Future + * @param ts6 the 6th Future + * @param ts7 the 7th Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @param component type of the 4th Future + * @param component type of the 5th Future + * @param component type of the 6th Future + * @param component type of the 7th Future + * @return a new {@code For}-comprehension of arity 7 + */ + public static For7Future For(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6, Future ts7) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + return new For7Future<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7); + } + + /** + * Creates a {@code For}-comprehension of 8 Futures. + * + * @param ts1 the 1st Future + * @param ts2 the 2nd Future + * @param ts3 the 3rd Future + * @param ts4 the 4th Future + * @param ts5 the 5th Future + * @param ts6 the 6th Future + * @param ts7 the 7th Future + * @param ts8 the 8th Future + * @param component type of the 1st Future + * @param component type of the 2nd Future + * @param component type of the 3rd Future + * @param component type of the 4th Future + * @param component type of the 5th Future + * @param component type of the 6th Future + * @param component type of the 7th Future + * @param component type of the 8th Future + * @return a new {@code For}-comprehension of arity 8 + */ + public static For8Future For(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6, Future ts7, Future ts8) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + Objects.requireNonNull(ts8, "ts8 is null"); + return new For8Future<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8); + } + /** + * Creates a {@code For}-comprehension of one Try. + * + * @param ts1 the 1st Try + * @param component type of the 1st Try + * @return a new {@code For}-comprehension of arity 1 + */ + public static For1Try For(Try ts1) { + Objects.requireNonNull(ts1, "ts1 is null"); + return new For1Try<>(ts1); } /** - * For-comprehension with 7 Iterables. + * Creates a {@code For}-comprehension of two Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @return a new {@code For}-comprehension of arity 2 */ - public static class For7 { + public static For2Try For(Try ts1, Try ts2) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + return new For2Try<>(ts1, ts2); + } - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - private final Iterable ts4; - private final Iterable ts5; - private final Iterable ts6; - private final Iterable ts7; + /** + * Creates a {@code For}-comprehension of three Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @return a new {@code For}-comprehension of arity 3 + */ + public static For3Try For(Try ts1, Try ts2, Try ts3) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + return new For3Try<>(ts1, ts2, ts3); + } - private For7(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6, Iterable ts7) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - this.ts4 = ts4; - this.ts5 = ts5; - this.ts6 = ts6; - this.ts7 = ts7; - } + /** + * Creates a {@code For}-comprehension of 4 Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param ts4 the 4th Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @param component type of the 4th Try + * @return a new {@code For}-comprehension of arity 4 + */ + public static For4Try For(Try ts1, Try ts2, Try ts3, Try ts4) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + return new For4Try<>(ts1, ts2, ts3, ts4); + } - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function7 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).flatMap(t3 -> - Iterator.ofAll(ts4).flatMap(t4 -> - Iterator.ofAll(ts5).flatMap(t5 -> - Iterator.ofAll(ts6).flatMap(t6 -> - Iterator.ofAll(ts7).map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); - } + /** + * Creates a {@code For}-comprehension of 5 Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param ts4 the 4th Try + * @param ts5 the 5th Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @param component type of the 4th Try + * @param component type of the 5th Try + * @return a new {@code For}-comprehension of arity 5 + */ + public static For5Try For(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + return new For5Try<>(ts1, ts2, ts3, ts4, ts5); + } + /** + * Creates a {@code For}-comprehension of 6 Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param ts4 the 4th Try + * @param ts5 the 5th Try + * @param ts6 the 6th Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @param component type of the 4th Try + * @param component type of the 5th Try + * @param component type of the 6th Try + * @return a new {@code For}-comprehension of arity 6 + */ + public static For6Try For(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + return new For6Try<>(ts1, ts2, ts3, ts4, ts5, ts6); + } + + /** + * Creates a {@code For}-comprehension of 7 Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param ts4 the 4th Try + * @param ts5 the 5th Try + * @param ts6 the 6th Try + * @param ts7 the 7th Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @param component type of the 4th Try + * @param component type of the 5th Try + * @param component type of the 6th Try + * @param component type of the 7th Try + * @return a new {@code For}-comprehension of arity 7 + */ + public static For7Try For(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6, Try ts7) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + return new For7Try<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7); + } + + /** + * Creates a {@code For}-comprehension of 8 Trys. + * + * @param ts1 the 1st Try + * @param ts2 the 2nd Try + * @param ts3 the 3rd Try + * @param ts4 the 4th Try + * @param ts5 the 5th Try + * @param ts6 the 6th Try + * @param ts7 the 7th Try + * @param ts8 the 8th Try + * @param component type of the 1st Try + * @param component type of the 2nd Try + * @param component type of the 3rd Try + * @param component type of the 4th Try + * @param component type of the 5th Try + * @param component type of the 6th Try + * @param component type of the 7th Try + * @param component type of the 8th Try + * @return a new {@code For}-comprehension of arity 8 + */ + public static For8Try For(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6, Try ts7, Try ts8) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + Objects.requireNonNull(ts8, "ts8 is null"); + return new For8Try<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8); } /** - * For-comprehension with 8 Iterables. + * Creates a {@code For}-comprehension of one List. + * + * @param ts1 the 1st List + * @param component type of the 1st List + * @return a new {@code For}-comprehension of arity 1 */ - public static class For8 { + public static For1List For(List ts1) { + Objects.requireNonNull(ts1, "ts1 is null"); + return new For1List<>(ts1); + } - private final Iterable ts1; - private final Iterable ts2; - private final Iterable ts3; - private final Iterable ts4; - private final Iterable ts5; - private final Iterable ts6; - private final Iterable ts7; - private final Iterable ts8; + /** + * Creates a {@code For}-comprehension of two Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param component type of the 1st List + * @param component type of the 2nd List + * @return a new {@code For}-comprehension of arity 2 + */ + public static For2List For(List ts1, List ts2) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + return new For2List<>(ts1, ts2); + } - private For8(Iterable ts1, Iterable ts2, Iterable ts3, Iterable ts4, Iterable ts5, Iterable ts6, Iterable ts7, Iterable ts8) { - this.ts1 = ts1; - this.ts2 = ts2; - this.ts3 = ts3; - this.ts4 = ts4; - this.ts5 = ts5; - this.ts6 = ts6; - this.ts7 = ts7; - this.ts8 = ts8; - } + /** + * Creates a {@code For}-comprehension of three Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @return a new {@code For}-comprehension of arity 3 + */ + public static For3List For(List ts1, List ts2, List ts3) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + return new For3List<>(ts1, ts2, ts3); + } - /** - * Yields a result for elements of the cross product of the underlying Iterables. - * - * @param f a function that maps an element of the cross product to a result - * @param type of the resulting {@code Iterator} elements - * @return an {@code Iterator} of mapped results - */ - public Iterator yield(Function8 f) { - Objects.requireNonNull(f, "f is null"); - return - Iterator.ofAll(ts1).flatMap(t1 -> - Iterator.ofAll(ts2).flatMap(t2 -> - Iterator.ofAll(ts3).flatMap(t3 -> - Iterator.ofAll(ts4).flatMap(t4 -> - Iterator.ofAll(ts5).flatMap(t5 -> - Iterator.ofAll(ts6).flatMap(t6 -> - Iterator.ofAll(ts7).flatMap(t7 -> - Iterator.ofAll(ts8).map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); - } + /** + * Creates a {@code For}-comprehension of 4 Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param ts4 the 4th List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @param component type of the 4th List + * @return a new {@code For}-comprehension of arity 4 + */ + public static For4List For(List ts1, List ts2, List ts3, List ts4) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + return new For4List<>(ts1, ts2, ts3, ts4); + } + /** + * Creates a {@code For}-comprehension of 5 Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param ts4 the 4th List + * @param ts5 the 5th List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @param component type of the 4th List + * @param component type of the 5th List + * @return a new {@code For}-comprehension of arity 5 + */ + public static For5List For(List ts1, List ts2, List ts3, List ts4, List ts5) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + return new For5List<>(ts1, ts2, ts3, ts4, ts5); } + /** + * Creates a {@code For}-comprehension of 6 Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param ts4 the 4th List + * @param ts5 the 5th List + * @param ts6 the 6th List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @param component type of the 4th List + * @param component type of the 5th List + * @param component type of the 6th List + * @return a new {@code For}-comprehension of arity 6 + */ + public static For6List For(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + return new For6List<>(ts1, ts2, ts3, ts4, ts5, ts6); + } + + /** + * Creates a {@code For}-comprehension of 7 Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param ts4 the 4th List + * @param ts5 the 5th List + * @param ts6 the 6th List + * @param ts7 the 7th List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @param component type of the 4th List + * @param component type of the 5th List + * @param component type of the 6th List + * @param component type of the 7th List + * @return a new {@code For}-comprehension of arity 7 + */ + public static For7List For(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6, List ts7) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + return new For7List<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7); + } + + /** + * Creates a {@code For}-comprehension of 8 Lists. + * + * @param ts1 the 1st List + * @param ts2 the 2nd List + * @param ts3 the 3rd List + * @param ts4 the 4th List + * @param ts5 the 5th List + * @param ts6 the 6th List + * @param ts7 the 7th List + * @param ts8 the 8th List + * @param component type of the 1st List + * @param component type of the 2nd List + * @param component type of the 3rd List + * @param component type of the 4th List + * @param component type of the 5th List + * @param component type of the 6th List + * @param component type of the 7th List + * @param component type of the 8th List + * @return a new {@code For}-comprehension of arity 8 + */ + public static For8List For(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6, List ts7, List ts8) { + Objects.requireNonNull(ts1, "ts1 is null"); + Objects.requireNonNull(ts2, "ts2 is null"); + Objects.requireNonNull(ts3, "ts3 is null"); + Objects.requireNonNull(ts4, "ts4 is null"); + Objects.requireNonNull(ts5, "ts5 is null"); + Objects.requireNonNull(ts6, "ts6 is null"); + Objects.requireNonNull(ts7, "ts7 is null"); + Objects.requireNonNull(ts8, "ts8 is null"); + return new For8List<>(ts1, ts2, ts3, ts4, ts5, ts6, ts7, ts8); + } + + /** + * For-comprehension with one Iterator. + */ + public static class For1Option { + + private final Option ts1; + + private For1Option(Option ts1) { + this.ts1 = ts1; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function f) { + Objects.requireNonNull(f, "f is null"); + return ts1.map(f); + } + + /** + * A shortcut for {@code yield(Function.identity())}. + * + * @return an {@code Iterator} of mapped results + */ + public Option yield() { + return yield(Function.identity()); + } + } + + /** + * For-comprehension with two Iterators. + */ + public static class For2Option { + + private final Option ts1; + private final Option ts2; + + private For2Option(Option ts1, Option ts2) { + this.ts1 = ts1; + this.ts2 = ts2; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(BiFunction f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.map(t2 -> f.apply(t1, t2))); + } + + } + + /** + * For-comprehension with three Iterators. + */ + public static class For3Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + + private For3Option(Option ts1, Option ts2, Option ts3) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function3 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.map(t3 -> f.apply(t1, t2, t3)))); + } + + } + + /** + * For-comprehension with 4 Iterators. + */ + public static class For4Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + private final Option ts4; + + private For4Option(Option ts1, Option ts2, Option ts3, Option ts4) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function4 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.map(t4 -> f.apply(t1, t2, t3, t4))))); + } + + } + + /** + * For-comprehension with 5 Iterators. + */ + public static class For5Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + private final Option ts4; + private final Option ts5; + + private For5Option(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function5 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); + } + + } + + /** + * For-comprehension with 6 Iterators. + */ + public static class For6Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + private final Option ts4; + private final Option ts5; + private final Option ts6; + + private For6Option(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function6 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); + } + + } + + /** + * For-comprehension with 7 Iterators. + */ + public static class For7Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + private final Option ts4; + private final Option ts5; + private final Option ts6; + private final Option ts7; + + private For7Option(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6, Option ts7) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function7 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); + } + + } + + /** + * For-comprehension with 8 Iterators. + */ + public static class For8Option { + + private final Option ts1; + private final Option ts2; + private final Option ts3; + private final Option ts4; + private final Option ts5; + private final Option ts6; + private final Option ts7; + private final Option ts8; + + private For8Option(Option ts1, Option ts2, Option ts3, Option ts4, Option ts5, Option ts6, Option ts7, Option ts8) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + this.ts8 = ts8; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Option yield(Function8 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.flatMap(t7 -> + ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); + } + + } + + /** + * For-comprehension with one Iterator. + */ + public static class For1Future { + + private final Future ts1; + + private For1Future(Future ts1) { + this.ts1 = ts1; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function f) { + Objects.requireNonNull(f, "f is null"); + return ts1.map(f); + } + + /** + * A shortcut for {@code yield(Function.identity())}. + * + * @return an {@code Iterator} of mapped results + */ + public Future yield() { + return yield(Function.identity()); + } + } + + /** + * For-comprehension with two Iterators. + */ + public static class For2Future { + + private final Future ts1; + private final Future ts2; + + private For2Future(Future ts1, Future ts2) { + this.ts1 = ts1; + this.ts2 = ts2; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(BiFunction f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.map(t2 -> f.apply(t1, t2))); + } + + } + + /** + * For-comprehension with three Iterators. + */ + public static class For3Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + + private For3Future(Future ts1, Future ts2, Future ts3) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function3 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.map(t3 -> f.apply(t1, t2, t3)))); + } + + } + + /** + * For-comprehension with 4 Iterators. + */ + public static class For4Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + private final Future ts4; + + private For4Future(Future ts1, Future ts2, Future ts3, Future ts4) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function4 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.map(t4 -> f.apply(t1, t2, t3, t4))))); + } + + } + + /** + * For-comprehension with 5 Iterators. + */ + public static class For5Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + private final Future ts4; + private final Future ts5; + + private For5Future(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function5 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); + } + + } + + /** + * For-comprehension with 6 Iterators. + */ + public static class For6Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + private final Future ts4; + private final Future ts5; + private final Future ts6; + + private For6Future(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function6 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); + } + + } + + /** + * For-comprehension with 7 Iterators. + */ + public static class For7Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + private final Future ts4; + private final Future ts5; + private final Future ts6; + private final Future ts7; + + private For7Future(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6, Future ts7) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function7 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); + } + + } + + /** + * For-comprehension with 8 Iterators. + */ + public static class For8Future { + + private final Future ts1; + private final Future ts2; + private final Future ts3; + private final Future ts4; + private final Future ts5; + private final Future ts6; + private final Future ts7; + private final Future ts8; + + private For8Future(Future ts1, Future ts2, Future ts3, Future ts4, Future ts5, Future ts6, Future ts7, Future ts8) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + this.ts8 = ts8; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Future yield(Function8 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.flatMap(t7 -> + ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); + } + + } + + /** + * For-comprehension with one Iterator. + */ + public static class For1Try { + + private final Try ts1; + + private For1Try(Try ts1) { + this.ts1 = ts1; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function f) { + Objects.requireNonNull(f, "f is null"); + return ts1.map(f); + } + + /** + * A shortcut for {@code yield(Function.identity())}. + * + * @return an {@code Iterator} of mapped results + */ + public Try yield() { + return yield(Function.identity()); + } + } + + /** + * For-comprehension with two Iterators. + */ + public static class For2Try { + + private final Try ts1; + private final Try ts2; + + private For2Try(Try ts1, Try ts2) { + this.ts1 = ts1; + this.ts2 = ts2; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(BiFunction f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.map(t2 -> f.apply(t1, t2))); + } + + } + + /** + * For-comprehension with three Iterators. + */ + public static class For3Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + + private For3Try(Try ts1, Try ts2, Try ts3) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function3 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.map(t3 -> f.apply(t1, t2, t3)))); + } + + } + + /** + * For-comprehension with 4 Iterators. + */ + public static class For4Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + private final Try ts4; + + private For4Try(Try ts1, Try ts2, Try ts3, Try ts4) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function4 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.map(t4 -> f.apply(t1, t2, t3, t4))))); + } + + } + + /** + * For-comprehension with 5 Iterators. + */ + public static class For5Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + private final Try ts4; + private final Try ts5; + + private For5Try(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function5 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); + } + + } + + /** + * For-comprehension with 6 Iterators. + */ + public static class For6Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + private final Try ts4; + private final Try ts5; + private final Try ts6; + + private For6Try(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function6 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); + } + + } + + /** + * For-comprehension with 7 Iterators. + */ + public static class For7Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + private final Try ts4; + private final Try ts5; + private final Try ts6; + private final Try ts7; + + private For7Try(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6, Try ts7) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function7 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); + } + + } + + /** + * For-comprehension with 8 Iterators. + */ + public static class For8Try { + + private final Try ts1; + private final Try ts2; + private final Try ts3; + private final Try ts4; + private final Try ts5; + private final Try ts6; + private final Try ts7; + private final Try ts8; + + private For8Try(Try ts1, Try ts2, Try ts3, Try ts4, Try ts5, Try ts6, Try ts7, Try ts8) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + this.ts8 = ts8; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public Try yield(Function8 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.flatMap(t7 -> + ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); + } + + } + + /** + * For-comprehension with one Iterator. + */ + public static class For1List { + + private final List ts1; + + private For1List(List ts1) { + this.ts1 = ts1; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function f) { + Objects.requireNonNull(f, "f is null"); + return ts1.map(f); + } + + /** + * A shortcut for {@code yield(Function.identity())}. + * + * @return an {@code Iterator} of mapped results + */ + public List yield() { + return yield(Function.identity()); + } + } + + /** + * For-comprehension with two Iterators. + */ + public static class For2List { + + private final List ts1; + private final List ts2; + + private For2List(List ts1, List ts2) { + this.ts1 = ts1; + this.ts2 = ts2; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(BiFunction f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.map(t2 -> f.apply(t1, t2))); + } + + } + + /** + * For-comprehension with three Iterators. + */ + public static class For3List { + + private final List ts1; + private final List ts2; + private final List ts3; + + private For3List(List ts1, List ts2, List ts3) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function3 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.map(t3 -> f.apply(t1, t2, t3)))); + } + + } + + /** + * For-comprehension with 4 Iterators. + */ + public static class For4List { + + private final List ts1; + private final List ts2; + private final List ts3; + private final List ts4; + + private For4List(List ts1, List ts2, List ts3, List ts4) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function4 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.map(t4 -> f.apply(t1, t2, t3, t4))))); + } + + } + + /** + * For-comprehension with 5 Iterators. + */ + public static class For5List { + + private final List ts1; + private final List ts2; + private final List ts3; + private final List ts4; + private final List ts5; + + private For5List(List ts1, List ts2, List ts3, List ts4, List ts5) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function5 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.map(t5 -> f.apply(t1, t2, t3, t4, t5)))))); + } + + } + + /** + * For-comprehension with 6 Iterators. + */ + public static class For6List { + + private final List ts1; + private final List ts2; + private final List ts3; + private final List ts4; + private final List ts5; + private final List ts6; + + private For6List(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function6 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.map(t6 -> f.apply(t1, t2, t3, t4, t5, t6))))))); + } + + } + + /** + * For-comprehension with 7 Iterators. + */ + public static class For7List { + + private final List ts1; + private final List ts2; + private final List ts3; + private final List ts4; + private final List ts5; + private final List ts6; + private final List ts7; + + private For7List(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6, List ts7) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function7 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.map(t7 -> f.apply(t1, t2, t3, t4, t5, t6, t7)))))))); + } + + } + + /** + * For-comprehension with 8 Iterators. + */ + public static class For8List { + + private final List ts1; + private final List ts2; + private final List ts3; + private final List ts4; + private final List ts5; + private final List ts6; + private final List ts7; + private final List ts8; + + private For8List(List ts1, List ts2, List ts3, List ts4, List ts5, List ts6, List ts7, List ts8) { + this.ts1 = ts1; + this.ts2 = ts2; + this.ts3 = ts3; + this.ts4 = ts4; + this.ts5 = ts5; + this.ts6 = ts6; + this.ts7 = ts7; + this.ts8 = ts8; + } + + /** + * Yields a result for elements of the cross product of the underlying Iterables. + * + * @param f a function that maps an element of the cross product to a result + * @param type of the resulting {@code Iterator} elements + * @return an {@code Iterator} of mapped results + */ + public List yield(Function8 f) { + Objects.requireNonNull(f, "f is null"); + return + ts1.flatMap(t1 -> + ts2.flatMap(t2 -> + ts3.flatMap(t3 -> + ts4.flatMap(t4 -> + ts5.flatMap(t5 -> + ts6.flatMap(t6 -> + ts7.flatMap(t7 -> + ts8.map(t8 -> f.apply(t1, t2, t3, t4, t5, t6, t7, t8))))))))); + } + + } + // // Structural Pattern Matching // diff --git a/vavr/src-gen/test/java/io/vavr/APITest.java b/vavr/src-gen/test/java/io/vavr/APITest.java index 1afb30e186..2bf37005e4 100644 --- a/vavr/src-gen/test/java/io/vavr/APITest.java +++ b/vavr/src-gen/test/java/io/vavr/APITest.java @@ -29,7 +29,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; -import io.vavr.collection.CharSeq; import io.vavr.collection.List; import io.vavr.collection.Map; import io.vavr.concurrent.Future; @@ -964,7 +963,7 @@ public void shouldIterateFor1UsingSimpleYield() { } @Test - public void shouldIterateFor1() { + public void shouldIterateForList1() { final List result = For( List.of(1, 2, 3) ).yield(i1 -> i1).toList(); @@ -974,7 +973,7 @@ public void shouldIterateFor1() { } @Test - public void shouldIterateFor2() { + public void shouldIterateForList2() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3) @@ -985,7 +984,7 @@ public void shouldIterateFor2() { } @Test - public void shouldIterateFor3() { + public void shouldIterateForList3() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -997,7 +996,7 @@ public void shouldIterateFor3() { } @Test - public void shouldIterateFor4() { + public void shouldIterateForList4() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -1010,7 +1009,7 @@ public void shouldIterateFor4() { } @Test - public void shouldIterateFor5() { + public void shouldIterateForList5() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -1024,7 +1023,7 @@ public void shouldIterateFor5() { } @Test - public void shouldIterateFor6() { + public void shouldIterateForList6() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -1039,7 +1038,7 @@ public void shouldIterateFor6() { } @Test - public void shouldIterateFor7() { + public void shouldIterateForList7() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -1055,7 +1054,7 @@ public void shouldIterateFor7() { } @Test - public void shouldIterateFor8() { + public void shouldIterateForList8() { final List result = For( List.of(1, 2, 3), List.of(1, 2, 3), @@ -1071,11 +1070,287 @@ public void shouldIterateFor8() { assertThat(result.last()).isEqualTo(3 * 8); } + @Test + public void shouldIterateForOption1() { + final Option result = For( + Option.of(1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } + + @Test + public void shouldIterateForOption2() { + final Option result = For( + Option.of(1), + Option.of(2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } + + @Test + public void shouldIterateForOption3() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } + + @Test + public void shouldIterateForOption4() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } + + @Test + public void shouldIterateForOption5() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } + + @Test + public void shouldIterateForOption6() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } + + @Test + public void shouldIterateForOption7() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6), + Option.of(7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } + + @Test + public void shouldIterateForOption8() { + final Option result = For( + Option.of(1), + Option.of(2), + Option.of(3), + Option.of(4), + Option.of(5), + Option.of(6), + Option.of(7), + Option.of(8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } + + @Test + public void shouldIterateForFuture1() { + final Future result = For( + Future.of(() -> 1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } + + @Test + public void shouldIterateForFuture2() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } + + @Test + public void shouldIterateForFuture3() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } + + @Test + public void shouldIterateForFuture4() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } + + @Test + public void shouldIterateForFuture5() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } + + @Test + public void shouldIterateForFuture6() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } + + @Test + public void shouldIterateForFuture7() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6), + Future.of(() -> 7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } + + @Test + public void shouldIterateForFuture8() { + final Future result = For( + Future.of(() -> 1), + Future.of(() -> 2), + Future.of(() -> 3), + Future.of(() -> 4), + Future.of(() -> 5), + Future.of(() -> 6), + Future.of(() -> 7), + Future.of(() -> 8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } + + @Test + public void shouldIterateForTry1() { + final Try result = For( + Try.of(() -> 1) + ).yield(i1 -> i1); + assertThat(result.get()).isEqualTo(1); + } + + @Test + public void shouldIterateForTry2() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2) + ).yield((i1, i2) -> i1 + i2); + assertThat(result.get()).isEqualTo(3); + } + + @Test + public void shouldIterateForTry3() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3) + ).yield((i1, i2, i3) -> i1 + i2 + i3); + assertThat(result.get()).isEqualTo(6); + } + + @Test + public void shouldIterateForTry4() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4) + ).yield((i1, i2, i3, i4) -> i1 + i2 + i3 + i4); + assertThat(result.get()).isEqualTo(10); + } + + @Test + public void shouldIterateForTry5() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5) + ).yield((i1, i2, i3, i4, i5) -> i1 + i2 + i3 + i4 + i5); + assertThat(result.get()).isEqualTo(15); + } + + @Test + public void shouldIterateForTry6() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6) + ).yield((i1, i2, i3, i4, i5, i6) -> i1 + i2 + i3 + i4 + i5 + i6); + assertThat(result.get()).isEqualTo(21); + } + + @Test + public void shouldIterateForTry7() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6), + Try.of(() -> 7) + ).yield((i1, i2, i3, i4, i5, i6, i7) -> i1 + i2 + i3 + i4 + i5 + i6 + i7); + assertThat(result.get()).isEqualTo(28); + } + + @Test + public void shouldIterateForTry8() { + final Try result = For( + Try.of(() -> 1), + Try.of(() -> 2), + Try.of(() -> 3), + Try.of(() -> 4), + Try.of(() -> 5), + Try.of(() -> 6), + Try.of(() -> 7), + Try.of(() -> 8) + ).yield((i1, i2, i3, i4, i5, i6, i7, i8) -> i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8); + assertThat(result.get()).isEqualTo(36); + } + @Test public void shouldIterateNestedFor() { final List result = For(Arrays.asList(1, 2), i -> - For(CharSeq.of('a', 'b')).yield(c -> i + ":" + c)).toList(); + For(List.of('a', 'b')).yield(c -> i + ":" + c)).toList(); assertThat(result).isEqualTo(List.of("1:a", "1:b", "2:a", "2:b")); }