4 ms·
What's the deal with the lack of conditionals in comprehensions? Is there a good reason that the Julia devs didn't include that, or is it just something that's
by irremediable 12y ago
What's the deal with the lack of conditionals in comprehensions? Is there a good reason that the Julia devs didn't include that, or is it just something that's not been implemented yet?
Edit: after a little searching, I found this.
> Filters and guards don't mix with multidimensional comprehensions. For 1-d comprehensions, I'm not convinced that it's really worth the additional syntax. What's the case for making it part of the syntax instead of just using a filter function?
- omaranto 12y agoJulia'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 12y 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. :)