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).
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.
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).
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.
You are actually right, since we have minimum
, then one could have also switched the result entries, yes.
I’m visiting Milan, Italy, at the moment. I encountered an elevator that has the following sequence:
[-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?
“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?”
Or “Where is the minimum and what is it?”
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> 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)
Wow. Didn’t know that. That is pretty unfortunate …
Definitely unfortunate. There is an issue to fix this in the hypothetical far-future Julia 2.0
Apparently it would break broadcasting, which seems like a good thing imo
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> 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.
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?