# A type of collection inside ()

**URL:** <https://discourse.julialang.org/t/a-type-of-collection-inside/98371>\
**Category:** New to Julia\
**Created:** [May 5, 2023, 7:47am UTC](https://discourse.julialang.org/t/a-type-of-collection-inside/98371 "2023-05-05T07:47:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [May 5, 2023, 7:47am UTC](https://discourse.julialang.org/t/a-type-of-collection-inside/98371/1 "2023-05-05T07:47:19Z")

</div>

We know array comprehension, for example

`temp = [i for i in collect(1:3)]`

Instead of brackets I now use parentheses:

`temp = (i for i in collect(1:3))`

This is not a tuple, so temp[1] cannot be called. Its type is “Base.Generator{Vector{Int64}, typeof(identity)}”. Yet, `2 in temp` can be called. Do such objects have a special name and uses?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [May 5, 2023, 8:10am UTC](https://discourse.julialang.org/t/a-type-of-collection-inside/98371/2 "2023-05-05T08:10:10Z")

</div>

They are called “generators” ([Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions)).  
You can see it as a generic iterable collection with strictly sequential access (therefore, no indexing, but checking the presence of an element is possible by a linear search).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [May 5, 2023, 8:12am UTC](https://discourse.julialang.org/t/a-type-of-collection-inside/98371/3 "2023-05-05T08:12:12Z")

</div>

Also note that you don’t need to `collect` the range:

```julia
julia> (i for i ∈ 1:3)
Base.Generator{UnitRange{Int64}, typeof(identity)}(identity, 1:3)

julia> 2 ∈ ans
true

```

Iterators are useful and performant and as a rule of thumb you shouldn’t `collect` them unless you absolutely have to, as that’s bad for performance.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 5, 2023, 8:47am UTC](https://discourse.julialang.org/t/a-type-of-collection-inside/98371/4 "2023-05-05T08:47:03Z")

</div>

> [@nilshg](#):
>
> you don’t need to `collect` the range

It’s particularly undesirable here, @Mastomaki, since generators are ‘lazy’ constructs, it sort of ruins the point to eagerly `collect` a lazy collection inside a lazy generator:

```julia
julia> @btime (i for i in collect(1:1000)); # takes quite a bit of time
  525.758 ns (1 allocation: 7.94 KiB)

julia> @btime (i for i in (1:1000)); # takes no time at all
  1.100 ns (0 allocations: 0 bytes)

```

Actually, in your particular example, you wouldn’t need the generator at all, you would just directly write

```julia
temp = 1:3

```
