Array comprehension syntax
allen at wirfs-brock.com
Sat Sep 22 21:57:20 PDT 2012
On Sep 22, 2012, at 2:52 PM, Brendan Eich wrote:
> Allen Wirfs-Brock wrote:
>>> My personal bias, is that comprehensions are just sugar that are best used to express relatively simple and common construction use cases. Complicated sequences of |for| and |if| clauses will be rarely seen and hence less understandable than the equivalent explicit looping expansions.
> Ok, but this doesn't help get your solution to the inherent tension between "not using comprehensions for complicated cases" vs. "distaste for nanny syntax restrictions." :-|
My nanny remark was in regard to Jason's suggestion of requiring a ConditionalExpression rather than an AssignmentExpression in if clauses. eg, forbidding: if x=3
>> 2) a) one or more [for] clauses followed by a single optional if clause (what is currently in the draft)
actually what I meant to say was that personal preference for ES6 comprehensions, in increasing complexity order is:
1) [a non-starter alternative]
2) a single for clause followed by a single optional if clause
3) one or more for clauses followed by a single optional if clause (what is in the current draft)
4) a sequence where each element consists of a for clause and an optional if clause
> How is this not a nanny syntax restriction? The desugaring works without issue for either of
> [x*y for x in range(XDIM) for y in range(YDIM)if x & 1 ]
> [x*y for x in range(XDIM) if x & 1 for y in range(YDIM)]
> but the last has the virtue of skipping the y iteration for even x values.
(don't you need to use of instead of in)
I don't think this is a nanny syntax issue as it doesn't involve trying to make it impossible to express something that is likely erroneous. Instead it is a simplicity issue. "a sequence of for clauses followed by an optional if clause" is strictly simpler to express and understand than "a sequence where each element consists of a for clause and an optional if clause". If use of non-trailing if clause is quite rare, then it may be adding unneeded complexity to the language to support them, since there are already other ways to express the same rare thing.
As all of these comprehension forms are supported in various other language, it is likely that we could do some data mining and find out occurrence frequencies in real world corpuses . If non-trailing if clauses are rare we probably don't need them?
More information about the es-discuss