# List comprehension \`no method matching zero\` issue

**URL:** <https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 27, 2021, 12:08pm UTC](https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472 "2021-10-27T12:08:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 27, 2021, 12:08pm UTC](https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472/1 "2021-10-27T12:08:10Z")

</div>

Hello, good morning,

I had a following code block failing when I’m doing a loop for get a vector of results in a monte carlo simulation. Can’t understand why I got the issue above:  
in line below:

```julia
mc_estimate = sum([in(sample(omega), union(A, B)) for x in 1:i if x % 1000 == 0]) / i;

```

```julia
ERROR: LoadError: MethodError: no method matching zero(::Type{Any})
Closest candidates are:
  zero(::Type{Union{Missing, T}}) where T at missing.jl:105
  zero(::Union{Type{P}, P}) where P<:Dates.Period at C:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.6\Dates\src\periods.jl:53       
  zero(::Histogram{T, N, E}) where {T, N, E} at C:\Users\jcbritobr\.julia\packages\StatsBase\Q76Ni\src\hist.jl:538
  ...
Stacktrace:
  [1] zero(#unused#::Type{Any})
    @ Base .\missing.jl:106
  [2] reduce_empty(#unused#::typeof(+), #unused#::Type{Any})
    @ Base .\reduce.jl:311
  [3] reduce_empty(#unused#::typeof(Base.add_sum), #unused#::Type{Any})
    @ Base .\reduce.jl:320
  [4] mapreduce_empty(#unused#::typeof(identity), op::Function, T::Type)
    @ Base .\reduce.jl:343
  [5] reduce_empty(op::Base.MappingRF{typeof(identity), typeof(Base.add_sum)}, #unused#::Type{Any})
    @ Base .\reduce.jl:329
  [6] reduce_empty_iter
    @ .\reduce.jl:355 [inlined]
  [7] mapreduce_empty_iter(f::Function, op::Function, itr::Vector{Any}, ItrEltype::Base.HasEltype)
    @ Base .\reduce.jl:351
  [8] _mapreduce(f::typeof(identity), op::typeof(Base.add_sum), #unused#::IndexLinear, A::Vector{Any})
    @ Base .\reduce.jl:400
  [9] _mapreduce_dim
    @ .\reducedim.jl:318 [inlined]
 [10] #mapreduce#672
    @ .\reducedim.jl:310 [inlined]
 [11] mapreduce
    @ .\reducedim.jl:310 [inlined]
 [12] #_sum#682
    @ .\reducedim.jl:878 [inlined]
 [13] _sum
    @ .\reducedim.jl:878 [inlined]
 [14] #_sum#681
    @ .\reducedim.jl:877 [inlined]
 [15] _sum
    @ .\reducedim.jl:877 [inlined]
 [16] #sum#679
    @ .\reducedim.jl:873 [inlined]
 [17] sum(a::Vector{Any})
    @ Base .\reducedim.jl:873
 [18] top-level scope
    @ c:\Workspace\julia\statistics\samples.jl:49
in expression starting at c:\Workspace\julia\statistics\samples.jl:48

```

The code below:

```julia
# %% Monte Carlo simulation for above problem
using StatsBase, Plots

N=10^4;
omega = collect(Ω);

simulations = Vector(undef, N);
mc_estimate = 0;
for i in 1:N
    mc_estimate = sum([in(sample(omega), union(A, B)) for x in 1:i if x % 1000 == 0]) / i;
    simulations[i] = mc_estimate;
end

println("The result of monte carlo simulation is $mc_estimate");
plot(1:N, simulations);
plot!(x -> 0.6, 1:N)

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 27, 2021, 12:15pm UTC](https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472/2 "2021-10-27T12:15:07Z")

</div>

It looks like the element type of the comprehension is inferred as `Any`, and, from the stacktrace, it looks like it is empty, so Julia errors when you call `sum` on it:

```julia
julia> x = Any[];

julia> sum(x)
ERROR: MethodError: no method matching zero(::Type{Any})

```

If you know what element type you expect (e.g. `Int`), then you can use a typed comprehension: `Int[...]` instead, but perhaps there is a bug in your code since I am guessing you didn’t expect it to be empty in the first place.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 27, 2021, 12:23pm UTC](https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472/3 "2021-10-27T12:23:56Z")

</div>

It solves de error, but the results now I got is wrong. What I’m trying to do is optimize this list towards 10^4 or more.

![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/454f713c8f17d4c9ea5258a801f371d642e998bf.png)

What I was expecting is a plot like the below:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2781fde4ef767f09065fbf4d8227feb265d1163.png)

I’m just trying to skip some simulations, because I want to approximate Monte Carlo to 10^6 and get better results and plot them.

The comprehension must generate a result list for sum only if x % 1000 == 0, but its not working what we can see in plot. Algorithm is wrong. I cant skip the samples because this will lead to less aproximate results.

---

<div class="post-metadata">

**Author:** ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)\
**Post date:** [October 27, 2021, 1:41pm UTC](https://discourse.julialang.org/t/list-comprehension-no-method-matching-zero-issue/70472/4 "2021-10-27T13:41:01Z")

</div>

I think got what I want with the code below:

```julia
# %% Monte Carlo simulation for above problem
using StatsBase, Plots

N=10^4;
step = 1;
omega = collect(Ω);

size = Int(N/step)

simulations = Vector(undef, size);
mc_estimate = 0;
for i in 1:step:N
    println("\rSimulation $i of $N");
    mc_estimate = sum(Int[in(sample(omega), union(A, B)) for _ in 1:i]) / i;
    index =Int(round(i/step));
    simulations[index] = mc_estimate;
end

println("The result of monte carlo simulation is $mc_estimate");
plot(1:size, simulations, xlabel="Samples", ylabel="Result", label="Result");
plot!(x -> result, 1:size, label="Solution ($result)")

```

Plot:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/5/a5063d7b93dc8360b616b30b77d40e2780791cf1.png)
