Hacker News new | comments | show | ask | jobs | submit login

Yeah, just after I wrote that I realized that Deferreds/Promises are monads.

You don't have to convert them, but you do need a way to write code using those pure functions "inside" the monad which I don't think is easy with this model.

You should be able to just chain your actions together, with a wrapper function to turn pure functions into actions. I don't see why you would need to convert the actual pure functions to monadic style.

I guess what I had in my head when I made the comment was the convenience functions or the syntactic sugar around a monadic solution. Seems like the node guys are playing with things like this but all the solutions I've seen seem so ugly.

Pure code can be used in the monad just fine, (ie.: you can return values up to the monad and lift functions)

    //turn a function into a monadic action
    function lift(f){ return function(/**/){
        var d = new Deferred(); //Im using the Dojo API
        d.resolve( f.call(this, arguments) );
        return d.promise;
    var f = function(x){ return x+1}
    .then(function(y){ ... })
The problem is the opposite direction: pure code using monadic code and pure code turning into monadic code

If you have something like

    var x = f1( f2( f3() ) )
And f2 becomes a monadic action you have to rewrite this bit as

    var xPromise = f2( f3() )
        return f1(f2result);


ah, very nice, it is easy. The chaining is still fairly clunky though, although maybe comparing it to haskell's do notation syntactic sugar is unfair.

(isn't the opposite direction the feature of doing things this way? That you can't call monadic code from pure code is a good thing)


Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact