# findfirst for iterator-defined collections

**URL:** <https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792>\
**Category:** Internals & Design\
**Created:** [August 20, 2018, 10:00pm UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792 "2018-08-20T22:00:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wgm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wgm/32/114_2.png) [@wgm](https://discourse.julialang.org/u/wgm)\
**Post date:** [August 20, 2018, 10:00pm UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/1 "2018-08-20T22:00:28Z")

</div>

I would like to be able to use findfirst over a collection defined by an iterator. For example

```julia
findfirst(x->x==5, collect(Iterators.flatten((1:2,5:7)))) == 3

```

but there is no method for

```julia
findfirst(x->x==5, Iterators.flatten((1:2,5:7)))

```

Am I missing an idiomatic way to accomplish what I’d like without reifying the collection? If not, it might make a nice addition to IterTools. I’m using v0.7.0.

---

<div class="post-metadata">

**Author:** ![garrison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garrison/32/209519_2.png) [@garrison](https://discourse.julialang.org/u/garrison)\
**Post date:** [August 20, 2018, 10:09pm UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/2 "2018-08-20T22:09:46Z")

</div>

I believe this should really be implemented in Base. If it were in `IterTools`, it would be considered type piracy.

---

<div class="post-metadata">

**Author:** ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)\
**Post date:** [August 21, 2018, 1:17pm UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/3 "2018-08-21T13:17:39Z")

</div>

What does the return value indicate? It’s not an index, since a general iterator cannot be indexed. So to use it, you would anyway need to collect your iterator in one way or another. Or your iterator should be indexable, in which case it could probably be a subtype of `AbstractVector`.

Anyway, given the function `f` that needs to evaluate to `true`, it’s as simple as this:

```julia
for (i,v) in enumerate(iterator)
   f(v) && return i
end
```

---

<div class="post-metadata">

**Author:** ![wgm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wgm/32/114_2.png) [@wgm](https://discourse.julialang.org/u/wgm)\
**Post date:** [August 21, 2018, 4:23pm UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/4 "2018-08-21T16:23:22Z")

</div>

Good point Jutho. My use case was to check wheher some element of the collection satisfied a particular property so the better approach is to use any(predicate,collection) which does accept iterators.

Thanks!

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [August 22, 2018, 11:25am UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/5 "2018-08-22T11:25:35Z")

</div>

This has been raised a few times before, and a solution which has been proposed is to provide a (say) `withlinearindices` wrapper which would do as if the input iterator used linear indices starting from 1.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [November 26, 2019, 11:43am UTC](https://discourse.julialang.org/t/findfirst-for-iterator-defined-collections/13792/6 "2019-11-26T11:43:17Z")

</div>

Thinking about it, it is already there is some sense:

```julia
first(Iterators.filter(x->x==5, Iterators.flatten((1:2,5:7))))
5

first(Iterators.filter(x->x[2]==5, enumerate(Iterators.flatten((1:2,5:7)))))
(3, 5)

```

Just define

```julia
getfirst = first∘Iterators.filter

```
