Last active
May 8, 2017 07:17
-
-
Save emilypi/1b800c6750e10c4d8896752368f141b6 to your computer and use it in GitHub Desktop.
Higher-Functors of degree 0-3. Natural transformations are lax 2-Functors. Modifications lax 3-Functors
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
package misc | |
import scala.language.higherKinds | |
import scala.util.{Try, Success} | |
import scala.annotation.tailrec | |
object Kinds extends App { | |
trait Functor[F[_]] { | |
def pure[A](a: A): F[A] | |
def fmap[A, B](fa: F[A])(f: A => F[B]): F[B] | |
} | |
type >>[F[_]] = Functor[F] | |
trait NaturalTransformation[F[_], G[_]] { | |
def transform[A](fa: F[A]): G[A] | |
} | |
type ~>[F[_], G[_]] = NaturalTransformation[F, G] | |
class Modification[NT[_[_], _[_]], MT[_[_], _[_]]] | |
type ~>>[NT[_[_], _[_]], MT[_[_], _[_]]] = Modification[NT, MT] | |
/* Instances */ | |
//Functor instance - note the standard map | |
/** | |
* Natural Transformation examples | |
*/ | |
val nt = new (Option ~> List) { | |
def transform[A](o: Option[A]) = | |
o match { | |
case Some(v) => v :: Nil | |
case _ => Nil | |
} | |
} | |
val mt = new (Try ~> Set) { | |
def transform[A](t: Try[A]) = | |
t match { | |
case Success(v) => Set(v) | |
case _ => Set() | |
} | |
} | |
/** | |
* The question is how to instantiate Modifications. Is this the limit | |
* to Scala's typeclassing abilities? | |
* | |
*/ | |
val modification = new (~> ~>> ~>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment