# Default return value for find functions when no match found

**URL:** <https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035>\
**Category:** General Usage\
**Created:** [May 21, 2018, 4:52am UTC](https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035 "2018-05-21T04:52:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 21, 2018, 4:52am UTC](https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035/1 "2018-05-21T04:52:57Z")

</div>

For v0.7 the `find` functions return `nothing` when no match is found.

Would it be convenient to provide a default parameter to return if no match is found, similar to the `get` function?

For example:

```julia
findlast(!iszero, A, 0)

```

would return 0 if there is no match.

(then effectively `findlast(pred, A) = findlast(pred, A, nothing)`

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 21, 2018, 5:48am UTC](https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035/2 "2018-05-21T05:48:02Z")

</div>

Seems like you could define a similar one line function that does what you want. If the base function returns nothing, you set it to the extra parameter.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 21, 2018, 9:08pm UTC](https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035/3 "2018-05-21T21:08:13Z")

</div>

The issue with that would be efficiency.  
For strings, for example, where any valid index is guaranteed to be \> 0, using 0 for the sentinel can be more efficient than returning `nothing`, and then to have to test for `nothing` just to turn it back into a `0`, seems sad.  
(esp. since some of the internal code in base actually returns 0, and then checks for that,  
and returns `nothing` instead!)

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 22, 2018, 2:58am UTC](https://discourse.julialang.org/t/default-return-value-for-find-functions-when-no-match-found/11035/4 "2018-05-22T02:58:05Z")

</div>

Well of course I can define my own methods, but my question was would this be more widely useful.

Is so, there could be a standard implementation in Base, which would obviate separate custom implementations and promote idiomatic use.

In any case, here’s what I’ve defined for my purpose:

v0.6

```julia
Base.findlast(pred::Function, A, default) = 
    (i = findlast(pred, A); i in linearindices(A) ? i : default)

```

v0.7

```julia
Base.findlast(pred::Function, A, default) =
    (i = findlast(pred, A); i == nothing ? default : i)

```

And similarly for other `find` functions.
