Skip to content

Commit 9a92ec7

Browse files
committed
Add auto funtion tupled andThen and compose
1 parent c6aaaaf commit 9a92ec7

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ 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+
1626
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3+
import TupledFunction._
34

45
val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
56
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
6-
val h1 = f1.tupled.andThen(g1.tupled)
7+
val h1 = f1.andThen(g1)
78
println(h1(1, 2))
89

910
val f2 = (x1: Int, x2: Int) => (1, x1, x2, x1 + x2, x1 * x2)
1011
val g2 = (x1: Int, x2: Int, x3: Int, x4: Int, x5: Int) => (x1 + x2, x3 + x4 + x5)
11-
val h2 = f2.tupled.andThen(g2.tupled)
12+
val h2 = f2.andThen(g2)
1213
println(h2(1, 2))
1314

1415
val h3 = h2.andThen(h1)
@@ -20,8 +21,7 @@ object Test {
2021
val g25 =
2122
(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) =>
2223
(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+
val h25 = f25.andThen(g25)
2425
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-
2626
}
2727
}

tests/run/tupled-function-compose.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3+
import TupledFunction._
34

45
val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
56
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
6-
val h1 = g1.tupled.compose(f1.tupled)
7+
val h1 = g1.compose(f1)
78
println(h1(1, 2))
89

910
val f2 = (x1: Int, x2: Int) => (1, x1, x2, x1 + x2, x1 * x2)
1011
val g2 = (x1: Int, x2: Int, x3: Int, x4: Int, x5: Int) => (x1 + x2, x3 + x4 + x5)
11-
val h2 = g2.tupled.compose(f2.tupled)
12+
val h2 = g2.compose(f2)
1213
println(h2(1, 2))
1314

1415
val h3 = h1.compose(h2)

0 commit comments

Comments
 (0)