Skip to content
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

Support list comprehensions #415

Closed
DartBot opened this issue Nov 10, 2011 · 7 comments
Closed

Support list comprehensions #415

DartBot opened this issue Nov 10, 2011 · 7 comments
Assignees
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Nov 10, 2011

This issue was originally filed by adblockforc...@gmail.com


Support list comprehensions a la Python, or else list somewhere why you won't support them.

Rationale:
 - They are voted the number one favorite language feature of Pythonistas, which will help with adoption and evangelism.
 - They can be implemented as syntactic sugar and require no new keywords.
 - They allow for clearer and shorter looping constructs.
 - They allow for a functional style without explicit use of map() and filter().

You will gain converts who love list comprehensions and are tired of them not existing in JavaScript.

I'm sure you know the format, but for everyone else: the list comprehension

  [ expr1 for a in list1 if expr2 ]

is roughly equivalent to the JavaScript

  (function() {
    var result = [];
    for (var i = 0; i < list1.length; i++) {
      if (expr2)
        result.append(expr1); // can refer to a
    return result;
  )()

and multiple 'for' subclauses are supported.

@DartBot
Copy link
Author

DartBot commented Nov 10, 2011

This comment was originally written by adblockforc...@gmail.com


To be precise, list1 can be any iterable, so the syntactic un-sugaring would use a for..in loop rather than a for(;;) loop.

@dgrove
Copy link
Contributor

dgrove commented Nov 11, 2011

Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@gbracha
Copy link
Contributor

gbracha commented Nov 30, 2011

Set owner to @gbracha.
Added Accepted label.

@anders-sandholm
Copy link
Contributor

Short function syntax may come in handy.


Added WontFix label.

@DartBot
Copy link
Author

DartBot commented May 3, 2012

This comment was originally written by adblockforchr...@gmail.com


@sandholm, thanks for the update. Could you post a comment with the short function syntax equivalent of

  [ emp.salary / 12 for emp in employees if not emp.retired ]

for comparison?

@whesse
Copy link
Contributor

whesse commented May 3, 2012

My guess is that he means to use short functions in the filter and map:

employees.filter((emp) => !emp.retired ).map((emp) => emp.salary / 12 );

This isn't too bad, but I still prefer list comprehensions.

@DartBot
Copy link
Author

DartBot commented Nov 7, 2014

This comment was originally written by @seaneagan


If this bug (or a similar one) ever gets revived, ought to add an async version as well:

[await for e in stream if (e.isEven) e * e]

Note that this follows the ES6 format, which works better with "await for" to match the async loop syntax:

await for(var e in stream) ...

I would have preferred "on" which would work better with the python style:

[e * e on e in stream if e.isEven]

@DartBot DartBot added Type-Enhancement area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). labels Nov 7, 2014
@kevmoo kevmoo added closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug and removed resolution-wont_fix labels Mar 1, 2016
nex3 pushed a commit that referenced this issue Aug 31, 2016
Not sure why this wasn't implemented. Lists seem to be okay.

R=vsm@google.com

Review URL: https://codereview.chromium.org/1611753002 .
copybara-service bot pushed a commit that referenced this issue Jun 2, 2022
Changes:
```
> git log --format="%C(auto) %h %s" 5699caf..e3f4bd2
 https://dart.googlesource.com/markdown.git/+/e3f4bd2 example: update CDN asset links (#435)
 https://dart.googlesource.com/markdown.git/+/a678bfc example: add GitHub markdown CSS (#434)
 https://dart.googlesource.com/markdown.git/+/bc79c43 Merge pull request #425 from dart-lang/pq-patch-1
 https://dart.googlesource.com/markdown.git/+/4e8aa03 add pub badge
 https://dart.googlesource.com/markdown.git/+/7987e1e Remove dependency on third party package:charcode.
 https://dart.googlesource.com/markdown.git/+/90995fd Split block_parser.dart and inline_parser.dart (#422)
 https://dart.googlesource.com/markdown.git/+/8bb9062 Add trailing commas to some parameter lists to get better formatting (#420)
 https://dart.googlesource.com/markdown.git/+/1c5f2e7 Enable raw strings lint rules (#418)
 https://dart.googlesource.com/markdown.git/+/4784153 Enable use_if_null_to_convert_nulls_to_bools lint rule (#417)
 https://dart.googlesource.com/markdown.git/+/0d67e99 Enable prefer_interpolation_to_compose_strings (#416)
 https://dart.googlesource.com/markdown.git/+/5561351 Enable prefer_final_locals lint rule (#415)
 https://dart.googlesource.com/markdown.git/+/6d39147 Create DelimiterSyntax to replace TagSyntax (#407)
 https://dart.googlesource.com/markdown.git/+/4f4e899 Add caseSensitive parameter on the InlineSyntax constructor (#400)
 https://dart.googlesource.com/markdown.git/+/e16aff0 Check parser.isDone when title is null in _parseInlineBracketedLink (#394)
 https://dart.googlesource.com/markdown.git/+/3471578 Use `Uri.toFilePath()` instead of `Uri.path` for locating tests (#396)

```

Diff: https://dart.googlesource.com/markdown.git/+/5699cafa9ef004875fd7de8ae9ea00e5295e87a4~..e3f4bd28c9e61b522f75f291d4d6cfcfeccd83ee/
Change-Id: Ie04b17dfcce57fcd9e814bd8b9a09677a91136d6
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246984
Auto-Submit: Devon Carew <devoncarew@google.com>
Commit-Queue: Devon Carew <devoncarew@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-language Dart language related items (some items might be better tracked at github.com/dart-lang/language). closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

6 participants