Skip to content

Instantly share code, notes, and snippets.

@mergeconflict
Created January 10, 2014 20:15
Show Gist options
  • Save mergeconflict/8361764 to your computer and use it in GitHub Desktop.
Save mergeconflict/8361764 to your computer and use it in GitHub Desktop.
¯\_(ツ)_/¯
// this is really "category" but omg math words
trait Stackable[S[_, _]] {
def compose[A, B, C](lhs: S[A, B], rhs: S[B, C]): S[A, C]
def id[A]: S[A, A]
}
object Stackable {
// for example ...
implicit val function1stackable = new Stackable[Function1] {
def compose[A, B, C](lhs: A => B, rhs: B => C): A => C = lhs andThen rhs
def id[A]: A => A = identity
}
}
class Stack[S[_, _], A, B](implicit stackable: Stackable[S]) {
val impl = // a heterogeneous collection of S[_, _] elements, where the first elem is S[A, _] and the last is S[_, B]
def build: S[A, B] = impl.foldLeft(stackable.id)(stackable.compose)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment