Skip to content

Instantly share code, notes, and snippets.

@huijari huijari/monad$.js
Last active Sep 4, 2017

Embed
What would you like to do?
import {
from,
just,
} from 'comea'
const log = label => value => console.log(label, value)
const range = x => from(Array(x).fill(0).map((_, k) => k + 1))
const bind = (base, f) => next
base(data => f(data)(next))
// f x == just x >>= f
const first = _ => {
const x = 3
range(x)(log('first'))
bind(just(x), range)(log('first*'))
}
// m == m >>= just
const second = _ => {
const o = from([1, 2, 3])
o(log('second'))
bind(o, just)(log('second*'))
}
// m >>= (x => f(x) >>= h) == m >>= f >>= h
const third = _ => {
const o = from([1, 2, 3])
bind(o, x => bind(range(x), range))(log('third'))
bind(bind(o, range), range)(log('third*'))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.