count_zeros = sum( map(x->(x==0.0), actuals) )
# can be rewritten count_zeros = sum(x==0.0 for x in actuals) This shouldn't allocate, because it's using a generator. I agree with DNF that _v1 is already straight-forward, as far as performance-sensitive code goes. On Tuesday, October 25, 2016 at 6:34:45 AM UTC-4, DNF wrote: > > Could you explain why you are unhappy with v1? It seems like a > near-optimal implementation to me. It is idiomatic Julia, clean, clear, and > fast. Are you certain that you're not looking for a one-liner out of force > of habit? > > On Tuesday, October 25, 2016 at 9:15:54 AM UTC+2, Martin Florek wrote: >> >> Hi all, >> I'm new in Julia and I'm doing refactoring. I have the following function: >> >> function mapeBase_v1(A::Vector{Float64}, F::Vector{Float64}) >> s = 0.0 >> count = 0 >> for i in 1:length(A) >> if(A[i] != 0.0) >> s += abs( (A[i] - F[i]) / A[i]) >> count += 1 >> end >> end >> >> s, count >> >> end >> >> I'm looking for a simpler variant which is as follows: >> >> function mapeBase_v2(A::Vector{Float64}, F::Vector{Float64}) >> # A - actual target values >> # F - forecasts (model estimations) >> >> s = sumabs((x - y) / x for (x, y) in zip(A, F) if x != 0) # Generator >> >> count = length(A) # ??? >> s, countend >> >> >> However with this variant can not determine the number of non-zero elements. >> I found option with length(A[A .!= 0.0]), but it has a large allocation. >> Please, someone knows a solution with generator, or variant v1 is very good >> choice? >> >> >> Thanks in advance, >> Martin >> >>