Functional Programming in the Fantasy Land

Marius Jurgelenas & Karolis Masiulis

VilniusJs

2018

(x) => x

Fantasy Land

  • Method Signatures
  • Laws
  • Constraints

https://github.com/fantasyland/fantasy-land

Specified in JavaScript by Fantasy Land spec

Functor

Applicative

Apply

Chain

Monad

Method signatures

Laws

map :: Functor f => f a ~> (a -> b) -> f b
u.map(a => a) === u
u.map(x => f(g(x))) === u.map(g).map(f)

Identity

Composition

Functor

Extended Hindley-Milner type system

[1,2,3].map(x => x + 1)  // [2,3,4]

Constraints

  1. f must be a function,
    • If f is not a function, the behaviour of map is unspecified.
    • f can return any value.
    • No parts of f's return value should be checked.
  2. map must return a value of the same Functor
u.map(f)

Functor

Functor

Applicative

Apply

Chain

Monad

Method signature

of :: Applicative f => a -> f a

A value which has an Applicative must provide an of function on its type representative. The of function takes one argument:

F.of(a)

Applicative

Functor

Applicative

Apply

Chain

Monad

Method signature

ap :: Apply f => f a ~> f (a -> b) -> f b

Apply

map :: Functor f => f a ~>   (a -> b) -> f b
ap  ::   Apply f => f a ~> f (a -> b) -> f b

Functor vs Apply

Functor

Applicative

Apply

Chain

Monad

Method signature

chain :: Chain f => f a ~> (a -> f b) -> f b

Chain

map :: Functor f => f a ~>   (a -> b) -> f b
ap ::   Apply f => f a ~> f (a -> b) -> f b

Functor vs Apply vs Chain

chain ::   Chain f => f a ~> (a -> f b) -> f b

Functor

Applicative

Apply

Chain

Monad

Monad

A Monad must implement the Applicative and Chain specifications.

There is one operation to manipulate values of the monad (chain), and an operation to put values into a monad (of)

Thank you!

Functional Programming in the Fantasy Land

By Marius Jurgelenas

Functional Programming in the Fantasy Land

Presentation for VilniusJS

  • 431