Skip to content

Instantly share code, notes, and snippets.

@wheaties
Created October 25, 2014 13:14
Show Gist options
  • Save wheaties/cb7f03aad225a2a7376d to your computer and use it in GitHub Desktop.
Save wheaties/cb7f03aad225a2a7376d to your computer and use it in GitHub Desktop.
Dependent Function1
trait DepFunction1[T]{ self =>
type R
def apply(t: T): R
def andThen(dep: DepFunction1[R]): Aux[T, dep.R] = new DepFunction1[T]{
type R = dep.R
def apply(t: T) = dep(self(t))
}
def compose[A](dep: Aux[A, T]): Aux[A, R] = dep andThen self
def andThen[A](f: R => A): Aux[T, A] = new DepFunction1[T]{
type R = A
def apply(t: T) = f(self(t))
}
def compose[A](f: A => T): Aux[A, R] = new DepFunction1[A]{
type R = self.R
def apply(a: A) = self(f(a))
}
}
object DepFunction1{
type Aux[T, R0] = DepFunction1[T]{ type R = R0 }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment