Skip to content

Instantly share code, notes, and snippets.

@andyscott
Created November 6, 2017 00:32
Show Gist options
  • Save andyscott/876862296aff4bc98909954d374da2a6 to your computer and use it in GitHub Desktop.
Save andyscott/876862296aff4bc98909954d374da2a6 to your computer and use it in GitHub Desktop.
def foldTo[B]: CopFoldTo[B] = new CopFoldTo[B]
class CopFoldTo[B] {
def apply[L <: TList, LF <: TList](a: Cop[L])(fs: Prod[LF])(
implicit ev: TList.Compute.Aux[Op.Map[? => B, L], LF]
): B =
fs.values(a.index).asInstanceOf[Any => B].apply(a.value)
}
object Finn
object Cinder
object Bridger
type Dog = Cop[Finn.type :: Cinder.type :: Bridger.type :: TNil]
val dog0 = Finn.inject[Dog]
val dog1 = Cinder.inject[Dog]
val res = foldTo[String](dog0)(Prod(
(finn: Finn.type) => s"we got finn $finn",
(cinder: Cinder.type) => s"we got cinder $cinder",
(bridger: Bridger.type) => s"we got bridger $bridger"))
println(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment