Re: Re: Return value of forEach

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Re: Return value of forEach

Naveen Chawla
In that case, there should be a new function:

"forEvery" (or "forEachAndReturn" or "each" or whatever)

that does exactly the same as "forEach" but returns the array.

This is a much needed feature for those of us that don't want to create a new array on each invocation but still have the ability to transform the contents of the array (or do some other processing) from within a .sort / .map / .filter etc. chain.

Otherwise, in complicated transformations, we have to end the chain, perform the forEach separately, and then resume the chain, making the code more messy. Sort is an example of a function that already allows mutation of the existing array and returns it, so this would be nothing extraordinary for ES.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Return value of forEach

Sebastian Malton
Would allowing for each to have the following two properties also help? 

1. Returning a falsy value ends the chain and the next element will not be called 
2. Ability to pass in a call back as parameter 4 and if present will stop the next element from being iterated over until the callback is called with a truthy value (falsey also ends)



Sent: July 6, 2017 8:44 AM
Subject: Re: Re: Return value of forEach

In that case, there should be a new function:

"forEvery" (or "forEachAndReturn" or "each" or whatever)

that does exactly the same as "forEach" but returns the array.

This is a much needed feature for those of us that don't want to create a new array on each invocation but still have the ability to transform the contents of the array (or do some other processing) from within a .sort / .map / .filter etc. chain.

Otherwise, in complicated transformations, we have to end the chain, perform the forEach separately, and then resume the chain, making the code more messy. Sort is an example of a function that already allows mutation of the existing array and returns it, so this would be nothing extraordinary for ES.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Return value of forEach

Naveen Chawla
In reply to this post by Naveen Chawla
Suitable for that would be an "until" function that returns an array with all elements of the target array until a condition is met (which can then be followed by a chained call to forEach/forEvery), not "forEach" / "forEvery" as such. Reason: readability. The counterpart could be an "after" function which returns an array with all elements after the first element that satisfies a condition. (Maybe even "untilInclusive"/"from" for the inclusive versions too).

On 6 July 2017 at 18:21, Sebastian Malton <[hidden email]> wrote:
Would allowing for each to have the following two properties also help? 

1. Returning a falsy value ends the chain and the next element will not be called 
2. Ability to pass in a call back as parameter 4 and if present will stop the next element from being iterated over until the callback is called with a truthy value (falsey also ends)



Sent: July 6, 2017 8:44 AM
Subject: Re: Re: Return value of forEach

In that case, there should be a new function:

"forEvery" (or "forEachAndReturn" or "each" or whatever)

that does exactly the same as "forEach" but returns the array.

This is a much needed feature for those of us that don't want to create a new array on each invocation but still have the ability to transform the contents of the array (or do some other processing) from within a .sort / .map / .filter etc. chain.

Otherwise, in complicated transformations, we have to end the chain, perform the forEach separately, and then resume the chain, making the code more messy. Sort is an example of a function that already allows mutation of the existing array and returns it, so this would be nothing extraordinary for ES.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Return value of forEach

Michael J. Ryan
Woudn't Array.prototype.all satisfy those use cases, they run until a falsy value is returned.

--
Michael J. Ryan - http://tracker1.info

On Thu, Jul 6, 2017 at 6:51 AM, Naveen Chawla <[hidden email]> wrote:
Suitable for that would be an "until" function that returns an array with all elements of the target array until a condition is met (which can then be followed by a chained call to forEach/forEvery), not "forEach" / "forEvery" as such. Reason: readability. The counterpart could be an "after" function which returns an array with all elements after the first element that satisfies a condition. (Maybe even "untilInclusive"/"from" for the inclusive versions too).

On 6 July 2017 at 18:21, Sebastian Malton <[hidden email]> wrote:
Would allowing for each to have the following two properties also help? 

1. Returning a falsy value ends the chain and the next element will not be called 
2. Ability to pass in a call back as parameter 4 and if present will stop the next element from being iterated over until the callback is called with a truthy value (falsey also ends)



Sent: July 6, 2017 8:44 AM
Subject: Re: Re: Return value of forEach

In that case, there should be a new function:

"forEvery" (or "forEachAndReturn" or "each" or whatever)

that does exactly the same as "forEach" but returns the array.

This is a much needed feature for those of us that don't want to create a new array on each invocation but still have the ability to transform the contents of the array (or do some other processing) from within a .sort / .map / .filter etc. chain.

Otherwise, in complicated transformations, we have to end the chain, perform the forEach separately, and then resume the chain, making the code more messy. Sort is an example of a function that already allows mutation of the existing array and returns it, so this would be nothing extraordinary for ES.


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss



_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Return value of forEach

T.J. Crowder-2
On Sat, Jul 8, 2017 at 1:40 AM, Michael J. Ryan <[hidden email]> wrote:
Woudn't Array.prototype.all satisfy those use cases, they run until a falsy value is returned.

Is there an `Array.prototype.all` proposal? I'm not seeing it on github. If you're thinking of `every` and `some`, they don't return a reference to the array, which seemed to be important to Naveen for chaining.

-- T.J. Crowder

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Loading...