3 ms·
Julia's comprehensions are multidimensional array comprehensions so allowing filtering doesn't make much sense because the result needs to be rectangular and fi
by omaranto 13y ago
Julia's comprehensions are multidimensional array comprehensions so allowing filtering doesn't make much sense because the result needs to be rectangular and filtering can create arbitrary holes.
For a one-dimensional array comprehension it does make sense to allow filtering and this can be easily expressed in Julia by making a coroutine that produces the values and collecting its output in an array.
Multidimensional (here multi=2) array result:
julia> [i+j for i=1:3, j=1:3]
3x3 Array{Int32,2}:
2 3 4
3 4 5
4 5 6
One dimensional array result, with filtering:
julia> collect(@task for i=1:6; if i%3>0; produce(i); end; end)
4-element Array{Any,1}:
1
2
4
5
Notice that this last technique is more powerful than typical comprehensions, since it doesn't just add the possibility of filtering but arbitrary code to generate the elements.
- irremediable 13y agoThanks for the further info! I'd managed to figure out that it was because of multidimensional arrays (that snippet I quoted) but I didn't immediately see a nice way to achieve the same thing. And now, because you posted it, I do. I'll have to give Julia a go. :)