# Why does this not work?

**URL:** <https://discourse.julialang.org/t/why-does-this-not-work/54923>\
**Category:** General Usage\
**Created:** [February 9, 2021, 12:45pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923 "2021-02-09T12:45:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [February 9, 2021, 12:45pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923/1 "2021-02-09T12:45:53Z")

</div>

I wanted to find the first line of a matrix which is all `-1` s. I am surprised the following does not work.  
What is going on here?

```julia
julia> findfirst(l->all(y->y==-1,l),eachrow([1 1;-1 -1]))
ERROR: MethodError: no method matching keys(::Base.Generator{Base.OneTo{Int64},Base.var"#192#193"{Array{Int64,2}}})
Closest candidates are:
  keys(::Cmd) at process.jl:638
  keys(::Core.SimpleVector) at essentials.jl:603
  keys(::LibGit2.GitTree) at /home/jmichel/.julia/packages/Revise/nWJXk/src/git.jl:52
  ...
Stacktrace:
 [1] pairs(::Base.Generator{Base.OneTo{Int64},Base.var"#192#193"{Array{Int64,2}}}) at ./abstractdict.jl:134
 [2] findfirst(::var"#5#7", ::Base.Generator{Base.OneTo{Int64},Base.var"#192#193"{Array{Int64,2}}}) at ./array.jl:1854
 [3] top-level scope at REPL[1]:1

```

---

<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:** [February 9, 2021, 12:50pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923/2 "2021-02-09T12:50:36Z")

</div>

Broadcast?

```julia
julia> findfirst.(l->all(y->y==-1,l),eachrow([1 1;-1 -1]))
2-element Vector{Union{Nothing, Int64}}:
  nothing
 1

```

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [February 9, 2021, 1:05pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923/3 "2021-02-09T13:05:54Z")

</div>

The question is not how to do it differently, but why does it not work? By the way, your solution is less efficient than the desired one, since it checks _all_ lines of the matrix (and allocates a vector of booleans).

---

<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:** [February 9, 2021, 1:11pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923/4 "2021-02-09T13:11:07Z")

</div>

Sorry: the reason is that `findfirst` doesn’t work with generators, as the error message suggests: [https://github.com/JuliaLang/julia/issues/16884](https://github.com/JuliaLang/julia/issues/16884)

```julia
julia> findfirst(x -> x > 5, (i for i ∈ 1:10))
ERROR: MethodError: no method matching keys(::Base.Generator{UnitRange{Int64}, typeof(identity)})

```

Hence one other way (but again presumably less efficient than it could theoretically be if we iterated over the generator) is to collect:

```julia
julia> findfirst(l->all(y->y==-1,l),collect(eachrow([1 1;-1 -1])))
2

```

I’m sure someone will be along soon to provide an efficient solution…

---

<div class="post-metadata">

**Author:** ![Jean\_Michel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jean_michel/32/8282_2.png) [@Jean\_Michel](https://discourse.julialang.org/u/Jean_Michel)\
**Post date:** [February 9, 2021, 1:19pm UTC](https://discourse.julialang.org/t/why-does-this-not-work/54923/5 "2021-02-09T13:19:47Z")

</div>

Thank for the pointer to the ticket. It does not seem too difficult to implement findfirst just relying on the  
iteration protocol, just having a counter, so I do not understand why it has not been implemented for all iterators…
