# Generator sometimes works, sometimes not

**URL:** https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701
**Category:** New to Julia
**Tags:** question, generator, iterators
**Created:** [April 4, 2025, 12:08pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701 "2025-04-04T12:08:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [April 4, 2025, 12:08pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701/1 "2025-04-04T12:08:12Z")

</div>

It’s about [Generator Expressions](https://docs.julialang.org/en/v1/manual/arrays/#man-generators).  
I used to only know about `sum(i for i in 1:I)`.  
But today I happened to discover that we can also write `minimum(i for i in 1:I)`.  
I’m thus intrigued:

- Do all function who takes an Vector arg can comprehend grammar like this?

I then came up with a counter-example which fails

```julia
julia> a
4-element Vector{Bool}:
 0
 0
 1
 1

julia> b
4-element Vector{Bool}:
 0
 1
 0
 1

julia> findall(a .& b)
1-element Vector{Int64}:
 4

julia> [(i & j) for (i, j) in zip(a, b)]
4-element Vector{Bool}:
 0
 0
 0
 1

julia> findall( (i & j) for (i, j) in zip(a, b) )
ERROR: MethodError: no method matching keys(::Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}})
The function `keys` exists, but no method is defined for this combination of argument types.

Closest candidates are:
  keys(::Cmd)
   @ Base process.jl:716
  keys(::Base.TermInfo)
   @ Base terminfo.jl:232
  keys(::BenchmarkTools.BenchmarkGroup)
   @ BenchmarkTools K:\judepot1114\packages\BenchmarkTools\1i1mY\src\groups.jl:75
  ...

Stacktrace:
 [1] keys(g::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
   @ Base .\generator.jl:55
 [2] pairs(collection::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
   @ Base .\abstractdict.jl:178
 [3] findall(A::Base.Generator{Base.Iterators.Zip{Tuple{Vector{Bool}, Vector{Bool}}}, var"#331#332"})
   @ Base .\array.jl:2691
 [4] top-level scope
   @ REPL[393]:1

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 4, 2025, 2:03pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701/2 "2025-04-04T14:03:26Z")

</div>

It’s unrelated to syntax and parsing. `findall` is documented to only work on things with indices or keys that work with the methods `keys` and `pairs`. `Zip`-ped generators currently don’t, but if someone implements `keys` method, it could. On the other hand, `minimum` is documented to work with iterators (by implication of its argument name `itr`, so not very formally), which all generators should be.

Just in case, bear in mind that you can get away with omitting the parentheses for generator expressions in function calls when it’s the only argument before `;`. If there are other arguments, proper syntax demands those parentheses, and the consequences are unfortunately silent:

```julia
julia> sum(i for i in 1:10)
55

julia> sum(i for i in 1:10, init=100) # parsed as 2-level nested loop
55

julia> sum((i for i in 1:10), init=100) # parsed as 2 arguments
155

julia> sum(i for i in 1:10; init=100) # ; not in loops, so parsed as 2 arguments
155

```

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [April 4, 2025, 2:33pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701/3 "2025-04-04T14:33:04Z")

</div>

> [@WalterMadelim](#):
>
> Do all function who takes an Vector arg can comprehend grammar like this?

Generators are unrelated to `Vector`s or function calls, they’re just regular ol’ objects:

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

```

There’s some special support in the parser for constructing them, but that’s just syntax sugar, you can use `Iterators` instead. Especially `Iterators.map` and `Iterators.filter`:

```julia-repl
julia> (i for i in 1:3) === Iterators.map(identity, 1:3)
true

```

Furthermore, any user may implement their own iterator types easily:

- [Interfaces · The Julia Language](https://docs.julialang.org/en/v1/manual/interfaces/)

---

<div class="post-metadata">

### Author: ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)
#### Post date: [April 4, 2025, 3:08pm UTC](https://discourse.julialang.org/t/generator-sometimes-works-sometimes-not/127701/4 "2025-04-04T15:08:10Z")

</div>

Thank you, learned a lot🙂
