Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Created June 29, 2018 06:50
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 afsalthaj/b658245bf47f647f02513213ce6aa719 to your computer and use it in GitHub Desktop.
Save afsalthaj/b658245bf47f647f02513213ce6aa719 to your computer and use it in GitHub Desktop.
// Write Json. In this case the type parameters in the type class
// came at the method parameters level (contra variant position), calling for having
// a `Contramap`
trait EncodeJson[A] { self =>
def toJson(a: A): Json
def contramap(f: B => A): EncodeJson[B] = new EncodeJson[B] {
def toJson(a: B): Json = self.toJson(f(b))
}
} // Implies EncodeJson can have an instance of contravariant functor
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment