Monad Haskell, Ruby, JS, ...
http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html http://adit.io/posts/2013-06-10-three-useful-monads.html
- Einheitsfunktion unit-function (Haskells return):
return :: a -> a
- bind-Operator (>>=)
(>>=) :: m a -> (a -> m b) -> m b
The OO transform: * bind(monad, func) * monad.bind(func)
- Kleisli-Operator(>=>)
(>=>) :: (a -> m b) -> (b -> m c) -> (a -> m c)
- Functor
fmap :: (a -> b) -> m a -> m b
- join
join :: m (m a) -> m a
-
empty
-
+
- fail
Axioms
https://wiki.haskell.org/Monad_laws
OO rewritting
-
unit(value).bind(f) `=== f(value)
-
monad.bind(unit) ==== monad
-
Composition:
monad.bind(f).bind(g) ==== monad.bind(function(value) { return f(value).bind(g); }) bind(bind(monad, f), f) monad.bind(f).bind(g)
Ruby
http://www.idryman.org/blog/2014/01/23/yet-another-monad-tutorial/
From Monads in Ruby, Part 1: Introduction
A monad, for our purposes, is made up of three things:
- A container type.
- the operation wrap: puts a single value in an instance of the container; Haskell calls this (confusingly) return
- the operation pass: extracts the values from the container and filters them through a function (we’ll use a block); Haskell calls this “bind” (spelt »=)
GoGaRuCo 2014- Refactoring Ruby with Monads http://codon.com/refactoring-ruby-with-monads https://github.com/tomstuart/monads
Javascript
http://igstan.ro/posts/2011-05-02-understanding-monads-with-javascript.html
Philip Roberts: What the heck is the event loop anyway? | JSConf EU 2014 Simulation on how the JS call stack and event queue work
function MONAD() {
return function unit(value) {
var monad = Object.create(null);
monad.bind = function (func) {
return func(value);
};
return monad;
};
}
var identify = MONAD();
var monad = identity("Hello World.");
monad.bind(alert);
Making ugly things like with promises:
getStudent(name, function student)) {
getLatestGrade(student, function (grade) {
console.log(grade);
logOut();
});
});
Look like that:
getStudent(name)
.then(getLatestGrade)
.then(console.log)
.catch(logError)
.then(logOut)
https://speakerdeck.com/kerrick/javascript-promises-thinking-sync-in-an-async-world
https://importantshock.wordpress.com/2009/01/18/jquery-is-a-monad/
http://morenews.blogspot.com.au/2015/02/jquery-still-not-monad.html
Haskell
[1,2,3] >>= (\ x -> [1,2,3] >>= (\y -> return (x/=y) >>= (\r -> case r of
True -> return (x,y)
_ -> fail "")))
Maybe Monad
Get rid of all the null checks…
Concurrency
Concurrency is trying to make lots of things happen at the same time. Currency is really hard. Threads are evil. Threads do not scale well. They are really hard to use. They race, they dead lock. There are alternative to threads - one of them is pure functional programming.
If there are no side effects nothing ever conflicts.
Problem - you have to deal with the real world.
More practical solution:
Turn based processing
- single-threaded - race free - deadlock free
-
the 3 laws: ** never wait ** never block ** finish fast
- events - message passing - no threads - no mutexes
-
usage: ** web browsers ** ui frameworks ** server - like node.js
- Asynchronicity can be hard to manage
The naive way is to write nested event handlers.
Promises / Futures
Promises are an excellent mechanism for managing asynchronicity. A promise is an object that represents a possible future value.
Every promise has a corresponding resolver that is used to ultimately assign a value to the promise.
A Promise has one of 3 states:
* pending
* kept / fullfilled: has a value
* broken / rejected: has a reason
pending ----> kept
\---> broken
A promise is an event generator. It fires its events when the value of the promise is ultimately known. At any time after the making the promise, event handling functions can be registered with the promise, which will be called in order with the promise’s value when it is known. A promise can accept functions that will be called
promise.when(success, failure) returns another promise for the result of your success function.
Make a vow:
var my_vow = VOW.make(); .make(value) .break(reason) .promise .when(kept, broken)
Other links
Further viewing
https://byorgey.wordpress.com/2009/01/12/abstraction-intuition-and-the-monad-tutorial-fallacy/