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

All good :)

Implementing exceptions with the continuation monad is straightforward. As you can see, that's sort of how it's used here. You can also recreate the State monad, Maybe, and others, deriving each from the continuation monad. I don't recommend this for actual use in JS, because JS lacks tail call optimization, which means any serious use of monads will cause stack overflows straight away.

But it gets crazier: you can implement the continuation monad using JavaScript exceptions. And JavaScript exceptions do cause the stack to reset (with a performance penalty). So if you derive a continuation monad in JS from its built-in exceptions mechanism, you can implement the other monads without breaking the stack.




You can also implement them with a setTimeout(0), which gives the browser's event loop a chance to run so that you don't lock up the browser forever. I did something like this with ArcLite, resetting the stack with a setTimeout(0) after ever top-level definition was parsed, so that it didn't take 30 seconds of unresponsive browser for the standard library to load. It occurred to me afterwards that I could've used this to implement continuations, but by then I was pretty much done with Arc development.


This has actually been done, in Inria's HOP project with Scheme->JS




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: