Return value of forEach
Isiah Meadows
isiahmeadows at gmail.com
Sun Oct 18 04:29:44 UTC 2015
Lol...not necessary in strict mode (which I thankfully work almost
purely in by default) ;)
But I myself occasionally depend on that behavior as well, that of
`forEach` returning undefined. I've found that it's not that hard to
band-aid `.map` when I need to actually reuse the value (which isn't
the usual case). I also use the same function for Promises, which I
need it more frequently for.
```js
function pipe(f) {
return function (x) {
f.apply(this, arguments)
return x
}
}
// Example
const numberRead = books
.map(title => new Book(title))
.map(pipe(book => console.log(`Book info: ${JSON.stringify(book)}`)))
.filter(book => book.read)
.length
// With my `.tap` idea
const numberRead = books
.map(title => new Book(title))
.tap(book => console.log(`Book info: ${JSON.stringify(book)}`))
.filter(book => book.read)
.length
```
On Fri, Oct 16, 2015 at 5:36 PM, Alexander Jones <alex at weej.com> wrote:
> You mean to say you *don't* have
>
> ```js
> var undefined = [].forEach(Array.prototype.forEach.call);
> ```
>
> at the top of every file!?
>
>
> On Friday, 16 October 2015, Niloy Mondal <niloy.mondal84 at gmail.com> wrote:
>>
>> > That'd be a compatibility break.
>>
>> Ugh... you mean people actually depend on `forEach` returning `undefined`
>> (which it always does) to do further task?
>>
>> I wonder what that kinda code would look like >.<
>>
>> On Fri, Oct 16, 2015 at 6:08 PM, Frankie Bagnardi <f.bagnardi at gmail.com>
>> wrote:
>>>
>>> That'd be a compatibility break.
>>>
>>> If we end up getting :: though:
>>>
>>> ```js
>>> function logEach(){
>>> this.forEach((x) => console.log(x));
>>> return this;
>>> }
>>>
>>>
>>> const a = [1, 2, 3]
>>> .map(square)
>>> ::logEach()
>>> .map(plus1)
>>> .reduce(add);
>>> ```
>>>
>>> You could make that a global variable so you can sprinkle it around your
>>> code in development.
>>>
>>> Having some developer tools in the language would be nice though. I don't
>>> even think console.log is in the spec. A global like Debug.logThis for
>>> example would be a more general ::-able.
>>>
>>>
>>> On Fri, Oct 16, 2015 at 5:32 AM, Andrea Giammarchi
>>> <andrea.giammarchi at gmail.com> wrote:
>>>>
>>>> ```js
>>>> const a = [1, 2, 3]
>>>> .map(square)
>>>> .map(x => console.log(x) || x )
>>>> .map(plus1)
>>>> .reduce(add);
>>>> ```
>>>>
>>>> Regards
>>>>
>>>>
>>>> On Fri, Oct 16, 2015 at 10:13 AM, Niloy Mondal
>>>> <niloy.mondal84 at gmail.com> wrote:
>>>>>
>>>>> Currently, `Array.prototype.forEach` returns `undefined`. It would be
>>>>> more
>>>>> useful if it returns the array itself.
>>>>>
>>>>> Say I have written some code like this...
>>>>>
>>>>> ```js
>>>>> const a = [1, 2, 3]
>>>>> .map(square)
>>>>> .map(plus1)
>>>>> .reduce(add);
>>>>> ```
>>>>>
>>>>> For some reason, I am not getting the expected output. For debugging, I
>>>>> would
>>>>> like the print the values after each step. My first initial reaction is
>>>>> to
>>>>> put a `forEach` step in between and print the values like so...
>>>>>
>>>>> ```js
>>>>> const a = [1, 2, 3]
>>>>> .map(square)
>>>>> .forEach(x => console.log(x))
>>>>> .map(plus1)
>>>>> .reduce(add);
>>>>> ```
>>>>>
>>>>> Unfortunately, this does not work as `forEach` returns `undefined`. I
>>>>> now have
>>>>> to comment out all the code below it. Having the _plug and play_
>>>>> behaviour for
>>>>> `forEach` would be very convenient.
>>>>>
>>>>> _______________________________________________
>>>>> 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
>>>>
>>>
>>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
--
Isiah Meadows
More information about the es-discuss
mailing list