Skip to content

Instantly share code, notes, and snippets.

@emilypi
Last active May 8, 2017 07:17
Show Gist options
  • Save emilypi/1b800c6750e10c4d8896752368f141b6 to your computer and use it in GitHub Desktop.
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
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