@@ -2,23 +2,64 @@ package scala
2
2
3
3
import scala .annotation .implicitNotFound
4
4
5
+ /** Type class relating a `FunctionN[..., R]` with an equvalent tupled function `Function1[TupleN[...], R]`
6
+ *
7
+ * @tparam F a function type
8
+ * @tparam Args a tuple type with the same types as the function arguments of F
9
+ * @tparam R the return type of F
10
+ */
5
11
@ implicitNotFound(" ${F} cannot be tupled as ${Args} => ${R}" )
6
12
trait TupledFunction [F , Args <: Tuple , R ] {
7
13
def applyFunctionTo (f : F , args : Args ): R
8
14
}
9
15
16
+ /** Module of TupledFunction containing methods for auto function tupling
17
+ *
18
+ * Usage
19
+ * ```
20
+ * val t2: (Int, Int) = ???
21
+ * val t3: (Int, Int, Int) = ???
22
+ * val f1: (Int, Int) => (Int, Int, Int) = ???
23
+ * val f2: (Int, Int, Int) => (Int, Int) = ???
24
+ *
25
+ * import TupledFunction._
26
+ * f1(t2)
27
+ * f2(t3)
28
+ * val f3: (Int, Int) => (Int, Int) = f1.andThen(f2)
29
+ * val f4: (Int, Int, Int) => (Int, Int, Int) = f1.compose(f2)
30
+ * ```
31
+ */
10
32
object TupledFunction {
11
33
12
- /** Apply this function to with each element of the tuple as a parameter */
34
+ /** Apply this function to with each element of the tuple as a parameter
35
+ *
36
+ * @tparam F the function type
37
+ * @tparam Args the tuple type with the same types as the function arguments of F
38
+ * @tparam R the return type of F
39
+ */
13
40
def (f : F ) apply[F , Args <: Tuple , R ](args : Args ) given (tf : TupledFunction [F , Args , R ]): R =
14
41
tf.applyFunctionTo(f, args)
15
42
16
- /** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last */
43
+ /** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last
44
+ *
45
+ * @tparam F a function type
46
+ * @tparam G a function type
47
+ * @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G
48
+ * @tparam GArgs the tuple type with the same types as the function arguments of G
49
+ * @tparam R the return type of F
50
+ */
17
51
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
52
x => f(g(x))
19
53
}
20
54
21
- /** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first */
55
+ /** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first
56
+ *
57
+ * @tparam F a function type
58
+ * @tparam G a function type
59
+ * @tparam FArgs the tuple type with the same types as the function arguments of F
60
+ * @tparam GArgs the tuple type with the same types as the function arguments of G and return type of F
61
+ * @tparam R the return type of G
62
+ */
22
63
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
64
x => g(f(x))
24
65
}
0 commit comments