Skip to content

Instantly share code, notes, and snippets.

@arosien
Last active December 14, 2015 03:49
Show Gist options
  • Select an option

  • Save arosien/5024141 to your computer and use it in GitHub Desktop.

Select an option

Save arosien/5024141 to your computer and use it in GitHub Desktop.
shapes
(A => B) => A => B // function
(A => B) => F[A] => F[B] // Functor.fmap
Z[A => B] => Z[A] => Z[B] // Applicative.apply
(A => M[B]) => M[A] => M[B] // Monad.bind
(A => B) apply A: B // Function1.apply
F[A] fmap (A => B): F[B] // Functor.fmap
Z[A] <*> Z[A => B]: Z[B] // Applicative.apply
M[A] >>= (A => M[B]): M[B] // Monad.bind
K[M, A, B] >=> (B => M[C]): K[M, A, C] // Kleisli compose
trait Functor[F[_]] {
def fmap[A, B](a: F[A], f: A => B): F[B]
}
trait Applicative[A[_]] extends Functor[A] {
def <*>[A, B](a: Z[A], f: Z[A => B]): Z[B]
}
val za: Z[A]
val zb: Z[B]
/*
zb <*> (za fmap f)
Z[B] <*> (Z[A] fmap (A => B => C))
Z[B] <*> (Z[B => C])
Z[C]
*/
val f: (A => B => C)
val zc: Z[C] = zb <*> (za fmap f)
/*
Order doesn't matter!
za <*> (zb fmap f2)
Z[A] <*> (Z[B] fmap (B => A => C))
Z[A] <*> (Z[A => C])
Z[C]
*/
val f2: (B => A => C)
val zc: Z[C] = za <*> (zb fmap f2)
/*
* Sources:
*
* Functors and things using Scala, http://blog.tmorris.net/posts/functors-and-things-using-scala/
* Lifting, http://blog.tmorris.net/posts/lifting/
*/
@tonymorris
Copy link
Copy Markdown

The fmap operation is sometimes called lift1 It corresponds to lifting an arity-1 function into an environment (F). See http://blog.tmorris.net/posts/lifting/

Lines 13-19 do not accurately represent the operation under examination, since they omit the initial argument (F[A]). The use of this is not constrained enough. However, they are on the right track and with some modification, can be made more accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment