# Return value of findmin/findmax

**URL:** <https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567>\
**Category:** General Usage\
**Created:** [February 13, 2023, 8:09am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567 "2023-02-13T08:09:05Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 11, 2023, 8:43pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/1 "2023-01-11T20:43:04Z")

</div>

I would love if functions like `findmin` and `findmax` used a named tuple for their return value, because I can never remember which element of the return value is which (and code like `findmax(arr)[2]` has high mental load as a result).

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 13, 2023, 8:09am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/2 "2023-02-13T08:09:05Z")

</div>

> [@uniment](#):
>
> I would love if functions like `findmin` and `findmax` did this, because I can never remember which element of the return value is which (and code like `findmax(arr)[2]` has high mental load as a result).

Come to think of it, maybe the reason `findmin` and `findmax` have such high mental load is because their return value is backwards.

Everywhere else, key-value pairs appear as `k=>v` and index-value pairs as `(i,x)` (e.g. as from `enumerate`), but `findmin` and `findmax` return index and value in opposite order.

![WhyWhateverGIF](https://global.discourse-cdn.com/julialang/original/3X/1/a/1a6fcf560f38b46f61d481d39acf6d6fe0abe5ac.gif)

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [February 13, 2023, 10:36am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/3 "2023-02-13T10:36:25Z")

</div>

I would argue that this is because the minimum is the main focus of the function, otherwise it might be called `findminindex` if the index (key) came first?

For me the mental load here is that though it is called `findmin` is _also_ does `findminindex` (which computationally of course makes sense).

---

<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:** [February 13, 2023, 10:53am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/4 "2023-02-13T10:53:48Z")

</div>

> [@kellertuer](#):
>
> I would argue that this is because the minimum is the main focus of the function

I don’t quite agree with that; the main focus is finding it: “_Where_ is the minimum?”

You already have `minimum` to tell you _what_ the minimum is, so I would expect `findmin` to have a different focus, namely location.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [February 13, 2023, 11:02am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/5 "2023-02-13T11:02:40Z")

</div>

You are actually right, since we have `minimum`, then one could have also switched the result entries, yes.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [February 13, 2023, 12:08pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/6 "2023-02-13T12:08:00Z")

</div>

I’m visiting Milan, Italy, at the moment. I encountered an elevator that has the following sequence:

```julia
[-3 -2 -1 0 'A' 1 2 3 4]

```

On `findmin` and `findmax`, how breaking would it be if it returned a `NamedTuple` instead or perhaps via an additonal argument?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [February 13, 2023, 12:54pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/7 "2023-02-13T12:54:15Z")

</div>

> “_Where_ is the minimum?”

This is best answered by `argmin`, because it only returns the position.

`findmin` really tries to answer: “What is the minimum and where is it?”

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [February 13, 2023, 12:57pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/8 "2023-02-13T12:57:21Z")

</div>

> [@fatteneder](#):
>
> `findmin` really tries to answer: “What is the minimum and where is it?”

Or “Where is the minimum and what is it?” 😉

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [February 13, 2023, 1:00pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/9 "2023-02-13T13:00:10Z")

</div>

> [@fatteneder](#):
>
> This is best answered by `argmin`, because it only returns the position.

`argmin` isn’t really consistent here: sometimes it returns the position, sometimes the element. `findmin` is consistent and always does the same, and is less error-prone because of that.

```julia
julia> A = [5, 1, 3, -10]

# index:
julia> argmin(A)
4
# value:
julia> argmin(abs, A)
1

# (value, index):
julia> findmin(A)
(-10, 4)
# also (value, index):
julia> findmin(abs, A)
(1, 2)

```

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [February 13, 2023, 1:04pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/10 "2023-02-13T13:04:00Z")

</div>

Wow. Didn’t know that. That is pretty unfortunate …

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [February 13, 2023, 2:05pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/11 "2023-02-13T14:05:09Z")

</div>

Definitely unfortunate. There is an issue to fix this in the hypothetical far-future [Julia 2.0](https://github.com/JuliaLang/julia/issues/48502)

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [February 13, 2023, 2:50pm UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/12 "2023-02-13T14:50:39Z")

</div>

see discussion: [Unintuitive findmin and findmax · Issue #39203 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/39203)

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 14, 2023, 1:02am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/13 "2023-02-14T01:02:54Z")

</div>

> [@mkitti](#):
>
> how breaking would it be if it returned a `NamedTuple` instead

Apparently [it would break broadcasting](https://github.com/JuliaLang/julia/pull/41339#issuecomment-867326932), which seems like a good thing imo

---

<div class="post-metadata">

**Author:** ![Jollywatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jollywatt/32/202198_2.png) [@Jollywatt](https://discourse.julialang.org/u/Jollywatt)\
**Post date:** [February 14, 2023, 2:07am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/14 "2023-02-14T02:07:35Z")

</div>

I agree; the return values of `findmin`/`findmax` look backwards.

Moreover, it seems a missed opportunity to return a `Pair`, which would make the `key => value` order semantically clear.

```julia
julia> findmin("hello")
('e', 2)

julia> findmin′(iter) = Pair(reverse(findmin(iter))...)
findmin′ (generic function with 1 method)

julia> findmin′("hello")
2 => 'e'

```

Pairs are just as convenient as tuples for destructuring, etc.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 14, 2023, 2:22am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/15 "2023-02-14T02:22:59Z")

</div>

> [@Jollywatt](#):
>
> it seems a missed opportunity to return a `Pair`

![JensenAcklesPointingGIF](https://global.discourse-cdn.com/julialang/original/3X/8/e/8ee2f3e0fcb8e318eefeb9b45fc6ea118297e3fb.gif)

---

<div class="post-metadata">

**Author:** ![TheLateKronos](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thelatekronos/32/12824_2.png) [@TheLateKronos](https://discourse.julialang.org/u/TheLateKronos)\
**Post date:** [February 20, 2023, 5:42am UTC](https://discourse.julialang.org/t/return-value-of-findmin-findmax/94567/16 "2023-02-20T05:42:58Z")

</div>

I completely agree that changing the order/type of the output would be an improvement. Is it at all in the cards to make such a breaking change for 2.0 though?
