Skip to content

Commit f6eb444

Browse files
committed
Remove TupledFunction tuppled, apply, andThen and compose from stdlib
All these operations can and probably should be implemented in a library.
1 parent 5f07a17 commit f6eb444

7 files changed

+47
-68
lines changed

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,4 @@ object DottyPredef {
3838

3939
inline def the[T] given (x: T): x.type = x
4040

41-
/** Creates a tupled version of this function: instead of N arguments,
42-
* it accepts a single [[scala.Tuple]] argument.
43-
*
44-
* This is a generalization of [[scala.FunctionN.tupled]] that work on functions of any arity
45-
*
46-
* @tparam F the function type
47-
* @tparam Args the tuple type with the same types as the function arguments of F
48-
* @tparam R the return type of F
49-
*/
50-
def (f: F) tupled[F, Args <: Tuple, R] given (tupled: TupledFunction[F, Args => R]): Args => R = tupled(f)
51-
5241
}

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,3 @@ import scala.annotation.implicitNotFound
1111
trait TupledFunction[F, G] {
1212
def apply(f: F): G
1313
}
14-
15-
/** Module of TupledFunction containing methods for auto function tupling
16-
*
17-
* Usage
18-
* ```
19-
* val t2: (Int, Int) = ???
20-
* val t3: (Int, Int, Int) = ???
21-
* val f1: (Int, Int) => (Int, Int, Int) = ???
22-
* val f2: (Int, Int, Int) => (Int, Int) = ???
23-
*
24-
* import TupledFunction._
25-
* f1(t2)
26-
* f2(t3)
27-
* val f3: (Int, Int) => (Int, Int) = f1.andThen(f2)
28-
* val f4: (Int, Int, Int) => (Int, Int, Int) = f1.compose(f2)
29-
* ```
30-
*/
31-
object TupledFunction {
32-
33-
/** Apply this function to with each element of the tuple as a parameter
34-
*
35-
* @tparam F the function type
36-
* @tparam Args the tuple type with the same types as the function arguments of F
37-
* @tparam R the return type of F
38-
*/
39-
def (f: F) apply[F, Args <: Tuple, R](args: Args) given (tupled: TupledFunction[F, Args => R]): R =
40-
tupled(f)(args)
41-
42-
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last
43-
*
44-
* @tparam F a function type
45-
* @tparam G a function type
46-
* @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G
47-
* @tparam GArgs the tuple type with the same types as the function arguments of G
48-
* @tparam R the return type of F
49-
*/
50-
def (f: F) compose[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[G, GArgs => FArgs], TupledFunction[F, FArgs => R]: GArgs => R = {
51-
x => f(g(x))
52-
}
53-
54-
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first
55-
*
56-
* @tparam F a function type
57-
* @tparam G a function type
58-
* @tparam FArgs the tuple type with the same types as the function arguments of F
59-
* @tparam GArgs the tuple type with the same types as the function arguments of G and return type of F
60-
* @tparam R the return type of G
61-
*/
62-
def (f: F) andThen[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[F, FArgs => GArgs], TupledFunction[G, GArgs => R]: FArgs => R = {
63-
x => g(f(x))
64-
}
65-
66-
}

tests/run/tupled-function-andThen.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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
@@ -24,4 +23,17 @@ object Test {
2423
val h25 = f25.andThen(g25)
2524
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))
2625
}
26+
27+
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first
28+
*
29+
* @tparam F a function type
30+
* @tparam G a function type
31+
* @tparam FArgs the tuple type with the same types as the function arguments of F
32+
* @tparam GArgs the tuple type with the same types as the function arguments of G and return type of F
33+
* @tparam R the return type of G
34+
*/
35+
def (f: F) andThen[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given (tf: TupledFunction[F, FArgs => GArgs], tg: TupledFunction[G, GArgs => R]): FArgs => R = {
36+
x => tg(g)(tf(f)(x))
37+
}
38+
2739
}

tests/run/tupled-function.scala renamed to tests/run/tupled-function-apply.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3-
import TupledFunction._
43

54
val f0 = () => 0
65
val t0 = ()
@@ -107,4 +106,13 @@ object Test {
107106
println(f25(t25))
108107

109108
}
109+
110+
/** Apply this function to with each element of the tuple as a parameter
111+
*
112+
* @tparam F the function type
113+
* @tparam Args the tuple type with the same types as the function arguments of F
114+
* @tparam R the return type of F
115+
*/
116+
def (f: F) apply[F, Args <: Tuple, R](args: Args) given (tupled: TupledFunction[F, Args => R]): R =
117+
tupled(f)(args)
110118
}

tests/run/tupled-function-compose.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
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
@@ -21,8 +20,21 @@ object Test {
2120
val g25 =
2221
(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) =>
2322
(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)
24-
val h25 = f25.tupled.compose(g25.tupled)
23+
val h25 = f25.compose(g25)
2524
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))
2625

2726
}
27+
28+
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last
29+
*
30+
* @tparam F a function type
31+
* @tparam G a function type
32+
* @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G
33+
* @tparam GArgs the tuple type with the same types as the function arguments of G
34+
* @tparam R the return type of F
35+
*/
36+
def (f: F) compose[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given (tg: TupledFunction[G, GArgs => FArgs], tf: TupledFunction[F, FArgs => R]): GArgs => R = {
37+
x => tf(f)(tg(g)(x))
38+
}
39+
2840
}

tests/run/tupled-function-tupled.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,15 @@ object Test {
1414
println(f25.tupled(t25))
1515

1616
}
17+
18+
/** Creates a tupled version of this function: instead of N arguments,
19+
* it accepts a single [[scala.Tuple]] argument.
20+
*
21+
* This is a generalization of [[scala.FunctionN.tupled]] that work on functions of any arity
22+
*
23+
* @tparam F the function type
24+
* @tparam Args the tuple type with the same types as the function arguments of F
25+
* @tparam R the return type of F
26+
*/
27+
def (f: F) tupled[F, Args <: Tuple, R] given (tupled: TupledFunction[F, Args => R]): Args => R = tupled(f)
1728
}

0 commit comments

Comments
 (0)