Reductions over one-element collections can return surprising values #34380
Labels
arrays
[a, r, r, a, y, s]
collections
Data structures holding multiple items, e.g. sets
fold
sum, maximum, reduce, foldl, etc.
Since
reduce
(and friends) are specified to call a two-argument function, the choice of what to do (and what to return) when there's only one element is rather arbitrary and can be surprising. A great motivating case here isreduce(vcat, X)
:I really want
reduce
to just callvcat(1)
, but of course it cannot blindly do that in general. The solution is to provide aninit
. We do patch up this return value in a few notable cases — as this was required to getreduce
to be type stable.julia/base/reduce.jl
Lines 348 to 369 in 0ee3264
This function is nicely documented but doesn't appear in the manual. Should we just patch up
vcat
andhcat
here for now? Or perhapsreduce
ing over 1-element collections without aninit
value should be just as much of an error asreduce
ing over empty ones.The text was updated successfully, but these errors were encountered: