Created
August 4, 2015 07:04
-
-
Save ruxo/5e2f87dc6c34c2bd78b2 to your computer and use it in GitHub Desktop.
[sketch] Promise as functor in Javascript
This file contains hidden or 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
| var Promise__map = function(g){ // g: a -> b | |
| var _f = function(){}; | |
| var h = function(f){ // h : (b -> c) -> 0 | |
| _f = f; | |
| }; | |
| this.fp(function(a){ | |
| var b = g(a); | |
| _f(b); | |
| return b; | |
| }); | |
| return Promise(h); | |
| } | |
| var Promise = function(fp){ // fp : (a -> b) -> 0 | |
| return { fp: fp, subscribe: Promise__map }; | |
| }; | |
| x = [] | |
| function async(f) { x.push(f); } | |
| p = Promise(async); | |
| p1 = p.subscribe(function(n) { return "Number = " + n; }); | |
| p2 = p1.subscribe(console.log.bind(console)); | |
| x[0](123); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment