Array Comprehensions

Gil Tayar gil at tayar.org
Mon Feb 6 17:47:05 UTC 2017


Forgive me if I'm wrong, but the double-for comprehension (e.g. [for (i of
numbers) for (j of letters) i + j]) can't be easily expressed in JS because
there's no flatMap.

- Gil

On Mon, Feb 6, 2017 at 7:17 PM T.J. Crowder <tj.crowder at farsightsoftware.com>
wrote:

> Given `Array.prototype.filter`, `Array.prototype.map`, and other utilities
> you can create combined with arrow functions, what is it about using syntax
> for this instead that draws you? For me, the syntax doesn't add any
> expressiveness, e.g. I don't see that this:
>
> ```js
> const numbers = [1, 2, 3];
> const doubled = [for (i of numbers) i * 2];
> ```
>
> is any more expressive than
>
> ```js
> const numbers = [1, 2, 3];
> const doubled = numbers.map(i => i * 2);
> ```
>
> > How do you usually approach or avoid this issue?
>
> I use `filter`, `map`, and such.
>
> > Do you think we should look at improving and standardizing the
> comprehension syntax?
>
> I don't see much percentage in it, FWIW, barring hearing more about the
> motivations for having it.
>
> -- T.J.
>
> On Mon, Feb 6, 2017 at 4:59 PM, Ryan Birmingham <rainventions at gmail.com>
> wrote:
>
> Hello all,
>
> I frequently find myself desiring a short array or generator comprehension
> syntax. I'm aware that there are functional ways around use of
> comprehension syntax, but I personally (at least) love the syntax in the ES
> reference (
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions
> ).
>
> The best previous discussion on this that I found was six years old (
> https://esdiscuss.org/topic/array-comprehensions-shorter-syntax) and
> answers some of my questions, raising others. That said, I wanted to ask:
>
>    - Why is the Comprehension Syntax in the reference yet not more
>    standard? It feels almost like a tease.
>    - How do you usually approach or avoid this issue?
>    - Do you think we should look at improving and standardizing the
>    comprehension syntax?
>
>
> Thank you all for humoring me,
>
> -Ryan Birmingham
>
> _______________________________________________
> 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/20170206/2d1a6f50/attachment-0001.html>


More information about the es-discuss mailing list