4 ms·
Continuation-passing style in JavaScript (2010)
- bcg1 12y agoFWIW, GNU Guile lets you use "native" call/cc in its (broken?) JavaScript implementation. http://wingolog.org/archives/2009/02/25/callcc-and-ecmascript http://wingolog.org/archives/2009/02/25/callcc-and-ecmascrip...
- jonny_eh 12y agoSo how is this different than just using callbacks? Looks like you end up in callback hell. Why not move onto promises?
- myblake 12y agoIt's just a more organized way to use callbacks, but having spent several months on a project using CPS in node I will say it certainly helps add order and remove hellishness.
- PaulHoule 12y agoNeat article, but I'd like, for once, for articles about functional programming to NOT use the Fibonacci example because in terms of O(N) performance it is a clear example of malpractice. (i.e. if they were really math geniuses they'd just use the closed form)
- wging 12y agoIt is a simple problem to explain, and the goal may be to use it to explore novel programming paradigms rather than to solve it optimally. I am sure Matt Might knows about the efficient solutions to this problem. By the way, there are plenty of reasons not to use the closed form.
- asQuirreL 12y agoNobody's saying that the best way to implement the fibonacci sequence is using CPS, but in this instance it is a great example of what to do when converting a function with multiple recursive calls into CPS. Sometimes, if you want an example to be both small and instructive, you'll have to put up with some contrivances.
- wwweston 12y agoIn implementation, yes, you'd (probably) use the closed form. Wouldn't make for much discussion of various fine points of recursion, though. The purpose of these examples isn't to discuss efficient mathematical computation, it's to examine recursion. For pedagogy Fibonacci and Factorial are used so often because they're easily defined, often already understood by most with high school algebra under their belt, and pretty much the archetypical recurrence relations. Not a bad place to start exploring recursion. If you want people to move on, you're probably going to need to provide a better starting place for pedagogical purposes.
- PaulHoule 11y agoJust a different one. There are a lot of reasons why different programming communities don't listen to each other, and I find the fibonacci example to be a "bad smell".
- skibz 12y agoFrom the article, itself: "node.js is a high-performance, server-side platform for JavaScript in which blocking procedures are banned." Most of this sentence is correct, excpet for the final part. Blocking procedures are, in fact, permitted. They are simply discouraged.
- malkia 12y agoThis is a bit like saying cheaters are discouraged at casinos. In fact they are banned.
- billmalarky 12y agoThere are synchronous versions of many blocking async functions. There's rope there to hang yourself with.
- taternuts 11y agoA common example given for this is in node's `fs` class, which provides synchronous versions of many async calls like `readFile` -> `readFileSync`,
- deleted 12y ago[deleted]