4

I kind of hate people who use the JavaScript array method Array.reduce(...)
It rarely makes sense but makes code unnecessarily complex to read.

Comments
  • 3
    What do you use when you want to summate something or fill a dictionary from an array?
  • 3
    I've have to look up the differences between map reduce and filter way way way too often.
  • 3
    @N00bPancakes Filter filters and reduce reduces. What is complicated about it?
  • 1
    @N00bPancakes
    Just mentally replace the word reduce with fold. It folds the result of an n-value series into a single resulting value. It folds a series of value together.

    Filter just returns an array that is all values from the source array where the predicate returned true.
  • 2
    @lamka02sk

    I think it is more of a semantic satiation thing.

    I use map, filter, every day. Less so reduce.

    But then I type out another one and ... it just occurs to me I'm in zombie mode, the method syntax is similar, I start wonder about the vastness of the universe, and maybe not entirely sure what the difference is between them ;)
  • 1
    @SortOfTested Your mentioned usecase is one of the few where reduce is appropriate.
    I see it abused wher a map, find, filter or loop would have been the right choice.
  • 3
    @PonySlaystation
    Had to check, lots of readability trolls on the internet who don't like declarative programming in any context ;)

    Appropriateness does indeed matter. Usually, if you're side effecting, you should avoid the declarative method. Though I personally thing you should avoid side effecting in pretty much every case.

    @N00bPancakes
    Got bored in a pointless sales meeting, so I wrote up an example primer-as-code on it, with some other goodies thrown in.

    https://stackblitz.com/edit/...
  • 1
    It's useful, and maybe the only possible one-liner for finding sum of an array, It might be difficult to read, but if you are using JS, understanding it is a better option than not using it.
  • 2
    @SortOfTested Thanks! I think I'll have to unpack some of the RxJS and Observable to figure this out entirely ;)
  • 2
  • 1
  • 1
    @N00bPancakes
    The reason I like that approach is because of how broken the JS declarative array fns are.

    Each function will produce an entirely new array buffer for each operation. This results in a significiantly greater space complexity than you would usually expect from a streamlike interface.

    Rx will apply every operator to each value before the next value is processed, so the space complexity is always 1n.
Add Comment