I am surprised that no one mentions a ternary operator to execute a conditional:
julia> l3 = [sum_is_large((i,j)) ? (i,j) : nothing for i in l1, j in l2] 3x3 Array{Tuple,2}: nothing nothing nothing nothing nothing (2,6) nothing (3,5) (3,6)
or even a normal normal if block inside a compound statement, i.e.
[ (if sum_is_large((x,y)); (x,y); end) for x in l1, y in l2 ]
which gives the same result.
I feel that this result makes much more sense than filter() , because in julia the construction a in A, b in B interpreted by size, and therefore the output is actually an "understanding of the array" with the corresponding dimension, which is clearly In many cases, it would be beneficial and presumably desirable behavior (whether we include conditional or not).
While the filter will always return a vector. Obviously, if you really need a vector result, you can always collect to get the result; or to understand a conditional list such as here, you can simply remove the nothing elements from the array by doing l3 = l3[l3 .!= nothing] .
Presumably, this is still clearer and no less efficient than the filter() approach.
source share