Menu

Post image 1
Post image 2
1 / 2
0

Transducers in JavaScript: When `reduce()` Is Not the End of the Story

DEV Community: typescript·Amrishkhan Sheik Abdullah·2 days ago
#etsKdhg3
#dev#fullscreen#const#enter#exit#article
Reading 0:00
15s threshold

In my previous article, I wrote about one of the most underestimated functions in JavaScript: reduce() . That article triggered a very fair discussion. Some developers agreed that reduce() is powerful. Some pushed back. And honestly, the pushback was valid. Because reduce() is one of those functions that can be either beautiful or terrible depending on how it is used. A simple reduce() can express a clean state transformation. A bad reduce() can become unreadable very quickly. So before talking about transducers, let me say this clearly: I do not think reduce() should replace simple loops everywhere. In many cases, a plain for...of loop is more readable, more debuggable, and often faster. const result = [] for ( const user of users ) { if ( user . active ) { result . push ( user . name ) } } Enter fullscreen mode Exit fullscreen mode This is not “less functional”. This is just clear code. And clarity matters. But there is another interesting idea hiding behind reduce() .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More