Skip to content

Instantly share code, notes, and snippets.

@lemastero
Last active January 28, 2019 23:47
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 lemastero/5bbc9ab83ffd82bda71aa86b4b7995c9 to your computer and use it in GitHub Desktop.
Save lemastero/5bbc9ab83ffd82bda71aa86b4b7995c9 to your computer and use it in GitHub Desktop.
What we say when we say F.. Yea! or F... No!
import scalaz.{Comonad, Monad}
import scalaz.{Arrow, Contravariant, Profunctor}
import scalaz.{Lan, Ran}
object AnyAndNothingInstances {
// Monad
// https://twitter.com/runarorama/status/1085383309969014784
type FYea[A] = Any
implicit val yeaMonad = new Monad[FYea] {
def bind[A, B](a: FYea[A])(f: A => FYea[B]): FYea[B] = a
def point[A](a: => A): FYea[A] = a
}
// Comonad
// https://twitter.com/runarorama/status/1085383912229142528
type FNo[A] = Nothing
implicit val noComonad = new Comonad[FNo] {
def copoint[A](a: FNo[A]): A = a
def cobind[A, B](a: FNo[A])(f: FNo[A] => B): FNo[B] = a
def map[A, B](a: FNo[A])(f: A => B): FNo[B] = a
}
// both are Contravariant
val noContravariant = new Contravariant[FNo] {
def contramap[A, B](a: FNo[A])(f: B => A): FNo[B] = a
}
val yeaContravariant = new Contravariant[FYea] {
def contramap[A, B](a: FYea[A])(f: B => A): FYea[B] = a
}
// both are Profunctor
type FFYea[F,G] = Any
type FFNo[F,G] = Nothing
val yeaPro = new Profunctor[FFYea] {
def mapfst[A, B, C](a: FFYea[A, B])(f: C => A): FFYea[C, B] = a
def mapsnd[A, B, C](a: FFYea[A, B])(f: B => C): FFYea[A, C] = a
}
val noPro = new Profunctor[FFNo] {
def mapfst[A, B, C](a: FFNo[A, B])(f: C => A): FFNo[C, B] = a
def mapsnd[A, B, C](a: FFNo[A, B])(f: B => C): FFNo[A, C] = a
}
// Arrows
val yeaArrow = new Arrow[FFYea] { // TODO is it lawefull ?
def arr[A, B](f: A => B): FFYea[A, B] = f
def first[A, B, C](f: FFYea[A, B]): FFYea[(A, C), (B, C)] = f
def id[A]: FFYea[A, A] = identity _
def compose[A, B, C](f: FFYea[B, C], g: FFYea[A, B]): FFYea[A, C] = f
}
// Kan extensions
def yeaKan[A] = new Ran[FYea, FYea, A] {
def apply[B](f: A => FYea[B]): FYea[B] = f
}
def yeaNoKan[A] = new Ran[FNo, FYea, A] {
def apply[B](f: A => FNo[B]): FYea[B] = f
}
def yeaLan[A] = new Lan[FNo, FYea, A] { // TODO does it makes sense ?
type I = Any
def v: FYea[I] = ()
def f(f: FNo[I]): A = f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment