-
-
Save nlim/19fb7f4bc95ba8df1447 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import shims.{Permute2, Permute3} | |
import scalaz._ | |
object Scratch { | |
trait Functor[F[_]] { | |
def map[A, B](fa: F[A])(f: A => B): F[B] | |
} | |
object Functor { | |
def apply[F[_]](implicit F: Functor[F]) = F | |
implicit def listFunctor: Functor[List] = new Functor[List] { | |
def map[A, B](fa: List[A])(f: A => B): List[B] = fa map f | |
} | |
implicit def eitherFunctor[A]: Functor[A \/ ?] = new Functor[A \/ ?] { | |
def map[B, C](either: A \/ B)(f: B => C): A \/ C = either match { | |
case -\/(a) => -\/(a) | |
case \/-(b) => \/-(f(b)) | |
} | |
} | |
} | |
trait Unapply[E, TC[_[_]]] { | |
type A | |
type F[A] | |
def tc: TC[F] | |
def apply(e: E): F[A] | |
} | |
object Unapply { | |
type Aux[E, TC[_[_]], F0[_], A0] = Unapply[E, TC] { type A = A0; type F[A] = F0[A] } | |
implicit def materialize1[F0[_], TC[_[_]], A0](implicit tc0: TC[F0]): Unapply.Aux[F0[A0], TC, F0] = new Unapply[F0[A0], TC] { | |
type A = A0 | |
type F[A] = F0[A] | |
def tc = tc0 | |
def apply(e: F0[A]): F0[A] = e | |
} | |
implicit def materialize2[F01[_, _], F02[_, _], Z, TC[_[_]], A](implicit ev: Permute2[F01, F02], tc0: TC[F02[Z, ?]]): Unapply.Aux[F02[Z, A], TC, F02[Z, ?]] = new Unapply[F02[Z, A], TC] { | |
type F[A] = F02[Z, A] | |
def tc = tc0 | |
} | |
implicit def materialize3[F01[_, _, _], F02[_, _, _], Y, Z, TC[_[_]], A](implicit ev: Permute3[F01, F02], tc0: TC[F02[Y, Z, ?]]): Unapply.Aux[F02[Y, Z, A], TC, F02[Y, Z, ?]] = new Unapply[F02[Y, Z, A], TC] { | |
type F[A] = F02[Y, Z, A] | |
def tc = tc0 | |
} | |
} | |
def bar[E](fa: E)(implicit U: Unapply[E, Functor]) = U.tc.map(U(fa)) { _.hashCode } | |
// examples follow | |
bar(List(1, 2, 3)) | |
val e: String \/ Int = \/-(42) | |
bar(e) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment