Skip to content

Instantly share code, notes, and snippets.

@mandubian
Last active November 13, 2017 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandubian/b10513c3b8daac861535bb52c092d92e to your computer and use it in GitHub Desktop.
Save mandubian/b10513c3b8daac861535bb52c092d92e to your computer and use it in GitHub Desktop.
// We can write this type class parameterized by the morphism and a type A
trait CCCNumExt[->[_, _], A] {
def negateC: A -> A
def addC: (A, A) -> A
def mulC: (A, A) -> A
}
// and implement it for Function1 and any Numeric A
implicit def Function1CCCNumExt[A](implicit N: Numeric[A]): CCCNumExt[Function1, A] = new CCCNumExt[Function1, A] {
def negateC: A => A = N.negate _
def addC: ((A, A)) => A = { case (a, b) => N.plus(a, b) }
def mulC: ((A, A)) => A = { case (a, b) => N.times(a, b) }
}
// just note the trick: we put A in the trait type parameters
// to be able to constrain it later with Numeric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment