This is because Julia's type inference is not good in global scope and
thus the list comprehension returns a Array{Any,1}. Wrap it in a
function instead:
julia> t = [1:2]
2-element Array{Int64,1}:
1
2
julia> s = [sum(t[1:i]) for i=1:length(t)]
2-element Array{Any,1}:
1
3
julia> zero(eltype(e)) # this is what happens inside var
ERROR: `convert` has no method matching convert(::Type{MathConst{:e}}, ::Int64)
in zero at number.jl:47
julia> f(t) = [sum(t[1:i]) for i=1:length(t)]
f (generic function with 1 method)
julia> f(t)
2-element Array{Int64,1}:
1
3
julia> s =f(t)
2-element Array{Int64,1}:
1
3
julia> zero(eltype(s))
0
Generally it is good to put all your stuff into functions so Julia can
do its type inference. Better performance and sometime, as in your
case, no errors.
On Tue, 2014-11-18 at 23:27, witek gawlowski <[email protected]> wrote:
> Hello,
> some basic question although i couldn't find an answer easily:
>
> if i run
> using StatsBase
> t = [1:10]
> s = [sum(t[1:i]) for i=1:10]
> var(s)
>
> i get an error: `zero` has no method matching zero(::Type{Any})
>
> I see t is of [a, b, c] type and s is of {a, b, c} type. Can you give me
> the topics to search for?
> also if s is representing a list why do i have to convert it to something
> else (i guess) to calculate its variance?
>
> Regards