DEV Community

Discussion on: 🔥 Getting the largest number from an array

 
oliverradini profile image
OliverRadini • Edited

The first one is unnecessary. It's only a bit more convenient to write max(a, b, c) than [a, b, c].fold(max), but it's still something that makes sense.

I'm sure there are many situations where it's preferable to pass multiple arguments instead of constructing a whole new array to do max. I may find it preferable today to construct an array and use array methods to manipulate it, but those methods are, relatively speaking, very new additions to the language.

Regarding:

Passing extra information to the combining function

I've not yet used a language which doesn't allow providing a seed value into its reduction function. Haskell's foldr requires it. Cloure's reduce allows it. C#'s Aggregate allows it to be specified. I wouldn't want to work with a language that didn't allow me to specify a seed value this way.

I appreciate that I wrote basically the same code example that you did - I was trying to point out that there's not any issue at all with reduce in js, it just isn't called fold.

Thread Thread
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

I've not yet used a language which doesn't allow providing a seed value into its reduction function

The seed value is not the problem here. The combining function should take two arguments, but reduce passes 4, which is both dumb and the cause of this problem. I haven't seen another language that does this.

I was trying to point out that there's not any issue at all with reduce in js

Well, there is: it passes two extra arguments to the combining function, which no other fold function usually does.

Thread Thread
 
oliverradini profile image
OliverRadini

Ah I see your point now, sorry I misunderstood what you were saying originally, my bad.

It is frustrating, but really we're talking about edge cases here where you're looking to pass variadic functions into Array.reduce. In this instance we're hitting a problem due to the fact that, firstly, Math.max is variadic and secondly that Array.reduce passes additional arguments by default. Agreed that neither of those is ideal.

JS really isn't a language of ideals, though - it's a pragmatic scripting language, and, for all its flaws, it allows people to get things done quickly. For me a big part of being a good JS programmer is learning to live with its flaws, because if you obsess over them, you can't get very far with it.