Skip to content

Commit 92c83c1

Browse files
committed
Simplify TupledFunction API
1 parent 6d8208e commit 92c83c1

6 files changed

+32
-23
lines changed

library/src-3.x/dotty/DottyPredef.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ object DottyPredef {
3737
}
3838

3939
inline def the[T] given (x: T): x.type = x
40+
41+
/** Creates a tupled version of this function: instead of N arguments,
42+
* it accepts a single [[scala.Tuple]] argument.
43+
*/
44+
def (f: F) tupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args, R]): Args => R = {
45+
x => tf.applyFunctionTo(f, x)
46+
}
47+
4048
}

library/src-3.x/scala/TupledFunction.scala

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,4 @@ object TupledFunction {
1313
def (f: F) apply[F, Args <: Tuple, R](args: Args) given (tf: TupledFunction[F, Args, R]): R =
1414
tf.applyFunctionTo(f, args)
1515

16-
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last */
17-
def (f: F) compose[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[G, GArgs, FArgs], TupledFunction[F, FArgs, R]: GArgs => R = {
18-
x => f(g(x))
19-
}
20-
21-
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first */
22-
def (f: F) andThen[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[F, FArgs, GArgs], TupledFunction[G, GArgs, R]: FArgs => R = {
23-
x => g(f(x))
24-
}
25-
26-
/** Creates a tupled version of this function: instead of N arguments,
27-
* it accepts a single [[scala.Tuple]] argument.
28-
*/
29-
def (f: F) tupled[F, Args <: Tuple, R] given TupledFunction[F, Args, R]: Args => R = {
30-
x => f(x)
31-
}
32-
3316
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
6
22
(2,7)
33
18
4+
(6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126,132,138,144,150)
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3-
import TupledFunction._
43

54
val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
65
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
7-
val h1 = f1.andThen(g1)
6+
val h1 = f1.tupled.andThen(g1.tupled)
87
println(h1(1, 2))
98

109
val f2 = (x1: Int, x2: Int) => (1, x1, x2, x1 + x2, x1 * x2)
1110
val g2 = (x1: Int, x2: Int, x3: Int, x4: Int, x5: Int) => (x1 + x2, x3 + x4 + x5)
12-
val h2 = f2.andThen(g2)
11+
val h2 = f2.tupled.andThen(g2.tupled)
1312
println(h2(1, 2))
1413

1514
val h3 = h2.andThen(h1)
1615
println(h3(1, 2))
1716

17+
val f25 =
18+
(x0: Int, x1: Int, x2: Int, x3: Int, x4: Int, x5: Int, x6: Int, x7: Int, x8: Int, x9: Int, x10: Int, x11: Int, x12: Int, x13: Int, x14: Int, x15: Int, x16: Int, x17: Int, x18: Int, x19: Int, x20: Int, x21: Int, x22: Int, x23: Int, x24: Int) =>
19+
(2 * x0, 2 * x1, 2 * x2, 2 * x3, 2 * x4, 2 * x5, 2 * x6, 2 * x7, 2 * x8, 2 * x9, 2 * x10, 2 * x11, 2 * x12, 2 * x13, 2 * x14, 2 * x15, 2 * x16, 2 * x17, 2 * x18, 2 * x19, 2 * x20, 2 * x21, 2 * x22, 2 * x23, 2 * x24)
20+
val g25 =
21+
(x0: Int, x1: Int, x2: Int, x3: Int, x4: Int, x5: Int, x6: Int, x7: Int, x8: Int, x9: Int, x10: Int, x11: Int, x12: Int, x13: Int, x14: Int, x15: Int, x16: Int, x17: Int, x18: Int, x19: Int, x20: Int, x21: Int, x22: Int, x23: Int, x24: Int) =>
22+
(3 * x0, 3 * x1, 3 * x2, 3 * x3, 3 * x4, 3 * x5, 3 * x6, 3 * x7, 3 * x8, 3 * x9, 3 * x10, 3 * x11, 3 * x12, 3 * x13, 3 * x14, 3 * x15, 3 * x16, 3 * x17, 3 * x18, 3 * x19, 3 * x20, 3 * x21, 3 * x22, 3 * x23, 3 * x24)
23+
val h25 = f25.tupled.andThen(g25.tupled)
24+
println(h25(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25))
25+
1826
}
1927
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
6
22
(2,7)
33
18
4+
(6,12,18,24,30,36,42,48,54,60,66,72,78,84,90,96,102,108,114,120,126,132,138,144,150)
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3-
import TupledFunction._
43

54
val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
65
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
7-
val h1 = g1.compose(f1)
6+
val h1 = g1.tupled.compose(f1.tupled)
87
println(h1(1, 2))
98

109
val f2 = (x1: Int, x2: Int) => (1, x1, x2, x1 + x2, x1 * x2)
1110
val g2 = (x1: Int, x2: Int, x3: Int, x4: Int, x5: Int) => (x1 + x2, x3 + x4 + x5)
12-
val h2 = g2.compose(f2)
11+
val h2 = g2.tupled.compose(f2.tupled)
1312
println(h2(1, 2))
1413

1514
val h3 = h1.compose(h2)
1615
println(h3(1, 2))
1716

17+
val f25 =
18+
(x0: Int, x1: Int, x2: Int, x3: Int, x4: Int, x5: Int, x6: Int, x7: Int, x8: Int, x9: Int, x10: Int, x11: Int, x12: Int, x13: Int, x14: Int, x15: Int, x16: Int, x17: Int, x18: Int, x19: Int, x20: Int, x21: Int, x22: Int, x23: Int, x24: Int) =>
19+
(2 * x0, 2 * x1, 2 * x2, 2 * x3, 2 * x4, 2 * x5, 2 * x6, 2 * x7, 2 * x8, 2 * x9, 2 * x10, 2 * x11, 2 * x12, 2 * x13, 2 * x14, 2 * x15, 2 * x16, 2 * x17, 2 * x18, 2 * x19, 2 * x20, 2 * x21, 2 * x22, 2 * x23, 2 * x24)
20+
val g25 =
21+
(x0: Int, x1: Int, x2: Int, x3: Int, x4: Int, x5: Int, x6: Int, x7: Int, x8: Int, x9: Int, x10: Int, x11: Int, x12: Int, x13: Int, x14: Int, x15: Int, x16: Int, x17: Int, x18: Int, x19: Int, x20: Int, x21: Int, x22: Int, x23: Int, x24: Int) =>
22+
(3 * x0, 3 * x1, 3 * x2, 3 * x3, 3 * x4, 3 * x5, 3 * x6, 3 * x7, 3 * x8, 3 * x9, 3 * x10, 3 * x11, 3 * x12, 3 * x13, 3 * x14, 3 * x15, 3 * x16, 3 * x17, 3 * x18, 3 * x19, 3 * x20, 3 * x21, 3 * x22, 3 * x23, 3 * x24)
23+
val h25 = f25.tupled.compose(g25.tupled)
24+
println(h25(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25))
25+
1826
}
1927
}

0 commit comments

Comments
 (0)