Last active
June 13, 2017 13:09
-
-
Save mmollaverdi/1f2716186a6aa5949b0786e2e24e3ce9 to your computer and use it in GitHub Desktop.
Implementation of Applicatives for functions with arity of 1 in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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