Last active
November 13, 2017 14:34
-
-
Save mandubian/b10513c3b8daac861535bb52c092d92e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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