Array comprehensions shorter syntax (?)
Dmitry A. Soshnikov
dmitry.soshnikov at gmail.com
Sun May 29 07:00:04 PDT 2011
On 29.05.2011 17:45, François REMY wrote:
> An alternative syntax, which I tend to prefer, is a LINQ-like syntax.
As I noted from the beginning I wouldn't like to turn the topic into
bikesheding with all possible syntax constructions for comprehensions
(once again, there many interesting in many languages:
http://en.wikipedia.org/wiki/List_comprehension); the idea was to adopt
arrow symbol for that and only if arrow functions will be accepted. And
the main thing is to write it shorted, after all we already have normal
powerful list comprehensions borrowed from Python.
> Please note that LINQ don’t produce an array, but an enumerable. It
> means that if the result is never used, the query is never executed
> (and if only the first result is used, the query only executed itself
> to that point, and not further)
>
> let users = [ .... ]
> let names = (
> foreach user in users
> select user.name
> if user.age > 30
> );
> let newClientsSpendings = (
> foreach user in users
> let spendings = getSpendingsById(user.id)
> select spendings
> if (user.isNewClient && spendings.length!=0)
> ).merge()
>
Yeah, and ES also supports them. It's called a generator expression; in
this proposal it would look like:
let squares = (x * x | x < data, x > 5);
i.e. parens instead of brackets. In this way we create a generator
object and behind the scene call its `next` method.
> But I don’t know if we really need something special (ie: a new
> syntax) for that kind of things. Using a short function notation +
> paren-free loops could do the trick pretty well :
>
Perhaps, I don't cancel it. Let's see:
// full notation
let squares = [x * x for (x in values(data)) if (x > 5)]
// paren-free
let squares = [x * x for x in values(data) if x > 5]
// set-builder notation
let squares = [x * x | x <- data, x > 5 ]
Yes, I like paren-free notation as well. So, it's really just a
proposal, it can be accepted or not, depending on needs and complexity
of implementation. Moreover, it's syntactically incomparable.
> let newClientsSpendings = @{
> foreach user in users {
> if !user.isNewClient: continue;
> let spendings = getSpendingsById(user.id);
> if spendings.length!=0: yield spendings;
> }
> }().merge();
>
> For your “squares” sample, that gives :
>
> let squares = @{
> foreach x in data {
> if x<3 : yield x*x
> }
> }();
>
> It’s longer,
That's it, exactly. We always looking for a shorter sugar. Though, the
main thing that the sugar shouldn't be cryptic at the same time.
Probably Erlang's list comprehensions are cryptic for someone, but
again, taking into account arrow-functions, seems arrow-comprehensions
aren't so cryptic.
> but it’s aslo more extensible... and it don’t introduce anything new
> to ES Harmony.
As well as current array-comprehensions; though, paren-free are better IMO.
Dmitry.
> Please note that your syntax has to be rejected because we can’t get
> intellisense working since the variable is declared after the expression.
> Best regards,
> François
> *From:* Jose Antonio Perez <mailto:joseanpg at gmail.com>
> *Sent:* Sunday, May 29, 2011 2:18 PM
> *To:* es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> *Subject:* Re: Array comprehensions shorter syntax (?)
> 2011/5/29 Dmitry A. Soshnikov <dmitry.soshnikov at gmail.com
> <mailto:dmitry.soshnikov at gmail.com>>
>
> Hi,
>
> Don't get this proposal as a bikesheding, just an idea in case if
> arrow functions will win the block-functions.
>
> What about to make a sugar for Array comprehensions based also on
> arrow syntax? The same as in Erlang:
>
> let data = [1, 2, 3, 4, 5];
>
> let squares = [x * x | x <- data, x > 3]; // [16, 25]
>
>
> Basically you are proposing bring in Haskell's syntax for comprehensions
>
> 3. Of course it's assumed that we can destructure elements in such
> array comprehensions:
>
> let users = [
> {name: "Alex", age: 31},
> {name: "John", age: 25},
> {name: "Mark", age: 33}
> ];
>
> let names = [name | {name, age} <- users, age > 30]; // ["Alex",
> "Mark"]
>
>
> This form of located flat pattern matching is good.
>
> Jose.
>
> ------------------------------------------------------------------------
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110529/444447ea/attachment-0001.html>
More information about the es-discuss
mailing list