Skip to content

Instantly share code, notes, and snippets.

@yellowflash
Last active March 11, 2016 19:02
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 yellowflash/bbdd8e68aca1e5cb1e7f to your computer and use it in GitHub Desktop.
Save yellowflash/bbdd8e68aca1e5cb1e7f to your computer and use it in GitHub Desktop.
trait Functor[T, F[_]] { self: F[T] =>
def map[K](fn: T => K): F[K]
}
sealed trait Optional[+T] extends Functor[T, Optional]
case class Maybe[T](value:T) extends Optional[T] {
override def map[K](fn: T => K) = Maybe(fn(value))
}
case object Nope extends Optional[Nothing] {
override def map[K](fn: _ => K) = Nope
}
//Well we typedef another type call it X and use it as param here (Bit ugly syntax).
case class Tuple2[A, B](first: A, second: B) extends Functor[B, ({type X[D] =Tuple2[A,D]})#X] {
override def map[C](fn: B => C): Tuple2[A, C] = Tuple2(first, fn(second))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment