-
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
Proposal: Lazy comprehensions #5379
Comments
This seems like a cool idea to me. I'm not totally sure While thinking about alternatives, I wondered about do -> yield somethingExpensive item for item in list; null The final I still think shorthand for this could be nice. |
I can't think of a good syntax that builds upon previous iterator syntax ( somethingExpensive item for yield item in list somethingExpensive item for each item in list We can't use yield for item in list
somethingExpensive item
# Same as
arr = for item in list
somethingExpensive item
yield arr
|
So is this basically the inverse of In other words, this package but built into the language? So this is achievable today via that package wrapping the comprehension, e.g. I have to say, it feels a little weird to me. Do any of the Array prototype functions like |
No, it doesn't. Wrapping the comprehension that way, you'll iterate over the list once, and then reiterate on the arraygen. Generators are useful when you want to spread the computational overload of the operation (it is not executed it's declared, hence 'lazy').
Yeah, creating an iterator that returns the list ( It is also useful for Functional Programming, since it is more declarative than imperative, hence it is common place in functional languages like Haskell. |
I'd like to build on what @edemaine said. Where one might currently write (using an example from your PR): lazyMap = (fn ) -> (generator) -> yield fn item for item from generator ; null
lazyFilter = (pred) -> (generator) -> yield item for item from generator when pred item; null
squareItems = lazyMap (a) -> a * a
oddItems = lazyFilter (a) -> a & 1
results = squareItems oddItems [1 .. 3]
Your patch allows this to be written as results = a * a for a from [1 .. 3] when a & 1 Yes? |
I think you mean the following (need to wrap in parens, like all comprehensions in CoffeeScript, or else it's a one-line results = (a * a for* a from [1 .. 3] when a & 1) Idly, I was wondering whether another type of brackets might make sense (similar to how Python uses results = <a * a for a from [1 .. 3] when a & 1> Hmm, angle brackets conflict with JSX. Never mind. |
What about brackets or curly braces? Currently, this is invalid code (I hope) results = {a * a for a from [1..3]} but this looks better and isn't used as much: results = [a * a for a from [1..3]] Python uses bracketed syntax for arrays and parenthesized syntax for generators, but CS users already rely on parenthesized syntax, so we'd have to use bracket or curly braces if we go for the wrapping method. |
The brace notation does seem promising. I don't currently see any ambiguities, as We can't follow Python's notation because it confuses generators in arrays with array interpolations. Python has the unfortunate feature that |
Create a syntax (I'm thinking in
for*
) for allowing comprehensions to produce an Iterator instead of a list, similar to what python does. This would help when the goal is not to map a list, but use the result in another iteration.Proposed Syntax
Normal Behavior
Proposed Behavior
I'm planning on implementing that (as it does not seem hard, and you'd be a nice introduction to CoffeeScript architecture) so I wanted to know if it's a welcomed feature.
The text was updated successfully, but these errors were encountered: