Document parts of AList & components of TupleSyntax

Also, extract the AList.SplitK type lambda helper for call-site reuse.
This commit is contained in:
Dale Wijnand 2020-02-15 13:08:40 +00:00
parent 1111b3c581
commit 2608f8ade6
3 changed files with 10 additions and 5 deletions

View File

@ -223,8 +223,7 @@ object Instance {
def map[S, T](in: M[S], f: S => T): M[T] = a.map(in, (bv: B[S]) => b.map(bv, f))
def app[K[L[x]], Z](in: K[M], f: K[Id] => Z)(implicit alist: AList[K]): A[B[Z]] = {
val g: K[B] => B[Z] = in => b.app[K, Z](in, f)
type Split[L[x]] = K[(L B)#l]
a.app[Split, B[Z]](in, g)(AList.asplit(alist))
a.app[AList.SplitK[K, B]#l, B[Z]](in, g)(AList.asplit(alist))
}
}
}

View File

@ -91,9 +91,12 @@ object AList {
): N[P[A]] = f(a)
}
type ASplit[K[L[x]], B[x]] = AList[λ[L[x] => K[(L B)#l]]]
/** Example: calling `AList.SplitK[K, Task]#l` returns the type lambda `A[x] => K[A[Task[x]]`. */
sealed trait SplitK[K[L[x]], B[x]] { type l[A[x]] = K[(A B)#l] }
/** AList that operates on the outer type constructor `A` of a composition `[x] A[B[x]]` for type constructors `A` and `B`*/
type ASplit[K[L[x]], B[x]] = AList[SplitK[K, B]#l]
/** AList that operates on the outer type constructor `A` of a composition `[x] A[B[x]]` for type constructors `A` and `B`. */
def asplit[K[L[x]], B[x]](base: AList[K]): ASplit[K, B] = new ASplit[K, B] {
type Split[L[x]] = K[(L B)#l]

View File

@ -462,14 +462,17 @@ object Scoped {
) {
type App[T] = Initialize[Task[T]]
/** A higher-kinded function, where each parameter shares the same type constructor `M[_]`. */
type Fun[M[_], Ret]
/** Convert the higher-kinded function to a Function1. For tuples that means call `.tupled`. */
protected def convert[M[_], Ret](f: Fun[M, Ret]): K[M] => Ret
private[this] val inputs: K[App] = a.transform(keys, λ[ScopedTaskable ~> App](_.toTask))
private[this] def onTasks[T](f: K[Task] => Task[T]): App[T] =
Def.app[λ[L[x] => K[(L Task)#l]], Task[T]](inputs)(f)(AList.asplit[K, Task](a))
Def.app[AList.SplitK[K, Task]#l, Task[T]](inputs)(f)(AList.asplit[K, Task](a))
def flatMap[T](f: Fun[Id, Task[T]]): App[T] = onTasks(_.flatMap(convert(f)))
def flatMapR[T](f: Fun[Result, Task[T]]): App[T] = onTasks(_.flatMapR(convert(f)))