which fails when somelist is an empty array. The python version returns 0, as I want, for the sum.
What’s the “best” (fastest?) julia replacement here?
Is there any way to do this list comprehension and force the returned type to be e.g., Float64? That would have worked:
sum(zeros(Float64, 0)) = 0.0
The issue is what type of zero to return. For example, if you are summing a list of 3×3 matrices, you don’t want 0, you want a 3×3 matrix of zeros.
By default, it tries to get the type of zero from the element type of the array. However, [Lc[x] for x in []] returns an Any[] array, and Any provides no information about the type of zero. (See also https://github.com/JuliaLang/julia/issues/27766).
One possibility would be to simply give the array comprehension an explicit type, e.g.
sum(Float64[Lc[x] for x in somelist])
Another argument is to use reduce, which allows you to specify an explicit init argument:
reduce(+, [Lc[x] for x in somelist], init=0.0)
If you are trying to write type-generic code, that could be invoked for different types of Lc, then you need to be more careful and use eltype(Lc) so that your code is both type-stable and responsive to the types of the input.
Alternatively, you can simply do
sum(Lc[somelist])
where somelist is an array of indices (possibly empty), and it will do the right thing: you can index an array with another array to get a slice/subset.
You actually don’t need the comprehension here. Ditch the square brackets and let the sum function operate on a generator and you’ll save some allocations. This is not a solution to your empty list case, though.