Open utterances-bot opened 3 years ago
Interesting challenge! I think I have an approach that is a bit more succinct: https://codepen.io/jasonmcaffee/pen/ZEpeQBq?editors=0010
Thanks! I did find an issue with the implementation you provided - if a generator function is provided as a starting input instead of an array, the code will loop forever.
reduce((a,v) => a + v)
sum()
.add = (a,v) => a + v
.
Same with
isEven = n => n%2==0
[1,3,5].filter(isEven).reduce(add, 0)
is safe.Hey, sorry for bikeshedding on one line. I learned a ton, and I'll definitely come back to read this page again.
@david-wagstaff, thanks for your kind comment! The use of n
vs num
was not really intentional. While I think it's okay to use short variable names for simple pieces of code, and especially in lambda functions, it's also not recommended to overuse them. Having descriptive names is usually a good thing.
Lazy Evaluation in JavaScript with Generators, Map, Filter, and Reduce
A simple wrapper class in JavaScript for map, filter, and reduce
https://nestedsoftware.com/2018/02/27/lazy-evaluation-in-javascript-with-generators-map-filter-and-reduce-36h5.21002.html