@@ -2,23 +2,64 @@ package scala
22
33import scala .annotation .implicitNotFound
44
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+ */
511@ implicitNotFound(" ${F} cannot be tupled as ${Args} => ${R}" )
612trait TupledFunction [F , Args <: Tuple , R ] {
713 def applyFunctionTo (f : F , args : Args ): R
814}
915
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+ */
1032object TupledFunction {
1133
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+ */
1340 def (f : F ) apply[F , Args <: Tuple , R ](args : Args ) given (tf : TupledFunction [F , Args , R ]): R =
1441 tf.applyFunctionTo(f, args)
1542
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+ */
1751 def (f : F ) compose[F , G , FArgs <: Tuple , GArgs <: Tuple , R ](g : G ) given TupledFunction [G , GArgs , FArgs ], TupledFunction [F , FArgs , R ]: GArgs => R = {
1852 x => f(g(x))
1953 }
2054
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+ */
2263 def (f : F ) andThen[F , G , FArgs <: Tuple , GArgs <: Tuple , R ](g : G ) given TupledFunction [F , FArgs , GArgs ], TupledFunction [G , GArgs , R ]: FArgs => R = {
2364 x => g(f(x))
2465 }
0 commit comments