4 ms·
I realised yesterday that "yield" in Python (and I guess javascript) is kind of a baby continuation (http://en.wikipedia.org/wiki/Continuation http://en.wikiped
by AlexanderDhoore 13y ago
I realised yesterday that "yield" in Python (and I guess javascript) is kind of a baby continuation (http://en.wikipedia.org/wiki/Continuation http://en.wikipedia.org/wiki/Continuation). It can be used to create coroutines and turn async code into sync (looking) code. Really powerful.
Anyone interested should learn what continuations are (in Scheme).
- agumonkey 13y agoThere is some debate about continuations and generators : http://wingolog.org/archives/2013/02/25/on-generators http://wingolog.org/archives/2013/02/25/on-generators http://lambda-the-ultimate.org/node/4690 http://lambda-the-ultimate.org/node/4690
- dmunoz 13y agoA good resource for this is the control operations chapter [0] in Programming Languages: Application and Interpretation [1]. It start off with some motivation, then covers generators and continuations, with mention of threads and coroutines. The base motivation and most code is for an interpreter implementation, but it isn't impossible to follow if you're not familiar with such material. [0] http://cs.brown.edu/courses/cs173/2012/book/Control_Operations.html http://cs.brown.edu/courses/cs173/2012/book/Control_Operatio... [1] http://cs.brown.edu/courses/cs173/2012/book/index.html http://cs.brown.edu/courses/cs173/2012/book/index.html
- lhorie 13y agoI never got the point of generators until I saw Koa.js I always thought generators were merely a way of making convoluted iterators, but when I realized that you can put your application logic inside a generator, in my head, it immediately turned it into a "how did I ever code without this before" kind of feature. I just hope other browsers (especially IE) follow suit in implementing this feature
- apaprocki 13y agoWe (Bloomberg) sponsored Andy Wingo (Igalia) to do the work implementing the new generators in both V8 & SM in parallel, so the world would essentially get them in Chrome (+ Node, Opera) and Firefox at roughly the same time. We don't use JSC here, so I couldn't make a good case for extending the work to cover Safari as well. Anyone wanting to help implement them in JSC has two good blueprints to work from. I reached out to the IE team back when Andy was still early in the implementation and let them know what we were doing and what the release timeframe was in the hopes that it would help prioritize generators on their side, but I have yet to hear a peep about IE support. Maybe all we need are some benchmarks? :) edit: typo
- kybernetikos 13y agotaskjs is also an excellent use for generators http://taskjs.org/ http://taskjs.org/ It's also quite fun that you can embed a simple prolog in languages with generators fairly easily too: http://yieldprolog.sourceforge.net/ http://yieldprolog.sourceforge.net/
- sdegutis 13y agoCan you give an small concrete example of the new beneficial code that you realized you could do with it?
- _ZeD_ 13y agotake a look at http://www.dabeaz.com/coroutines/Coroutines.pdf http://www.dabeaz.com/coroutines/Coroutines.pdf
- anaphor 13y agoYou can also use continuations for implementing TCO (and a very coarse grained form of concurrency), see: http://www.cs.indiana.edu/hyplan/dfried/ts.ps http://www.cs.indiana.edu/hyplan/dfried/ts.ps
- handsomeransoms 13y agoThis property of ES6 generators, along with Promises, is what enables task.js [0] If you're interested in concurrency patterns for JS, you need to check it out. Does this mean that we can use Task.js in ordinary web pages now? [0] http://taskjs.org/ http://taskjs.org/