-
Notifications
You must be signed in to change notification settings - Fork 2k
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Python-like generators and generator expressions #983
Comments
Or, you could simply do something like this:
Lazy evaluation, infinite sequences, and more functional programming, all without adding any new language features. ;) |
A good implementation of lazy sequences. But where's my generator expressions? :) I want to write stuff like |
All of the examples can be written in CoffeeScript already:
No need to have lazy generators. If you really need them, consider async callbacks. I'd be interested to see any examples where generators would be useful and the current syntax or continuations can't deal with the code in an elegant way. |
Well, I for one think it would be fantastic if Coffeescript's list comprehensions were lazy and worked with user-defined types. While we're at it, how about a pony? |
Nah, I like turtles. |
Sorry, but I'm afraid you can't base CoffeeScript around generators and lazy evaluation, given that JavaScript eagerly evaluates everything (as it probably should). Making a change like this would make CoffeeScript libraries far more incompatible with JS libs than they need to be, and would slow down general iteration by an order of magnitude. So, I'm afraid this particular ticket is a
|
Yep, I've been working on a suite of functional data structures for several weeks now, and I can attest that it's a pleasure to do this in Coffeescript. Even plain Javascript would be far less clunky than Ruby (which I did my original experiments in), not to mention Python, but Coffeescript's function literals make it so much more concise and readable. That said, if we could come up with a variant syntax for lazy comprehension so that 'regular' loops won't be affected, that would still be lovely. I'm not exactly holding my breath, but I'd very much appreciate if it happened. |
@jashkenas |
Note that the implementation of |
I agree with the conclusion that this specific proposal goes beyond JS a bit too much. But Javascript 1.7 does have list and generator expressions, and the So I think it would be good for CoffeeScript to somehow align with that, and (soonish) implement support for outputting JS using that syntax. Given that CoffeScript's current list comprehensions look like the generator version of JS 1.7 (and writing JS1.7-style list expressions results in a comprehension nested in a list), this might call for something backwards-incompatible. Perhaps some kind of pragma/future mechanism would be usable (e.g. similar to the new JS "use strict" directive). |
Well, you can simulate it, taking advantage of JS's closures
of course, this is too simple. Either way, if you wanted to use generator (
However, you'll be constrained in Mozilla's JS engines (SpiderMonkey and Rhino). |
You already have list comprehensions, why not take another step and add generator expressions to the language? Yes, I know, there is a lack of yield statement. But look, it is possible to implement generators without real continuations.
Profits: infinite sequences, lazy evaluation, more functional programming :)
Here's the code:
The text was updated successfully, but these errors were encountered: