You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In JS, the initial value is optional, and if omitted will be taken from the first element of the array. Unfortunately this changes the type of the function, since the initial value needs to be constrained by the element type, and therefore requires a second set of bindings.
From MDN:
initialValue Optional
A value to which accumulator is initialized the first time the callback is called. If initialValue is specified, callbackFn starts executing with the first value in the array as currentValue. If initialValue is not specified, accumulator is initialized to the first value in the array, and callbackFn starts executing with the second value in the array as currentValue. In this case, if the array is empty (so that there's no first value to return as accumulator), an error is thrown.
The first problem then is what to name these bindings. Typically, the operation without an initial value is called reduce and with an initial value is called fold. That's one option, but would be a breaking change and also less familiar for JS devs. Another option is to name the fold operation reduceWithInit, but that seems a bit backwards since it's the more common operation and is still a breaking change. I can't think of any better alternatives though.
The second problem is that reduce without an initial value will throw an error if the array is empty. The solution to that is pretty straightforward though, just add a wrapper with a check for an empty array or catch the error, and return an option. Since there's already a wrapper function to flip the arguments, that's not much of a problem.
Any ideas on how to proceed here?
The text was updated successfully, but these errors were encountered:
In JS, the initial value is optional, and if omitted will be taken from the first element of the array. Unfortunately this changes the type of the function, since the initial value needs to be constrained by the element type, and therefore requires a second set of bindings.
From MDN:
The first problem then is what to name these bindings. Typically, the operation without an initial value is called
reduce
and with an initial value is calledfold
. That's one option, but would be a breaking change and also less familiar for JS devs. Another option is to name the fold operationreduceWithInit
, but that seems a bit backwards since it's the more common operation and is still a breaking change. I can't think of any better alternatives though.The second problem is that
reduce
without an initial value will throw an error if the array is empty. The solution to that is pretty straightforward though, just add a wrapper with a check for an empty array or catch the error, and return anoption
. Since there's already a wrapper function to flip the arguments, that's not much of a problem.Any ideas on how to proceed here?
The text was updated successfully, but these errors were encountered: