flatMap or flatten
# Frankie Bagnardi (9 years ago)
I often do the .reduce((xs, ys) => xs.concat(ys)) and I feel the need to
leave a comment explaining what it does.
+1 to this proposal.
I often do the `.reduce((xs, ys) => xs.concat(ys))` and I feel the need to
leave a comment explaining what it does.
+1 to this proposal.
On Fri, May 19, 2017 at 12:44 PM, Luan Nico <luannico27+es-discuss at gmail.com
> wrote:
> I always surprise myself finding out there's no native way to flat an
> array. I know it's easy to implement, but to increase legibility I propose
> creating one of two options (either suffice, I believe):
>
> - `flatMap` : regular map, but flattens the array afterwards
> - `flatten` : just call on an array and get a new flattened array
>
> Some examples usages of both:
>
> With `flatMap`:
> ```
> const numbers = [12, 35];
> const divisors = numbers.flatMap(factors);
> ```
>
> With `flatten`:
> ```
> const numbers = [12, 35];
> const divisors = numbers.map(factors).flatten();
> ```
>
> Flattening an array is converting something like `[1, [2, 3], 4]` to `[1,
> 2, 3, 4]`.
> There is also need to choose whether it's deep or shallow.
>
> I suggest adding a `flatten` shallow method, as that would be useful in
> all scenarios described.
>
> Example implementation:
>
> ```
> Array.prototype.flatten = function () {
> return [].concat.apply([], this);
> };
> ```
>
> What are your opinions about it?
>
> _______________________________________________
> 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/20170519/dbb8a689/attachment.html># Jordan Harband (9 years ago)
See https://github.com/bterlson/proposal-flatMap
On Fri, May 19, 2017 at 12:54 PM, Frankie Bagnardi <f.bagnardi at gmail.com>
wrote:
> I often do the `.reduce((xs, ys) => xs.concat(ys))` and I feel the need to
> leave a comment explaining what it does.
>
> +1 to this proposal.
>
>
> On Fri, May 19, 2017 at 12:44 PM, Luan Nico <luannico27+es-discuss at gmail.
> com> wrote:
>
>> I always surprise myself finding out there's no native way to flat an
>> array. I know it's easy to implement, but to increase legibility I propose
>> creating one of two options (either suffice, I believe):
>>
>> - `flatMap` : regular map, but flattens the array afterwards
>> - `flatten` : just call on an array and get a new flattened array
>>
>> Some examples usages of both:
>>
>> With `flatMap`:
>> ```
>> const numbers = [12, 35];
>> const divisors = numbers.flatMap(factors);
>> ```
>>
>> With `flatten`:
>> ```
>> const numbers = [12, 35];
>> const divisors = numbers.map(factors).flatten();
>> ```
>>
>> Flattening an array is converting something like `[1, [2, 3], 4]` to `[1,
>> 2, 3, 4]`.
>> There is also need to choose whether it's deep or shallow.
>>
>> I suggest adding a `flatten` shallow method, as that would be useful in
>> all scenarios described.
>>
>> Example implementation:
>>
>> ```
>> Array.prototype.flatten = function () {
>> return [].concat.apply([], this);
>> };
>> ```
>>
>> What are your opinions about it?
>>
>> _______________________________________________
>> 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/20170519/6e407ebf/attachment.html># Darien Valentine (9 years ago)
Looks like the link got cut off:
bterlson.github.io/proposal-flatMap, bterlson/proposal-flatMap
Looks like the link got cut off: https://bterlson.github.io/proposal-flatMap/ https://github.com/bterlson/proposal-flatMap -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170519/1b77c72a/attachment-0001.html>
# Igor Baklan (8 years ago)
+1 Good step toward (`JS` <= [`Scala`]( https://www.scala-lang.org/documentation/) ) unification. ( [flatMap]( https://www.scala-lang.org/api/current/scala/collection/TraversableLike.html#flatMap[B](f:A=>scala.collection.GenTraversableOnce[B]):Traversable[B]) , [flatten]( https://www.scala-lang.org/api/current/scala/collection/generic/GenericTraversableTemplate.html#flatten[B]:Traversable[B]) ) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170606/5bcd7d3f/attachment.html>
# Isiah Meadows (8 years ago)
Eh, not exactly. Scala uses a more general version that works with any
monadic type (flatMap is technically a monadic bind, and flatten a
monadic join), while this is roughly equivalent to
[].concat(...array.map(func)) or _.flatten(_.map(array, func)).
Eh, not exactly. Scala uses a more general version that works with any monadic type (`flatMap` is technically a monadic bind, and `flatten` a monadic join), while this is roughly equivalent to `[].concat(...array.map(func))` or `_.flatten(_.map(array, func))`. On Tue, Jun 6, 2017, 07:02 Igor Baklan <io.baklan at gmail.com> wrote: > +1 > > Good step toward (`JS` <= [`Scala`]( > https://www.scala-lang.org/documentation/) ) unification. ( [flatMap]( > https://www.scala-lang.org/api/current/scala/collection/TraversableLike.html#flatMap[B](f:A=>scala.collection.GenTraversableOnce[B]):Traversable[B]) > , [flatten]( > https://www.scala-lang.org/api/current/scala/collection/generic/GenericTraversableTemplate.html#flatten[B]:Traversable[B]) > ) > _______________________________________________ > 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/20170608/1271a087/attachment-0001.html>
I always surprise myself finding out there's no native way to flat an array. I know it's easy to implement, but to increase legibility I propose creating one of two options (either suffice, I believe):
flatMap: regular map, but flattens the array afterwardsflatten: just call on an array and get a new flattened arraySome examples usages of both:
With
flatMap:With
flatten:Flattening an array is converting something like
[1, [2, 3], 4]to[1, 2, 3, 4]. There is also need to choose whether it's deep or shallow.I suggest adding a
flattenshallow method, as that would be useful in all scenarios described.Example implementation:
What are your opinions about it?
I always surprise myself finding out there's no native way to flat an array. I know it's easy to implement, but to increase legibility I propose creating one of two options (either suffice, I believe): - `flatMap` : regular map, but flattens the array afterwards - `flatten` : just call on an array and get a new flattened array Some examples usages of both: With `flatMap`: ``` const numbers = [12, 35]; const divisors = numbers.flatMap(factors); ``` With `flatten`: ``` const numbers = [12, 35]; const divisors = numbers.map(factors).flatten(); ``` Flattening an array is converting something like `[1, [2, 3], 4]` to `[1, 2, 3, 4]`. There is also need to choose whether it's deep or shallow. I suggest adding a `flatten` shallow method, as that would be useful in all scenarios described. Example implementation: ``` Array.prototype.flatten = function () { return [].concat.apply([], this); }; ``` What are your opinions about it? -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170519/6a97dbb4/attachment.html>