reindexio / youmightnotneedunderscore

https://www.reindex.io/blog/you-might-not-need-underscore/
MIT License
226 stars 18 forks source link

Updated range to remove redundant k param #16

Closed arcseldon closed 8 years ago

arcseldon commented 8 years ago

Found the redundant k param distracting, like I had missed something. Instead, just use an underscore '_' to indicate you don't care about that param.

Curiously, in addition, this might interest you.

Couple of alternatives:

Array(n).fill().map((_,i) => x + i)
Array.from(Array(n), (_, i) => x + i)

Demo using n = 10, x = 1:

> Array(10).fill().map((_,i) => i + 1)
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
> Array.from(Array(10), (_, i) => i + 1)
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]

When I exercised each of these 1000,000 times, as part of a repeat function, I found the Array(n).fill() approach actually more performant too.

See my stackoverflow answer related to a question regarding repeat functions in JS.

However, I have kept your answer for this repo, because I think it is perhaps clearer to read, and a good working example of Array.from.

To be honest, I would also say your use of rest params for the arguments, could also just have been Array.from(arguments), but this example demonstrates Array.from too.

arcseldon commented 8 years ago

And I just wished to add that I really enjoyed reading your blog ! It is great to see more proven functional utilities make it into the core language, and I agree we are diminishing the need for underscore / lodash :smile:

fson commented 8 years ago

Thank you @arcseldon!

arcseldon commented 8 years ago

@fson - pleasure, thank you for merging.