selfrefactor / rambda

Faster and smaller alternative to Ramda

Home Page:https://selfrefactor.github.io/rambda

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

use forIn or forEach instead of map

gabrieldavid98 opened this issue · comments

Hi guys.

I was checking source code and i found that "map" function is used incorrectly. For that case i advise to use "forIn" or "forEach" instead.

Lines where i found that:

arr.map((value, key) => {

aKeys.map(val => {

Thanks for this library and for your job.
Greetings ✋

Hi @GhostThrone and thanks for bringing this issue up.

Can you collaborate a bit further?

What is the benefit of forIn and forEach over map?

Also what you mean by map is used incorrectly as I fail to see that?

I think what he meant is that this functions mutate something out of their scope, and you aren't using the map to actually map and return new values, you are using it to iterate through an array to mutate something. IMO forEach is more semantic in this case because it clearly states you want to iterate this array to mutate something, but i wouldn't say this is a "wrong usage of map", you could test the performance to see if it makes a difference, if it doesn't than it's just a minor semantic issue :)

@MarcoWorms Thanks for the clarification. It makes sense. I will do the benchmark and see if a change is required or not.

Benchmark showed that forEach is good to go, so it replaces map in R.equals

In R.lastIndexOf I need both key and value so forEach is not applicable there.

The change is published in the new version 1.0.5 and I am closing the issue.

forEach also has access to key and value

Right, right :)

I never use the method and I checked MDN docs for it and I read only the example where key is not showed.

My mistake - next version will address changing lastIndexOf and other methods where forEach can be used instead of map

@MarcoWorms Thanks.
@selfrefactor I am so sorry, i didn't have time to reply before. Maybe i didn't explain in a good way the differences between map and forEach my mistake.
Thanks for all
Greetings ✋