Skip to content

Instantly share code, notes, and snippets.

@mmollaverdi
Last active June 13, 2017 13:09
Show Gist options
  • Save mmollaverdi/1f2716186a6aa5949b0786e2e24e3ce9 to your computer and use it in GitHub Desktop.
Save mmollaverdi/1f2716186a6aa5949b0786e2e24e3ce9 to your computer and use it in GitHub Desktop.
Implementation of Applicatives for functions with arity of 1 in Javascript
// Read on Applicatives here: https://drboolean.gitbooks.io/mostly-adequate-guide/content/ch10.html
function function1(f) {
return {
ap: g => function1(x => f(x)(g.apply(x))),
apply: f
}
}
Function1 = {
of: a => function1(x => a) // aka `pure`
}
var res = Function1.of(a => b => a + b).ap(function1(x => x * 2)).ap(function1(x => x * 3))
res.apply(10)
// 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment