Created
February 7, 2013 05:17
-
-
Save puffnfresh/4728748 to your computer and use it in GitHub Desktop.
jQuery.Deferred's monad
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
(function() { | |
function bind(d, f) { | |
return d.pipe(f) | |
} | |
function point(a) { | |
var d = $.Deferred(); | |
d.resolve(a); | |
return d.promise(); | |
} | |
function join(d) { | |
return bind(d, function(a) { | |
return a; | |
}); | |
} | |
var inner = point("inner"), | |
nested = point(inner); | |
bind(nested, function(a) { | |
alert(a); // [object Object] | |
}); | |
bind(join(nested), function(a) { | |
alert(a); // inner | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes more sense to me than the other deferreds-as-a-monad examples I've seen, but now I'm curious what the adjunction is and why it's interesting. It looks like it's just the identity functor (which is a monad).