Skip to content

Commit

Permalink
Remove TupledFunction apply, andThen and compose from stdlib
Browse files Browse the repository at this point in the history
All these operations can and probably should be implemented in a library.
  • Loading branch information
nicolasstucki committed May 28, 2019
1 parent 5f07a17 commit 664790c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 56 deletions.
53 changes: 0 additions & 53 deletions library/src-3.x/scala/TupledFunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,3 @@ import scala.annotation.implicitNotFound
trait TupledFunction[F, G] {
def apply(f: F): G
}

/** Module of TupledFunction containing methods for auto function tupling
*
* Usage
* ```
* val t2: (Int, Int) = ???
* val t3: (Int, Int, Int) = ???
* val f1: (Int, Int) => (Int, Int, Int) = ???
* val f2: (Int, Int, Int) => (Int, Int) = ???
*
* import TupledFunction._
* f1(t2)
* f2(t3)
* val f3: (Int, Int) => (Int, Int) = f1.andThen(f2)
* val f4: (Int, Int, Int) => (Int, Int, Int) = f1.compose(f2)
* ```
*/
object TupledFunction {

/** Apply this function to with each element of the tuple as a parameter
*
* @tparam F the function type
* @tparam Args the tuple type with the same types as the function arguments of F
* @tparam R the return type of F
*/
def (f: F) apply[F, Args <: Tuple, R](args: Args) given (tupled: TupledFunction[F, Args => R]): R =
tupled(f)(args)

/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last
*
* @tparam F a function type
* @tparam G a function type
* @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G
* @tparam GArgs the tuple type with the same types as the function arguments of G
* @tparam R the return type of F
*/
def (f: F) compose[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[G, GArgs => FArgs], TupledFunction[F, FArgs => R]: GArgs => R = {
x => f(g(x))
}

/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first
*
* @tparam F a function type
* @tparam G a function type
* @tparam FArgs the tuple type with the same types as the function arguments of F
* @tparam GArgs the tuple type with the same types as the function arguments of G and return type of F
* @tparam R the return type of G
*/
def (f: F) andThen[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given TupledFunction[F, FArgs => GArgs], TupledFunction[G, GArgs => R]: FArgs => R = {
x => g(f(x))
}

}
14 changes: 13 additions & 1 deletion tests/run/tupled-function-andThen.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
import TupledFunction._

val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
Expand All @@ -24,4 +23,17 @@ object Test {
val h25 = f25.andThen(g25)
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))
}

/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied first
*
* @tparam F a function type
* @tparam G a function type
* @tparam FArgs the tuple type with the same types as the function arguments of F
* @tparam GArgs the tuple type with the same types as the function arguments of G and return type of F
* @tparam R the return type of G
*/
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 = {
x => tg(g)(tf(f)(x))
}

}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
import TupledFunction._

val f0 = () => 0
val t0 = ()
Expand Down Expand Up @@ -107,4 +106,13 @@ object Test {
println(f25(t25))

}

/** Apply this function to with each element of the tuple as a parameter
*
* @tparam F the function type
* @tparam Args the tuple type with the same types as the function arguments of F
* @tparam R the return type of F
*/
def (f: F) apply[F, Args <: Tuple, R](args: Args) given (tupled: TupledFunction[F, Args => R]): R =
tupled(f)(args)
}
14 changes: 13 additions & 1 deletion tests/run/tupled-function-compose.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
import TupledFunction._

val f1 = (x1: Int, x2: Int) => (x1, x2, x1 + x2)
val g1 = (x1: Int, x2: Int, x3: Int) => x1 + x2 + x3
Expand All @@ -25,4 +24,17 @@ object Test {
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))

}

/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last
*
* @tparam F a function type
* @tparam G a function type
* @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G
* @tparam GArgs the tuple type with the same types as the function arguments of G
* @tparam R the return type of F
*/
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 = {
x => tf(f)(tg(g)(x))
}

}

0 comments on commit 664790c

Please sign in to comment.