Open LilithHafner opened 2 years ago
When init is not specified, we should construct inital values from the provided array.
The peculiarity with reduce(op, [1])
stems from it calling through a call chain which ends here. In fact, op
never gets called; for example:
julia> mybinaryop(a, b) = (println("hello world!"); a + b)
mybinaryop (generic function with 1 method)
julia> reduce(mybinaryop, [1])
1
julia> reduce(mybinaryop, [1,2])
hello world!
3
julia> reduce(mybinaryop, [1,2,3])
hello world!
hello world!
6
To be fair, the docstring for reduce
with kwarg dims
states that the init
kwarg is optional only for +
, *
, max
and min
.
Constructing initial values can be quite difficult -- the neutral element cannot necessarily be derived from the array and binary operator alone, hence the error thrown.
To be fair, the docstring for reduce with kwarg dims states that the init kwarg is optional only for +, *, max and min.
reduce(f, A; dims=:, [init])
Reduce 2-argument function f along dimensions of A. dims is a vector specifying the dimensions to reduce, and the keyword argument init is the initial value to use in the reductions. For +, *, max and min the init argument is optional.
That's a good point, however init
is still listed in square braces which indicates that it is optional. More importantly, we should never need an init
argument for non-empty collections. We can simply use the first element of the collection as the initial value. This is worth supporting because, as you noted, it can be hard to come up with a neutral element. In some cases, there is no neutral element, but we should still support multidimensional reductions.