Hacker Newsnew | comments | show | ask | jobs | submit login

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}
    
    lift(f)(0)
    .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() )
    .then(function(f2result){
        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

Search: