Skip to content

Commit 664790c

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

5 files changed

+35
-56
lines changed

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: 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
@@ -25,4 +24,17 @@ object Test {
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
}

0 commit comments

Comments
 (0)