-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Define extrema
using mapreduce
; support init
#36265
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
Changes from all commits
7b0e72e
c6ffc87
194c329
793a020
de7b9bc
5ebf8ad
2d70b7d
317548a
360b346
841de8a
2533b14
f27f874
ce07cbf
3ceaaae
5669d63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -504,58 +504,6 @@ julia> minmax('c','b') | |||
""" | ||||
minmax(x,y) = isless(y, x) ? (y, x) : (x, y) | ||||
|
||||
""" | ||||
extrema(itr) -> Tuple | ||||
|
||||
Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple. | ||||
|
||||
# Examples | ||||
```jldoctest | ||||
julia> extrema(2:10) | ||||
(2, 10) | ||||
|
||||
julia> extrema([9,pi,4.5]) | ||||
(3.141592653589793, 9.0) | ||||
``` | ||||
""" | ||||
extrema(itr) = _extrema_itr(identity, itr) | ||||
|
||||
""" | ||||
extrema(f, itr) -> Tuple | ||||
|
||||
Compute both the minimum and maximum of `f` applied to each element in `itr` and return | ||||
them as a 2-tuple. Only one pass is made over `itr`. | ||||
|
||||
!!! compat "Julia 1.2" | ||||
This method requires Julia 1.2 or later. | ||||
|
||||
# Examples | ||||
```jldoctest | ||||
julia> extrema(sin, 0:π) | ||||
(0.0, 0.9092974268256817) | ||||
``` | ||||
""" | ||||
extrema(f, itr) = _extrema_itr(f, itr) | ||||
|
||||
function _extrema_itr(f, itr) | ||||
y = iterate(itr) | ||||
y === nothing && throw(ArgumentError("collection must be non-empty")) | ||||
(v, s) = y | ||||
vmin = vmax = f(v) | ||||
while true | ||||
y = iterate(itr, s) | ||||
y === nothing && break | ||||
(x, s) = y | ||||
fx = f(x) | ||||
vmax = max(fx, vmax) | ||||
vmin = min(fx, vmin) | ||||
end | ||||
return (vmin, vmax) | ||||
end | ||||
|
||||
extrema(x::Real) = (x, x) | ||||
extrema(f, x::Real) = (y = f(x); (y, y)) | ||||
Comment on lines
-556
to
-557
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aren't these still useful? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Line 419 in 5142abf
So, I think the compiler will generate the equivalent machine code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool. Maybe worth checking just in case? |
||||
|
||||
## definitions providing basic traits of arithmetic operators ## | ||||
|
||||
""" | ||||
|
Uh oh!
There was an error while loading. Please reload this page.