Array comprehensions with Spread operator

Mark S. Miller erights at google.com
Wed Apr 15 20:20:05 UTC 2015


On Wed, Apr 15, 2015 at 1:11 PM, monolithed <monolithed at gmail.com> wrote:

> @Mark S. Miller,
>
> > Dave Herman did an excellent presentation at one of the TC39 meetings
> that convinced us all to drop comprehension syntax from ES6. I remember it
> surprised us all including, earlier Dave, which led to his presentation.
> Anyone have a link?
>
> > The arguments that I remember as most significant are a) When you look
> as how much syntactic convenience comprehensions provide above explicit
> calls to higher-order operations (assuming we have .map, .filter, as well
> as the currently absent .flatMap) and arrow functions, the answer is not
> much. b) When your comprehensions involve only those ho operations, fine.
> But as soon as you try to mix in some other ho operation, such as e.g., a
> reduce, if you started with a comprehension you're gonna create a mess.
> OTOH, if you were starting with code explicitly calling ho operations, then
> there's nothing confusing or unnatural mixing in some others.
> I can not agree with this approach, [many languages](
>
> http://en.wikipedia.org/wiki/List_comprehension) have this functionality
>


Which is why we were all surprised.



> For example:
>
> ```js
> var files = function (data) {
> let result = [];
>
> for (let path of data) {
> result.push(...fs.readdirSync(path));
> }
>
> return result;
> }
> ```
>
> I believe that this code is awkward and not as beautiful as the possible
> alternatives:
>
>
> ```js
> var files = function (data) {
>    return [ for (path of data) ...fs.readdirSync(path) ];
> }
> ```
>
> or
>
> ```js
> var files = function (data) {
>    return data.map(path => ...fs.readdirSync(path));
> }
> ```
>

or, if we had flatMap

var files = function (data) {
   return data.flatMap(path => fs.readdirSync(path));
}


which looks as good as the comprehension and is clearer -- especially as a
starting point for refactorings.

So should we add flatMap? Yeah, probably.


-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150415/48c7b0cd/attachment-0001.html>


More information about the es-discuss mailing list