# Findfirst that returns the value/element

**URL:** <https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542>\
**Category:** New to Julia\
**Created:** [January 23, 2018, 10:53am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542 "2018-01-23T10:53:40Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [January 23, 2018, 10:53am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/1 "2018-01-23T10:53:40Z")

</div>

Sorry for the maybe very trivial question but I couldn’t find it anywhere in the documentation.  
Is there a function that does this:  
`filter(func , iterable)[1]`?

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [January 23, 2018, 11:03am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/2 "2018-01-23T11:03:28Z")

</div>

[https://docs.julialang.org/en/stable/stdlib/arrays/#Base.findfirst-Tuple{Function,Any}](https://docs.julialang.org/en/stable/stdlib/arrays/#Base.findfirst-Tuple%7BFunction,Any%7D)

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [January 23, 2018, 11:04am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/3 "2018-01-23T11:04:32Z")

</div>

This doesn’t return the element, only index as far as I understand?

---

<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:** [January 23, 2018, 11:29am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/4 "2018-01-23T11:29:07Z")

</div>

Then you can retrieve the element using `getindex` (`[]`). If that is costly for some reason, you can roll your own very easily:

```julia
function findfirst_and_elt(pred, itr)
    for (index, elt) in enumerate(itr)
        if pred(elt)
            return index, elt
        end
    end
    nothing
end

x = 'a' .+ (0:9)

findfirst_and_elt(z -> z == 'f', x)

```

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [January 23, 2018, 11:36am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/5 "2018-01-23T11:36:04Z")

</div>

Yes, sure, it just seemed that since it is such a trivial extension and (maybe) used a lot, there would be a built in function, and that I just couldn’t locate it. But so there seems to be none. Okay thanks!

---

<div class="post-metadata">

**Author:** ![saschatimme](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/saschatimme/32/10313_2.png) [@saschatimme](https://discourse.julialang.org/u/saschatimme)\
**Post date:** [January 23, 2018, 11:44am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/6 "2018-01-23T11:44:23Z")

</div>

The problem ist the case where for no element the predicate is satisfied. What do you return then? With the optimizations in 0.7 one could return `nothing`, but previously this would have been a major performance hit.

---

<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:** [January 23, 2018, 11:47am UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/7 "2018-01-23T11:47:41Z")

</div>

There are zillion variations on these functions, and not each of them can end up in `Base`. The best would be supporting a vocabulary which can be combined to obtain this, but I have no good suggestion ATM.

---

<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:** [January 23, 2018, 12:30pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/8 "2018-01-23T12:30:35Z")

</div>

While cleaning up the search and find functions, we realized we already have `findmin` and `findmax` which return both the index and the value. So it could make sense to add variants of the `find*` functions doing that, but that will probably won’t happen for 0.7/1.0. We also haven’t been able to find a good name for functions following this pattern. See [this issue](https://github.com/JuliaLang/julia/issues/24865).

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [January 23, 2018, 12:53pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/9 "2018-01-23T12:53:59Z")

</div>

Yes this was exactly why I thought there would be something! I don’t directly know a solution to the naming. I think for my own I’ll call it `firstval` or `findfirstval`.

> [@saschatimme](#):
>
> The problem ist the case where for no element the predicate is satisfied. What do you return then? With the optimizations in 0.7 one could return nothing, but previously this would have been a major performance hit.

I think the same idea as you would get for a `findfirst` with no solutions, `nothing` seems to make sense in that regard.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 23, 2018, 1:13pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/10 "2018-01-23T13:13:03Z")

</div>

> [@louisponet](#):
>
> I think the same idea as you would get for a findfirst with no solutions, nothing seems to make sense in that regard.

The problem is type instability. If you get back the index, you can check whether it is inbounds in order to see whether you have a first value. If the API instead returned e.g. a tuple (idx\_first, val\_first) then this is type-unstable (either `Tuple{Int64, eltype(V)}` or `Tuple{Int64, Void}`). Even worse, if val\_first is not bitstype, then this might allocate.

Hence, the state of the compiler constrains API design; I am very happy that the base API is mostly designed in a way that encourages people to write code that compiles well. In fact, I think julia does not go far enough in this direction (convenience functions that cannot be compiled fast should not be in base, imho; e.g. enumerate should not exist in base until it can be guaranteed to not allocate).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 23, 2018, 1:23pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/11 "2018-01-23T13:23:18Z")

</div>

> [@foobar\_lv2](#):
>
> The problem is type instability.

To be fair, multiple functions on 0.7 are type unstable, for example `findfirst`.

> [@foobar\_lv2](#):
>
> (convenience functions that cannot be compiled fast should not be in base, imho; e.g. enumerate should not exist in base until it can be guaranteed to not allocate)

Guess we have to remove tuples from Base.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 23, 2018, 2:17pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/12 "2018-01-23T14:17:38Z")

</div>

> [@kristoffer.carlsson](#):
>
> To be fair, multiple functions on 0.7 are type unstable, for example findfirst.

Sure; and I guess that the 0.6 compiler not being as good at producing fast code for `Union{T,Void}` is the reason for it not being type unstable for arrays before.

> [@kristoffer.carlsson](#):
>
> Guess we have to remove tuples from Base.

Sorry, I did not want to sound disparaging here. I am not advocating removal of anything in Base; also, tuples are sometimes unavoidable. Just saying that enumerate in 0.6 is very dangerous for new users and had very limited upsides (upside: save up to two lines of code; downside: bad performance when not bitstype; one can (always?) write `for (i,v) in enumerate(X) body end` as `i=0; for v in X i += 1; body end`.). This does not mean that I argue for a removal of enumerate, but I would have argued against its introduction if I had been around back then. Any enumerate is a landmine waiting for unsuspecting users to feed a surprise non-bitstype, and realistically these landmines will only get defused through compiler updates.

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [January 23, 2018, 2:22pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/13 "2018-01-23T14:22:00Z")

</div>

I agree on the notion that code inside Base should be type stable, I actually didn’t know this was not true. I think that I also maybe wouldn’t mind less functionality if all if the built in functionality gives you type stability. The point is that when I have code that is a lot slower than I would expect, I would probably never think that it is coming from a built-in function.

On the whole Void thing, do I understand correctly that the new `Some` type is the answer to this? Is it like a `Void` that is type specific?

---

<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:** [January 23, 2018, 3:22pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/14 "2018-01-23T15:22:26Z")

</div>

> [@louisponet](#):
>
> I think for my own I’ll call it firstval or findfirstval.

I kind of like `getfirst`.

`findfirst` _finds_ the location of the first element, and `getfirst` retrives it. It also reminds me of `getindex`, `getfield`, etc. that also retrieve values.

---

<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:** [January 23, 2018, 3:52pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/15 "2018-01-23T15:52:30Z")

</div>

> [@foobar\_lv2](#):
>
> Any enumerate is a landmine waiting for unsuspecting users to feed a surprise non-bitstype, and realistically these landmines will only get defused through compiler updates.

Julia _allows_ programmers to write very efficient code, for which type stability is helpful, but this does not mean that the user is forced to do this all the time. That would be very inconvenient. Expressive constructs have their place even when not optimized to squeeze out the last bit of performance.

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [January 23, 2018, 4:01pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/16 "2018-01-23T16:01:41Z")

</div>

I totally agree. It’s just that the advantage of enumerate in expressiveness is so trivial that it doesn’t justify the landmine, imho; but that ship has sailed anyway.

---

<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:** [January 23, 2018, 4:21pm UTC](https://discourse.julialang.org/t/findfirst-that-returns-the-value-element/8542/17 "2018-01-23T16:21:07Z")

</div>

> [@louisponet](#):
>
> On the whole Void thing, do I understand correctly that the new Some type is the answer to this? Is it like a Void that is type specific?

No, `Some{T}` is just a very simple type which wraps a value of type `T` to allow distinguishing `Some(nothing)` from `nothing`. It has nothing special, and is mostly useful with `Union{Some{T}, Nothing}`.
