`findmin` with arbitrary comparison operator?

212 views
Skip to first unread message

Erik Schnetter

unread,
Nov 29, 2015, 1:04:49 PM11/29/15
to julia...@googlegroups.com
Is there a version of `findmin` that allows passing in an arbitrary comparison operator?

In throw-away code, I often have an array containing tuples of both the result and some additional operator. I want to find the minimum and its location, but want to ignore the additional information. I would like to use a user-defined comparison operator that looks only at one of the tuple elements, yet `findmin` doesn't seem to offer this.

Júlio Hoffimann

unread,
Nov 29, 2015, 6:08:56 PM11/29/15
to julia-users
Hi Erik,

All you need to do is specialize the < operator for your tuple type.

Please check the definition of findmin by running:

@edit findmin([1,2,3])

You'll see the implementation is quite trivial. Let us know if you weren't able to make it work.

Best,
-Júlio

Erik Schnetter

unread,
Dec 1, 2015, 10:50:49 AM12/1/15
to julia...@googlegroups.com
Júlo

My "tuple type" is an actual tuple. While I could create custom comparison operator for e.g. `Tuple{Int, Int, AbstractString}`, this would be unorthodox, as this comparison operator would be globally visible. This could break many things, such as e.g. dictionaries that hold tuples of arbitrary type.

-erik

Júlio Hoffimann

unread,
Dec 1, 2015, 11:23:07 AM12/1/15
to Julia Users

Hi Erik,

What I meant was a derived type from Julia's Tuple type. Though I agree with you that this may be too much of an overhead for such a simple task.

I'll open an issue on GitHub to ask if others are ok with an additional predicate option a la C++:

findmin(array, pred=<)

If they are all positive, I'll add it for you.

Best,
-Júlio

Dan

unread,
Dec 1, 2015, 11:57:17 AM12/1/15
to julia-users
suggestion: call the named argument `lt` to match the argument of `sort`.

Júlio Hoffimann

unread,
Dec 1, 2015, 12:20:53 PM12/1/15
to Julia Users
The issue on GitHub: https://github.com/JuliaLang/julia/issues/14216

Thanks,
-Júlio

Josh Langsfeld

unread,
Dec 1, 2015, 12:38:52 PM12/1/15
to julia-users
Doesn't 'min' imply/require the usage of '<' ? A whole lot of methods would need the extra argument added for consistency, including min, minimum, maximum, etc...

Could a workable solution be to define a function that maps a tuple to a real number instead of a comparison function and do 'findmin(map(score_func, my_tuples))' ?

Erik Schnetter

unread,
Dec 1, 2015, 9:45:54 PM12/1/15
to julia...@googlegroups.com
One of the tuple entries is the number by which I'd like to sort, so the score_func is trivial. However, in the end I want not only the minimum value, but also the additional tuple elements, which would be stripped off by the score_func.

-erik

Josh Langsfeld

unread,
Dec 2, 2015, 11:23:15 AM12/2/15
to julia-users
That's true; my_tuples[findmin(map(score_func, my_tuples))[2]] is a fair bit uglier than just interesting the map.

Júlio Hoffimann

unread,
Dec 6, 2015, 11:52:22 PM12/6/15
to Julia Users
Actually, I am starting to like the simple approach of filtering the tuple suggested by Josh. It's cleaner and probably faster in general.

scores = map(t -> t[1], mytuplelist) # first tuple element as score
idx = indmin(scores)
mintuple = mytuplelist[idx]

I will close the issue.

Best,
-Júlio
Reply all
Reply to author
Forward
0 new messages