# Array comprehension in a quote

**URL:** <https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [January 4, 2024, 9:25am UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333 "2024-01-04T09:25:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [January 4, 2024, 9:25am UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333/1 "2024-01-04T09:25:40Z")

</div>

I want to do an array comprehension from a list of ranges in a quote but I get a syntax error.

```julia
loops = [:(i=1:2), :(j=1:3)]
:([i+j for :(loops...)]) # ideally maps to [i+j for i=1:2, j=1:3]
syntax: invalid iteration specification

```

How can I work around this?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 4, 2024, 9:32am UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333/2 "2024-01-04T09:32:32Z")

</div>

This can’t work I think because macros need to have valid Julia syntax at every step and `:(for something)` is not valid Julia, so `:(for $(something))` also does not work.

(You probably meant to use `$` inside the quote and not `:` but this doesn’t change anything)

I don’t really see how to work around this in your case, since I don’t know what problem you are trying to solve. Can you provide more context? Like what do you want to do and why do you think a macro is the solution?

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [January 4, 2024, 10:20am UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333/3 "2024-01-04T10:20:03Z")

</div>

Interpolation indeed does not seem to work.  
In any case, you can construct the expression explicitly:

```julia-repl

julia> Expr(:comprehension, Expr(:generator, :(i+j), loops...))
:([i + j for i = 1:2, j = 1:3])

julia> eval(ans)
2×3 Matrix{Int64}:
 2 3 4
 3 4 5

```

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [January 4, 2024, 9:44pm UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333/4 "2024-01-04T21:44:20Z")

</div>

The above solution works great though I’m not using it now but I now have a new problem where generated functions apparently don’t work with comprehensions.

My use case it to traverse a complicated nested structure to get some data. Here is the code that creates the quote for the generated function

```julia
h = IDSPath("time_slice/:/profiles_2d/0/psi")
function _getindex_gen2(ids, h::Type{IDSPath{H}}) where {H}
    path = _IMAS_HASH_MAP[H]

    I = :(ids.ids)
    I, indices, ranges = _parse_ids_path(I,path)

    if !isempty(indices)
        reverse!(indices)
        reverse!(ranges)
        for (ind, r) in zip(indices,ranges)
            I = :([$I for $ind in $r]) 
        end
    end
    return I
end
_getindex_gen2(x,h) #-->:([((ids.ids.time_slice[i1]).profiles_2d[0]).psi for i1 = 1:length(ids.ids.time_slice)])

```

My question now is to figure out how to avoid the comprehension entirely but get the same result for the retrieved data i.e. a vector of vectors of vectors… for the data that generally has the same shape but could have different sizes.

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [January 5, 2024, 4:33am UTC](https://discourse.julialang.org/t/array-comprehension-in-a-quote/108333/5 "2024-01-05T04:33:13Z")

</div>

For future readers I found a workaround that works. Basically I have to manually create the arrays like so

```julia
for (ind,r) in zip(inds,ranges)
    a = Symbol("arr",'_',ind)
    I = quote
        $a = let $a
            $ind,r... = $r
            $a = [$I]
            for $ind in r
                push!($a, $I)
            end
            $a
        end
    end
end

```

Not sure if there is a better way but this works.
