Using sequential conditions inside sum()

How to define sequential conditions inside sum() ?
For example, embedding the block

tot=0
for j in Set1
     if (i,j) in Set2                 condition1
        if  k in Set3[(i,j)]         condition2
           tot = tot + f(i,j,k)
        end
      end
end

inside sum(), keeping the condition2 dependent on condition1 concisely as follows

sum( f(i,j,k) for j in Set1 if ((i,j) in Set2) if (k in Set3[(i,j)]) )

It can be done, but it is really not good style. Your first way of doing it is almost certainly better.

julia> someset = reshape([1], 1, 1)
1×1 Array{Int64,2}:
 1

# the condition is only true when (i, j, k) == (1,1,1)
julia> sum( f(i,j,k) for i in 1:3 for j in 1:3 for k in 1:3 if (i,j) in [(1,1)] && k in someset[i, j])
3
2 Likes