-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sum(f, itr) when itr is an empty collection should return zero #36262
Comments
What if, for example, For example, suppose we define In this example, Won't that mean that this method of |
Well, ok. I guess I shouldn't have put julia> sum((x -> 1.0).(Int[]))
0.0
julia> sum(x -> 1.0, Int[])
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
[1] _empty_reduce_error() at ./reduce.jl:295
[2] mapreduce_empty(::Function, ::Function, ::Type{T} where T) at ./reduce.jl:334
[3] _mapreduce(::var"#13#14", ::typeof(Base.add_sum), ::IndexLinear, ::Array{Int64,1}) at ./reduce.jl:392
[4] _mapreduce_dim(::Function, ::Function, ::NamedTuple{(),Tuple{}}, ::Array{Int64,1}, ::Colon) at ./reducedim.jl:312
[5] #mapreduce#580 at ./reducedim.jl:307 [inlined]
[6] mapreduce at ./reducedim.jl:307 [inlined]
[7] _sum at ./reducedim.jl:657 [inlined]
[8] #sum#584 at ./reducedim.jl:653 [inlined]
[9] sum(::Function, ::Array{Int64,1}) at ./reducedim.jl:653
[10] top-level scope at REPL[28]:1 However, off the top of my head, I don't know how to implement that. Is there an internal function somewhere in Base or Core that returns the inferred output type of a function? |
I can't speak to the efficiency of this, but here's an implementation that has the desired behavior: julia> mysum(f, itr) = sum(f.(itr))
mysum (generic function with 1 method)
julia> mysum(x -> 1.0, Int[])
0.0
julia> mysum(==(1), Int[])
0 |
What zero should it return, when the type is not inferrable? On the other hand: julia> sum(Array{Union{Float64, Int64},1}())
0 So if |
Defining But we now have (Note: The confusion here is that broadcasting is "cheating" by using the internal inference API |
Well, technically it's possible if you "cheat" and use broadcasting:
Ok. And I see that you've updated the docstrings for @tkf Question: |
Actually, no, it's not defining any concrete API. It's exposing undefined behavior of
Personally, I think it's better to make
|
Is that the whole story? |
Actually I miss why these behaviors are inconsistent. I'd expect these behaviors if using |
@tkf commented on Jun 13, 2020, 4:51 AM GMT+4:30:
I am trying to count something using a for comprehension. I tried |
julia> sum(1 for i in []; init = 0)
0 Given the difficulty in determining which zero (i.e. of which type) to return in general for an empty input collection, I wonder whether |
closing since we now have
|
The sum of an empty, typed array is zero:
Broadcasting over an empty array returns an empty array:
So, as expected, summing over the above broadcasted operations returns zero:
However, if we perform the same operations using the
sum(f, itr)
method, we get anArgumentError
:The text was updated successfully, but these errors were encountered: