nestedsoftware / blog_comments

Comments for https://nestedsoftware.com using utterances
0 stars 0 forks source link

Lazy Evaluation in JavaScript with Generators, Map, Filter, and Reduce #5

Open utterances-bot opened 3 years ago

utterances-bot commented 3 years ago

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

jasonmcaffee commented 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

nestedsoftware commented 3 years ago

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.

david-wagstaff commented 1 year ago

reduce((a,v) => a + v)

  1. I've been using longer variable names, and I instantly adopted yours. So much easier to read.
  2. Half the time you use num and half the time you use n. It made me pause wondering if there was some meaning.
  3. Why stop at reduce? Do sum().
  4. Since I use some callback functions so much, I now just use their names add = (a,v) => a + v. Same with isEven = n => n%2==0
  5. I like not having to supply the accumulator like you did, but I've been burned too many times with Uncaught TypeError: Reduce of empty array with no initial values So now I always include the accumulator now, e.g. [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.

nestedsoftware commented 1 year ago

@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.