[Haskell-beginners] Monads in javascript

Thomas Davie tom.davie at gmail.com
Thu Jul 14 10:13:10 CEST 2011


On 14 Jul 2011, at 09:03, Mats Rauhala wrote:
> Could you give an example how CPS helped your situation? I'm trying to
> grok continuation passing style, but for now it just seems to complicate
> and/or slow down code (stack)

This obviously isn't Martin's example, but hopefully this should help.

In a 3D game engine, the norm (in procedural land at least) is to have calls like someInWorldObject.animate(someAnimation).  If you want to run that animation, and then another, the norm is to set a timer to fire when the animation is complete and to call another call like that.  My engine by comparison uses CPS – whenever you ask the engine to run an animation of some kind you must specify either a) that it repeats indefinitely, or b) has a continuation to deal with what happens after that animation is complete.  The result is that calling code doesn't have random timers floating around, and can define what happens at the completion of an animation in a closure, inline with creating that animation.

Apple's CoreAnimation also uses continuation passing style for a similar purpose.

Both of these examples use CPS because it allows things to happen asynchronously – an animation can disappear off and run, and then something come back to you once it's finished.  I have also found CPS can clean up synchronous code too.  Take a look at CPBrainfuck on hackage – I personally think it's one of the cleaner implementations of BF out there, though I will freely admit it's not the fastest.

Thanks

Tom Davie


More information about the Beginners mailing list