# Using CartesianRange, flattening before collect

**URL:** <https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 17, 2017, 12:06pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150 "2017-02-17T12:06:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 17, 2017, 12:06pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150/1 "2017-02-17T12:06:19Z")

</div>

I need to iterate through all possible indices of an array for a unit test, without constructing the array, ie just from the indices. Is

1. constructing a `CartesianRange`,
2. then iterating through it and extracting the `I` slot

the way to do this? I am asking because it is not documented, so I am not sure if this is an interface I should rely on being reasonably stable. Example:

```julia
function simulate_index_weights(_size...)
    vec(collect(c.I => rand() for c in CartesianRange(_size)))
end

simulate_index_weights(2,3,4)

```

Also, is there a more elegant way of discarding shape information from a generator before collecting, so that I would not have to call `vec`?

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [February 17, 2017, 2:28pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150/2 "2017-02-17T14:28:07Z")

</div>

Very stable and very much the way forward. A docstring PR would be most appreciated!

I wouldn’t say there’s any particular reason to pass back `c.I`, just return `c`. It gives you faster indexing because you don’t have to splat a tuple.

I think calling `vec` is your best best. What seems inelegant about that?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [February 17, 2017, 3:31pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150/3 "2017-02-17T15:31:37Z")

</div>

Thanks for the help.

> [@tim.holy](#):
>
> I think calling vec is your best best. What seems inelegant about that?

Perhaps I should rephrase: is there a way to take a generator with a shape, and convert it to a “flat” generator, without collecting?

Just curious.

---

<div class="post-metadata">

**Author:** ![PabloZubieta](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@PabloZubieta](https://discourse.julialang.org/u/PabloZubieta)\
**Post date:** [February 17, 2017, 4:03pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150/4 "2017-02-17T16:03:14Z")

</div>

Some options are

```julia
copy!(similar(Array{eltype(C)}, length(C)), C)
copy!(Vector{eltype(C)}(length(C)), C)
Vector{eltype(C)}(length(C)) .= C

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [February 19, 2017, 8:13pm UTC](https://discourse.julialang.org/t/using-cartesianrange-flattening-before-collect/2150/5 "2017-02-19T20:13:04Z")

</div>

> [@Tamas\_Papp](#):
>
> is there a way to take a generator with a shape, and convert it to a “flat” generator, without collecting?

Perhaps something like this?

```julia
simulate_index_weights(_size...) = (c.I => rand() for c in CartesianRange(_size))

```

```julia
for x in simulate_index_weights(2,3,4)
    println(x)
end

```
