# How do you iterate over a vector of ranges of different lengths?

**URL:** <https://discourse.julialang.org/t/how-do-you-iterate-over-a-vector-of-ranges-of-different-lengths/97181>\
**Category:** General Usage\
**Created:** [April 6, 2023, 2:11pm UTC](https://discourse.julialang.org/t/how-do-you-iterate-over-a-vector-of-ranges-of-different-lengths/97181 "2023-04-06T14:11:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [April 6, 2023, 2:11pm UTC](https://discourse.julialang.org/t/how-do-you-iterate-over-a-vector-of-ranges-of-different-lengths/97181/1 "2023-04-06T14:11:33Z")

</div>

So I have an arbitrary vector of ranges

```julia
julia> kthNvals(k) = (1:(2^k)) .- 1;

julia> kthNvals.(1:4)
4-element Vector{UnitRange{Int64}}:
 0:1
 0:3
 0:7
 0:15

```

I need to generate all the vectors where each index is a value in the relevant range.  
The hardcoded version looks like this:

```julia
julia> l = [[a,b,c,d] for a in 0:1,b in 0:3,c in 0:7,d in 0:15][:]
1024-element Vector{Vector{Int64}}:
 [0, 0, 0, 0]
 [1, 0, 0, 0]
 [0, 1, 0, 0]
 [1, 1, 0, 0]
 [0, 2, 0, 0]
 [1, 2, 0, 0]
 [0, 3, 0, 0]
 ⋮
 [0, 1, 7, 15]
 [1, 1, 7, 15]
 [0, 2, 7, 15]
 [1, 2, 7, 15]
 [0, 3, 7, 15]
 [1, 3, 7, 15]

```

How do you make this for any `kthNvals.(1:k)`?

Would you do it with a recursive macro or some other metaprogramming feature?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [April 6, 2023, 2:42pm UTC](https://discourse.julialang.org/t/how-do-you-iterate-over-a-vector-of-ranges-of-different-lengths/97181/2 "2023-04-06T14:42:57Z")

</div>

If I understand correctly, this should answer the request

```julia
collect(Base.product(kthNvals.(1:4)...))[:]

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [April 6, 2023, 11:23pm UTC](https://discourse.julialang.org/t/how-do-you-iterate-over-a-vector-of-ranges-of-different-lengths/97181/3 "2023-04-06T23:23:40Z")

</div>

The specific ranges given in the question, are easier to iterate over since they are powers of two, and essentially we can see each row as bit fields in a simple Int. This allows to randomly access this list and each entry in the list by direct caclulation. To make this observation concrete:

```julia
julia> [(i >> ((k*(k-1))>>1))&((1<<k)-1) for i in 0:20, k in 1:4]
21×4 Matrix{Int64}:
 0 0 0 0
 1 0 0 0
 0 1 0 0
 1 1 0 0
 0 2 0 0
 1 2 0 0
 0 3 0 0

```

or in Tuple form:

```julia
julia> [ntuple(k -> (i >> ((k*(k-1))>>1))&((1<<k)-1), Val(4)) for i in 0:20]
21-element Vector{NTuple{4, Int64}}:
 (0, 0, 0, 0)
 (1, 0, 0, 0)
 (0, 1, 0, 0)
 (1, 1, 0, 0)
 (0, 2, 0, 0)
 (1, 2, 0, 0)
 (0, 3, 0, 0)

```

or in iterator form:

```julia
julia> myiter = Iterators.map(i->ntuple(k -> (i >> ((k*(k-1))>>1))&((1<<k)-1), Val(4)), 0:(1<<(4*3÷2)))
Base.Generator{UnitRange{Int64}, var"#23#25"}(var"#23#25"(), 0:64)

julia> Iterators.take(myiter,7) |> collect
7-element Vector{NTuple{4, Int64}}:
 (0, 0, 0, 0)
 (1, 0, 0, 0)
 (0, 1, 0, 0)
 (1, 1, 0, 0)
 (0, 2, 0, 0)
 (1, 2, 0, 0)
 (0, 3, 0, 0)

```
