- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a by
keyword to maximum
and minimum
#28210
Comments
Makes sense—any ordering-dependent function should support this kind of API. |
Just saw #27639, where |
See also #19190 and #19359. |
Is it fine if we write a function like this ?
|
A solution could be adding a E.g. julia> isless((1,2),(2,1))
true
julia> function Base.isless(t1::Tuple, t2::Tuple; by = first)
a, b = by(t1), by(t2)
isless(a, b) || (isequal(a, b) && isless(tail(t1), tail(t2)))
end
julia> isless((1,2),(2,1))
true
julia> isless((1,2),(2,1), by = i->i[2])
false |
Any implementation that is compatible with I am in favor of using the |
Reviving this one as its related to #34719 .
Here's another way to think about it. Let's just restrict attention to An important reason why
or even
is never going to have a sane type-stable interpretation. I see two options:
function min(a, b; lt=isless, by=identity, order=Forward)
if isconcretetype(promote_type(typeof(a), typeof(b)))
a, b = promote(a, b)
end
isordered = Base.Order.lt(_ord(lt, by, order), a, b)
return isordered ? a : b
end Julia will evaluate I have a preference for this solution because I'm a heavy user of non- |
> passnothing(f) = x->isnothing(x) ? nothing : f(x)
> findmax(passnothing(abs), [-2., -4., nothing]; by=x->isnothing(x) ? -Inf : x) == (4., 2) |
Essentially the same thing that already exists for
sort
: an option to specify a selector function, so that the maximum is determined by whatever the selector function returns, but then themaximum
function returns an element from the original input sequence.For example,
maximum([(a=1,b=2),(a=2,b=1)], by=i->i.b)
would return(a=1,b=2)
.The text was updated successfully, but these errors were encountered: