Hacker News new | ask | show | jobs
by ZenPsycho 5227 days ago
this is just a sketch to give you an idea of what I am talking about. right well you've probably used jquery before, so the api would look something like this. You start with a sequence of async actions you want to perform.

$(ctx).geturl("http://example.com).parseExample().renderTemplate().end();

Now this is a bit different from jQuery since all the methods here do is push method names and arguments onto a stack. it's the .end() method that reads in the stack and goes through and executes it. This means that each method along the way we've constructed a partial computation value- that is, the stack, which we can pass around as a value- just leave off the "end" method...

var scrapeHN = $(ctx).geturl("http://example.com).parseExample().renderTemplate();

now we can modify the computation by turning it into an error handling monad...

var ecm = ErrorCatchingMonad(scrapeHN, errorCallback);

this wraps each method in the monad with a try catch block, and calls the callback if an error gets thrown, and stops the chain from getting executed further. in fact this is how the origial monad was created

var $ = DeferringMonad(scrapingAPI);

which takes a normal jquery style plugin interface/chaining api and turns it into that deferring stack type api with an end(); method.