Skip to content

Commit

Permalink
Added component-wise Tuple map1..8 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietrich committed Jan 17, 2016
1 parent 9cb4ee4 commit e0a30c3
Show file tree
Hide file tree
Showing 9 changed files with 497 additions and 11 deletions.
34 changes: 29 additions & 5 deletions generator/Generator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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<? super U$j>")(", ")}>"
val untyped = if (i == 0) "" else s"<${(1 to i).gen(j => "?")(", ")}>"
val functionType = i match {
Expand Down Expand Up @@ -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 <U$j> 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")}
Expand All @@ -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 <U> 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 <U> $className<${(1 to i).gen(k => if (j == k) "U" else s"T$k")(", ")}> map$j(${im.getType("java.util.function.Function")}<? super T$j, ? extends U> 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.
*
Expand Down
12 changes: 6 additions & 6 deletions src-gen/main/java/javaslang/Tuple1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <U1> 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 <U1> Tuple1<U1> map(Function<? super T1, ? extends U1> f1) {
Objects.requireNonNull(f1, "f1 is null");
return Tuple.of(f1.apply(_1));
public <U1> Tuple1<U1> map(Function<? super T1, ? extends U1> mapper) {
Objects.requireNonNull(mapper, "mapper is null");
return Tuple.of(mapper.apply(_1));
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src-gen/main/java/javaslang/Tuple2.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public T2 _2() {

/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param <U1> new type of the 1st component
* @param <U2> new type of the 2nd component
Expand Down Expand Up @@ -143,6 +144,32 @@ public <U1, U2> Tuple2<U1, U2> map(Function<? super T1, ? extends U1> f1, Functi
return Tuple.of(f1.apply(_1), f2.apply(_2));
}

/**
* Maps the 1st component of this tuple to a new value.
*
* @param <U> new type of the 1st component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 1st component
*/
public <U> Tuple2<U, T2> map1(Function<? super T1, ? extends U> 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 <U> new type of the 2nd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 2nd component
*/
public <U> Tuple2<T1, U> map2(Function<? super T2, ? extends U> 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.
*
Expand Down
40 changes: 40 additions & 0 deletions src-gen/main/java/javaslang/Tuple3.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public T3 _3() {

/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param <U1> new type of the 1st component
* @param <U2> new type of the 2nd component
Expand Down Expand Up @@ -173,6 +174,45 @@ public <U1, U2, U3> Tuple3<U1, U2, U3> map(Function<? super T1, ? extends U1> 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 <U> new type of the 1st component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 1st component
*/
public <U> Tuple3<U, T2, T3> map1(Function<? super T1, ? extends U> 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 <U> new type of the 2nd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 2nd component
*/
public <U> Tuple3<T1, U, T3> map2(Function<? super T2, ? extends U> 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 <U> new type of the 3rd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 3rd component
*/
public <U> Tuple3<T1, T2, U> map3(Function<? super T3, ? extends U> 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.
*
Expand Down
53 changes: 53 additions & 0 deletions src-gen/main/java/javaslang/Tuple4.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public T4 _4() {

/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param <U1> new type of the 1st component
* @param <U2> new type of the 2nd component
Expand Down Expand Up @@ -204,6 +205,58 @@ public <U1, U2, U3, U4> Tuple4<U1, U2, U3, U4> map(Function<? super T1, ? extend
return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3), f4.apply(_4));
}

/**
* Maps the 1st component of this tuple to a new value.
*
* @param <U> new type of the 1st component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 1st component
*/
public <U> Tuple4<U, T2, T3, T4> map1(Function<? super T1, ? extends U> 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 <U> new type of the 2nd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 2nd component
*/
public <U> Tuple4<T1, U, T3, T4> map2(Function<? super T2, ? extends U> 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 <U> new type of the 3rd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 3rd component
*/
public <U> Tuple4<T1, T2, U, T4> map3(Function<? super T3, ? extends U> 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 <U> new type of the 4th component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 4th component
*/
public <U> Tuple4<T1, T2, T3, U> map4(Function<? super T4, ? extends U> 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.
*
Expand Down
66 changes: 66 additions & 0 deletions src-gen/main/java/javaslang/Tuple5.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public T5 _5() {

/**
* Maps the components of this tuple using a mapper function.
*
* @param mapper the mapper function
* @param <U1> new type of the 1st component
* @param <U2> new type of the 2nd component
Expand Down Expand Up @@ -235,6 +236,71 @@ public <U1, U2, U3, U4, U5> Tuple5<U1, U2, U3, U4, U5> map(Function<? super T1,
return Tuple.of(f1.apply(_1), f2.apply(_2), f3.apply(_3), f4.apply(_4), f5.apply(_5));
}

/**
* Maps the 1st component of this tuple to a new value.
*
* @param <U> new type of the 1st component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 1st component
*/
public <U> Tuple5<U, T2, T3, T4, T5> map1(Function<? super T1, ? extends U> 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 <U> new type of the 2nd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 2nd component
*/
public <U> Tuple5<T1, U, T3, T4, T5> map2(Function<? super T2, ? extends U> 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 <U> new type of the 3rd component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 3rd component
*/
public <U> Tuple5<T1, T2, U, T4, T5> map3(Function<? super T3, ? extends U> 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 <U> new type of the 4th component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 4th component
*/
public <U> Tuple5<T1, T2, T3, U, T5> map4(Function<? super T4, ? extends U> 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 <U> new type of the 5th component
* @param mapper A mapping function
* @return a new tuple based on this tuple and substituted 5th component
*/
public <U> Tuple5<T1, T2, T3, T4, U> map5(Function<? super T5, ? extends U> 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.
*
Expand Down
Loading

0 comments on commit e0a30c3

Please sign in to comment.