# First element of iteration satisfying a predicate

**URL:** https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617
**Category:** General Usage
**Tags:** question
**Created:** [April 5, 2021, 1:58pm UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617 "2021-04-05T13:58:13Z")
**Posts on this page:** 6
**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: [April 5, 2021, 1:58pm UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/1 "2021-04-05T13:58:13Z")

</div>

I often find that I want to use something like `findfirst` on iterables that have no (or inefficient) random access, something like

```julia
function satisfies_first(predicate, itr)
    for i in itr
        predicate(i) && return Some(i)
    end
    nothing
end

julia> satisfies_first(iseven ∘ first, ((i, 2*i) for i in 1:10))
Some((2, 4))

```

It is pretty trivial to code this, but does this exist in a package somewhere? Or `Base`? I found [this discussion](https://discourse.julialang.org/t/standard-way-to-get-first-element-of-iterator-satisfying-predicate/25038/) about something similar, but I want to be careful about corner cases (element not found).

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [April 5, 2021, 3:25pm UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/2 "2021-04-05T15:25:38Z")

</div>

Possibly just chain `Iterators.filter(==(5), [1 2 3 4 5]) |> x -> isempty(x) ? nothing : first(x)`?

---

<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: [April 6, 2021, 11:28am UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/3 "2021-04-06T11:28:58Z")

</div>

Sure, but that’s quite a mouthful for something so basic (and also note that it should give back `Something(first(x))`).

I will wait for more replies, then consider contributinga function not unlike the above to

[https://github.com/JuliaCollections/IterTools.jl](https://github.com/JuliaCollections/IterTools.jl)

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [April 6, 2021, 11:47am UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/4 "2021-04-06T11:47:40Z")

</div>

Maybe a bit nicer:

```julia
itr = ((i, 2i) for i in 1:10)
predicate = isodd ∘ first

using Base.Iterators
first(dropwhile(predicate, itr))

```

But this breaks if the collection is empty (there is no first to collect). Yea, nm. I’ll post this anyways…

---

<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: [April 6, 2021, 12:02pm UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/5 "2021-04-06T12:02:38Z")

</div>

I would perhaps define the generally useful

```julia
function someiterate(iter)
           ϕ = iterate(iter)
           ϕ === nothing && return nothing
           return Some(first(ϕ))
       end

```

instead and then it becomes

```julia
someiterate(dropwhile(predicate, itr))

```

you stay a bit longer in the “iterator-world”

---

<div class="post-metadata">

### Author: ![non-Jedi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-jedi/32/3645_2.png) [@non-Jedi](https://discourse.julialang.org/u/non-Jedi)
#### Post date: [April 6, 2021, 12:08pm UTC](https://discourse.julialang.org/t/first-element-of-iteration-satisfying-a-predicate/58617/6 "2021-04-06T12:08:39Z")

</div>

People seemed open to the idea of adding that kind of functionality to Base, but I didn’t pursue it through until the PR was merged.

[https://github.com/JuliaLang/julia/pull/37119](https://github.com/JuliaLang/julia/pull/37119)
