|
Scala example source code file (Representable.scala)
The Representable.scala Scala example source codepackage scalaz /** Representable functors, that is to say, those with isomorphisms to * and from `[a](X => a)`. As such, all typeclasses and operations on * `[a](X => a)`, that is, fixed in `X`, can be trivially derived for * `F`. */ abstract class Representable[F[_], X](implicit val F: Functor[F]) { def rep[A](f: X => A): F[A] def unrep[A](f: F[A]): X => A trait RepresentableLaw { /** `rep compose unrep` is vacuous. */ def repUnrep[A](f: F[A])(implicit E: Equal[F[A]]) = E.equal(rep(unrep(f)), f) /** `unrep compose rep` is vacuous. */ def unrepRep[A](f: X => A, x: X)(implicit E: Equal[A]) = E.equal(unrep(rep(f))(x), f(x)) } def representableLaw = new RepresentableLaw {} } sealed abstract class RepresentableInstances { import scalaz.std.function._ /** The identity representable. */ implicit def readerRepresentable[E]: Representable[E => ?, E] = new Representable[E => ?, E] { def rep[A](f: E => A) = f def unrep[A](f: E => A) = f } implicit def curryRepresentable[E]: Representable[E => ?, (E, Unit)] = new Representable[E => ?, (E, Unit)] { def rep[A](f: ((E, Unit)) => A): E => A = e => f((e, ())) def unrep[A](f: E => A): ((E, Unit)) => A = e => f(e._1) } implicit val f0Representable: Representable[Function0, Unit] = new Representable[Function0, Unit] { def rep[A](f: Unit => A) = () => f(()) def unrep[A](f: () => A) = u => f() } } object Representable extends RepresentableInstances /** Corepresentable functors */ abstract class Corepresentable[F[_], X](implicit F: Contravariant[F]) { def corep[A](f: A => X): F[A] def uncorep[A](f: F[A]): A => X trait CorepresentableLaw { def corepUncorep[A](f: F[A])(implicit E: Equal[F[A]]) = E.equal(corep(uncorep(f)), f) def uncorepCorep[A](f: A => X, a: A)(implicit E: Equal[X]) = E.equal(uncorep(corep(f))(a), f(a)) } def corepresentableLaw = new CorepresentableLaw {} } Other Scala examples (source code examples)Here is a short list of links related to this Scala Representable.scala source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.