Hi Ben! Thanks so much for sharing this article on reduce(...). I've been recently trying to embrace a more "functional" style of program and write code where I avoid mutating any variables (killing off "standalone variables" as you call them). Reduce is certainly one helpful tool in the toolkit!
Out of curiosity, I'm wondering about your thoughts about putting a reduce(...)inside another reduce(...). Recently, I've been generating a lot of HTML code from JS (I'm trying to avoid React, at least for now, just for learning purposes) and I find myself using reduceall the time to collapse arrays of objects into a single HTML string to render. So for example, to generate an "Article List" GUI widget (screenshot) I've written: .../ts/widgets.ts#L8-L33.
At work, or elsewhere, do you see this kind of usage of reduce often? Is it a bad "code smell"? Any thoughts or opinions?
Thank you for writing this article up! 🙏 Map/Filter/Reduce is the way! ✊
Hey thanks Robert! That's a totally fair question. I've def seen nested reduce loops before, but it can be difficult to read if you throw everything together without helper functions.
So this is a little sketchy:
constsongsByAlbum=[['Rap Snitches Knishes','Beef Rap','Gumbo'],['Accordion','Meat Grinder','Figaro'],['Fazers','Anti-Matter','Krazy World']]songsByAlbum.reduce((songsAsString,album)=>{// Flatten our album strings to a big string of songs across *all* albumsreturnsongsAsString+album.reduce((albumAsString,song)=>{// Flatten album of songs to a string of songsreturnalbumAsString+""+song},"")},"")
That said, you sometimes don't even need reduce for "stringifying" lists. We could totally do something like this instead:
songsByAlbum.map(album=>{returnalbum.join("")// join our list into a string separated by spaces}).join("")// join this list of "album strings" to a big string
PS: if you're using a nested reduce because you have arrays within arrays, there's a new helper in JS to "flatten" to a single array! It's called array.flat. More on that here
Very cool, thanks for sharing, Ben! I never knew about array.flat until today. Very neat. 👍
Everyday, I seem to find a new use for array.reduce though. It's seriously the gift that just keeps giving! 😄 Just this morning, I was searching for an equivalent to Python's counter data structure in JS and found this:
Agreed, reduce is for a lot more than just reduce-ing arrays to something smaller (like a number of a string). That's why some languages like Rust and Kotlin call it "fold." That's because you're "folding" something of one data type (like an array) into something of another data type (like a Map in your example). Doesn't mean we've reduced it, we've just changed it into something different!
For further actions, you may consider blocking this person and/or reporting abuse
Hi Ben! Thanks so much for sharing this article on
reduce(...)
. I've been recently trying to embrace a more "functional" style of program and write code where I avoid mutating any variables (killing off "standalone variables" as you call them). Reduce is certainly one helpful tool in the toolkit!Out of curiosity, I'm wondering about your thoughts about putting a
reduce(...)
inside anotherreduce(...)
. Recently, I've been generating a lot of HTML code from JS (I'm trying to avoid React, at least for now, just for learning purposes) and I find myself usingreduce
all the time to collapse arrays of objects into a single HTML string to render. So for example, to generate an "Article List" GUI widget (screenshot) I've written: .../ts/widgets.ts#L8-L33.At work, or elsewhere, do you see this kind of usage of
reduce
often? Is it a bad "code smell"? Any thoughts or opinions?Thank you for writing this article up! 🙏 Map/Filter/Reduce is the way! ✊
Hey thanks Robert! That's a totally fair question. I've def seen nested
reduce
loops before, but it can be difficult to read if you throw everything together without helper functions.So this is a little sketchy:
...but this is a little better!
That said, you sometimes don't even need
reduce
for "stringifying" lists. We could totally do something like this instead:PS: if you're using a nested
reduce
because you have arrays within arrays, there's a new helper in JS to "flatten" to a single array! It's calledarray.flat
. More on that hereVery cool, thanks for sharing, Ben! I never knew about
array.flat
until today. Very neat. 👍Everyday, I seem to find a new use for
array.reduce
though. It's seriously the gift that just keeps giving! 😄 Just this morning, I was searching for an equivalent to Python'scounter
data structure in JS and found this:What a beauty! What a time to be alive!! 🚀
Source: stackoverflow.com/a/57028486
Haha yeah, love the enthusiasm 😁
Agreed,
reduce
is for a lot more than justreduce
-ing arrays to something smaller (like a number of a string). That's why some languages like Rust and Kotlin call it "fold." That's because you're "folding" something of one data type (like an array) into something of another data type (like a Map in your example). Doesn't mean we've reduced it, we've just changed it into something different!