Skip to content

Commit

Permalink
Add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed May 27, 2019
1 parent 9a92ec7 commit 15aa152
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
8 changes: 6 additions & 2 deletions library/src-3.x/dotty/DottyPredef.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ object DottyPredef {
inline def the[T] given (x: T): x.type = x

/** Creates a tupled version of this function: instead of N arguments,
* it accepts a single [[scala.Tuple]] argument.
*/
* it accepts a single [[scala.Tuple]] argument.
*
* @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) tupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args, R]): Args => R = {
x => tf.applyFunctionTo(f, x)
}
Expand Down
31 changes: 28 additions & 3 deletions library/src-3.x/scala/TupledFunction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,48 @@ package scala

import scala.annotation.implicitNotFound

/** Type class relating a `FunctionN[..., R]` with an equvalent tupled function `Function1[TupleN[...], R]`
*
* @tparam F a function type
* @tparam Args a tuple type with the same types as the function arguments of F
* @tparam R the return type of F
*/
@implicitNotFound("${F} cannot be tupled as ${Args} => ${R}")
trait TupledFunction[F, Args <: Tuple, R] {
def applyFunctionTo(f: F, args: Args): R
}

object TupledFunction {

/** Apply this function to with each element of the tuple as a parameter */
/** 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 (tf: TupledFunction[F, Args, R]): R =
tf.applyFunctionTo(f, args)

/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last */
/** 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 */
/** 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))
}
Expand Down

0 comments on commit 15aa152

Please sign in to comment.