diff --git a/generator/Generator.scala b/generator/Generator.scala index c8c1f3a41f..1094f7b061 100644 --- a/generator/Generator.scala +++ b/generator/Generator.scala @@ -792,7 +792,11 @@ def generateMainClasses(): Unit = { val params = (1 to i).gen(j => s"_$j")(", ") val paramTypes = (1 to i).gen(j => s"? super T$j")(", ") val resultGenerics = if (i == 0) "" else s"<${(1 to i).gen(j => s"U$j")(", ")}>" - val flatMapResultGenerics = if (i == 0) "" else s"<${(1 to i).gen(j => s"U$j")(", ")}>" + val mapResult = i match { + case 0 => "" + case 1 => "? extends U1" + case _ => s"Tuple$i<${(1 to i).gen(j => s"U$j")(", ")}>" + } val comparableGenerics = if (i == 0) "" else s"<${(1 to i).gen(j => s"U$j extends Comparable")(", ")}>" val untyped = if (i == 0) "" else s"<${(1 to i).gen(j => "?")(", ")}>" val functionType = i match { @@ -918,21 +922,26 @@ def generateMainClasses(): Unit = { } """)("\n\n")} - ${(i > 1).gen(xs""" + ${(i > 0).gen(xs""" /$javadoc * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function ${(1 to i).gen(j => s"* @param new type of the ${j.ordinal} component")("\n")} * @return A new Tuple of same arity. * @throws NullPointerException if {@code mapper} is null */ - public $resultGenerics $className$resultGenerics map($functionType<$paramTypes, $className$flatMapResultGenerics> mapper) { + public $resultGenerics $className$resultGenerics map($functionType<$paramTypes, $mapResult> mapper) { Objects.requireNonNull(mapper, "mapper is null"); - return mapper.apply($params); + ${if (i == 1) + "return Tuple.of(mapper.apply(_1));" + else + s"return mapper.apply($params);" + } } """)} - ${(i > 0).gen(xs""" + ${(i > 1).gen(xs""" /$javadoc * Maps the components of this tuple using a mapper function for each component. ${(0 to i).gen(j => if (j == 0) "*" else s"* @param f$j the mapper function of the ${j.ordinal} component")("\n")} @@ -946,6 +955,21 @@ def generateMainClasses(): Unit = { } """)} + ${(i > 1) gen (1 to i).gen(j => xs""" + /$javadoc + * Maps the ${j.ordinal} component of this tuple to a new value. + * + * @param new type of the ${j.ordinal} component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted ${j.ordinal} component + */ + public $className<${(1 to i).gen(k => if (j == k) "U" else s"T$k")(", ")}> map$j(${im.getType("java.util.function.Function")} mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_$j); + return Tuple.of(${(1 to i).gen(k => if (j == k) "u" else s"_$k")(", ")}); + } + """)("\n\n")} + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple1.java b/src-gen/main/java/javaslang/Tuple1.java index 273921872d..e571226815 100644 --- a/src-gen/main/java/javaslang/Tuple1.java +++ b/src-gen/main/java/javaslang/Tuple1.java @@ -87,16 +87,16 @@ public T1 _1() { } /** - * Maps the components of this tuple using a mapper function for each component. + * Maps the components of this tuple using a mapper function. * - * @param f1 the mapper function of the 1st component + * @param mapper the mapper function * @param new type of the 1st component * @return A new Tuple of same arity. - * @throws NullPointerException if one of the arguments is null + * @throws NullPointerException if {@code mapper} is null */ - public Tuple1 map(Function f1) { - Objects.requireNonNull(f1, "f1 is null"); - return Tuple.of(f1.apply(_1)); + public Tuple1 map(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + return Tuple.of(mapper.apply(_1)); } /** diff --git a/src-gen/main/java/javaslang/Tuple2.java b/src-gen/main/java/javaslang/Tuple2.java index 3d49a6a5cb..f1392654be 100644 --- a/src-gen/main/java/javaslang/Tuple2.java +++ b/src-gen/main/java/javaslang/Tuple2.java @@ -116,6 +116,7 @@ public T2 _2() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -143,6 +144,32 @@ public Tuple2 map(Function f1, Functi return Tuple.of(f1.apply(_1), f2.apply(_2)); } + /** + * Maps the 1st component of this tuple to a new value. + * + * @param new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple2 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple2 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple3.java b/src-gen/main/java/javaslang/Tuple3.java index cdaecf1784..e6e0f7d2b2 100644 --- a/src-gen/main/java/javaslang/Tuple3.java +++ b/src-gen/main/java/javaslang/Tuple3.java @@ -142,6 +142,7 @@ public T3 _3() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -173,6 +174,45 @@ public Tuple3 map(Function f1 return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3)); } + /** + * Maps the 1st component of this tuple to a new value. + * + * @param new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple3 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple3 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple3 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple4.java b/src-gen/main/java/javaslang/Tuple4.java index e253d5cd35..c76fc2a7c1 100644 --- a/src-gen/main/java/javaslang/Tuple4.java +++ b/src-gen/main/java/javaslang/Tuple4.java @@ -169,6 +169,7 @@ public T4 _4() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -204,6 +205,58 @@ public Tuple4 map(Function new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple4 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3, _4); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple4 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3, _4); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple4 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u, _4); + } + + /** + * Maps the 4th component of this tuple to a new value. + * + * @param new type of the 4th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 4th component + */ + public Tuple4 map4(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_4); + return Tuple.of(_1, _2, _3, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple5.java b/src-gen/main/java/javaslang/Tuple5.java index 7e6d349d0c..366d2fabb9 100644 --- a/src-gen/main/java/javaslang/Tuple5.java +++ b/src-gen/main/java/javaslang/Tuple5.java @@ -196,6 +196,7 @@ public T5 _5() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -235,6 +236,71 @@ public Tuple5 map(Function new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple5 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3, _4, _5); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple5 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3, _4, _5); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple5 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u, _4, _5); + } + + /** + * Maps the 4th component of this tuple to a new value. + * + * @param new type of the 4th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 4th component + */ + public Tuple5 map4(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_4); + return Tuple.of(_1, _2, _3, u, _5); + } + + /** + * Maps the 5th component of this tuple to a new value. + * + * @param new type of the 5th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 5th component + */ + public Tuple5 map5(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_5); + return Tuple.of(_1, _2, _3, _4, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple6.java b/src-gen/main/java/javaslang/Tuple6.java index 8e06ede1b3..98b9ee8b07 100644 --- a/src-gen/main/java/javaslang/Tuple6.java +++ b/src-gen/main/java/javaslang/Tuple6.java @@ -223,6 +223,7 @@ public T6 _6() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -266,6 +267,84 @@ public Tuple6 map(Function new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple6 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3, _4, _5, _6); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple6 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3, _4, _5, _6); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple6 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u, _4, _5, _6); + } + + /** + * Maps the 4th component of this tuple to a new value. + * + * @param new type of the 4th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 4th component + */ + public Tuple6 map4(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_4); + return Tuple.of(_1, _2, _3, u, _5, _6); + } + + /** + * Maps the 5th component of this tuple to a new value. + * + * @param new type of the 5th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 5th component + */ + public Tuple6 map5(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_5); + return Tuple.of(_1, _2, _3, _4, u, _6); + } + + /** + * Maps the 6th component of this tuple to a new value. + * + * @param new type of the 6th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 6th component + */ + public Tuple6 map6(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_6); + return Tuple.of(_1, _2, _3, _4, _5, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple7.java b/src-gen/main/java/javaslang/Tuple7.java index 784e76443a..8bc804c1e7 100644 --- a/src-gen/main/java/javaslang/Tuple7.java +++ b/src-gen/main/java/javaslang/Tuple7.java @@ -250,6 +250,7 @@ public T7 _7() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -297,6 +298,97 @@ public Tuple7 map(Funct return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3), f4.apply(_4), f5.apply(_5), f6.apply(_6), f7.apply(_7)); } + /** + * Maps the 1st component of this tuple to a new value. + * + * @param new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple7 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3, _4, _5, _6, _7); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple7 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3, _4, _5, _6, _7); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple7 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u, _4, _5, _6, _7); + } + + /** + * Maps the 4th component of this tuple to a new value. + * + * @param new type of the 4th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 4th component + */ + public Tuple7 map4(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_4); + return Tuple.of(_1, _2, _3, u, _5, _6, _7); + } + + /** + * Maps the 5th component of this tuple to a new value. + * + * @param new type of the 5th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 5th component + */ + public Tuple7 map5(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_5); + return Tuple.of(_1, _2, _3, _4, u, _6, _7); + } + + /** + * Maps the 6th component of this tuple to a new value. + * + * @param new type of the 6th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 6th component + */ + public Tuple7 map6(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_6); + return Tuple.of(_1, _2, _3, _4, _5, u, _7); + } + + /** + * Maps the 7th component of this tuple to a new value. + * + * @param new type of the 7th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 7th component + */ + public Tuple7 map7(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_7); + return Tuple.of(_1, _2, _3, _4, _5, _6, u); + } + /** * Transforms this tuple to an object of type U. * diff --git a/src-gen/main/java/javaslang/Tuple8.java b/src-gen/main/java/javaslang/Tuple8.java index 848096f5d9..ff9501736f 100644 --- a/src-gen/main/java/javaslang/Tuple8.java +++ b/src-gen/main/java/javaslang/Tuple8.java @@ -277,6 +277,7 @@ public T8 _8() { /** * Maps the components of this tuple using a mapper function. + * * @param mapper the mapper function * @param new type of the 1st component * @param new type of the 2nd component @@ -328,6 +329,110 @@ public Tuple8 m return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3), f4.apply(_4), f5.apply(_5), f6.apply(_6), f7.apply(_7), f8.apply(_8)); } + /** + * Maps the 1st component of this tuple to a new value. + * + * @param new type of the 1st component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 1st component + */ + public Tuple8 map1(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_1); + return Tuple.of(u, _2, _3, _4, _5, _6, _7, _8); + } + + /** + * Maps the 2nd component of this tuple to a new value. + * + * @param new type of the 2nd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 2nd component + */ + public Tuple8 map2(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_2); + return Tuple.of(_1, u, _3, _4, _5, _6, _7, _8); + } + + /** + * Maps the 3rd component of this tuple to a new value. + * + * @param new type of the 3rd component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 3rd component + */ + public Tuple8 map3(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_3); + return Tuple.of(_1, _2, u, _4, _5, _6, _7, _8); + } + + /** + * Maps the 4th component of this tuple to a new value. + * + * @param new type of the 4th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 4th component + */ + public Tuple8 map4(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_4); + return Tuple.of(_1, _2, _3, u, _5, _6, _7, _8); + } + + /** + * Maps the 5th component of this tuple to a new value. + * + * @param new type of the 5th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 5th component + */ + public Tuple8 map5(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_5); + return Tuple.of(_1, _2, _3, _4, u, _6, _7, _8); + } + + /** + * Maps the 6th component of this tuple to a new value. + * + * @param new type of the 6th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 6th component + */ + public Tuple8 map6(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_6); + return Tuple.of(_1, _2, _3, _4, _5, u, _7, _8); + } + + /** + * Maps the 7th component of this tuple to a new value. + * + * @param new type of the 7th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 7th component + */ + public Tuple8 map7(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_7); + return Tuple.of(_1, _2, _3, _4, _5, _6, u, _8); + } + + /** + * Maps the 8th component of this tuple to a new value. + * + * @param new type of the 8th component + * @param mapper A mapping function + * @return a new tuple based on this tuple and substituted 8th component + */ + public Tuple8 map8(Function mapper) { + Objects.requireNonNull(mapper, "mapper is null"); + final U u = mapper.apply(_8); + return Tuple.of(_1, _2, _3, _4, _5, _6, _7, u); + } + /** * Transforms this tuple to an object of type U. *