-
-
Notifications
You must be signed in to change notification settings - Fork 746
std.algorithm: {min,max}Element for a single range #4221
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
Conversation
16acaf1 to
f56c1b7
Compare
std/algorithm/searching.d
Outdated
|
|
||
| static if (isRandomAccessRange!Range && hasLength!Range) | ||
| { | ||
| size_t lenArray = r.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
immutable
f56c1b7 to
57da28e
Compare
std/algorithm/searching.d
Outdated
| import std.range: enumerate, iota; | ||
| assert([2, 1, 4, 3].maxElement == 4); | ||
| // allows to get the index too | ||
| assert([2, 1, 4, 3].enumerate.maxElement!"a.value" == tuple(2, 4)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs another example of a custom accessor, something like the assert on line 1279
|
This also needs tests using the dummy ranges, as you have no tests currently with input ranges. |
std/algorithm/searching.d
Outdated
| $(D maxCount([2, 4, 1, 4, 1])) returns $(D tuple(4, 2)).) | ||
| $(T2 minElement, | ||
| Selects the minimal element of a range. | ||
| $(D minElement([3, 4, 1, 2])) returns $(D 1).) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use the new `` style for D code where ever possible. Same throughout.
|
Also, I forgot to mention that this should also have unit tests with |
57da28e to
c6410f6
Compare
Thanks a lot for your review @JackStouffer! |
c6410f6 to
b8e6dc1
Compare
std/algorithm/searching.d
Outdated
| alias selectorFun = binaryFun!selector; | ||
|
|
||
| // init extremum | ||
| Unqual!(ElementType!Range) extremeElement = r.front; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick - how about:
alias Element = ElementType!Range;
alias MapType = Unqual!(typeof(mapFun(Element.init)));
Unqual!Element extremeElement = r.front;
MapType extremeElementMapped = mapFun(extremeElement);
(Evaluate r[0] once and make the code a bit more DRY).
|
LGTM besides minor comments. |
b8e6dc1 to
c2ffcfa
Compare
Thanks a lot for reviewing this @ntrel - I just addressed your comments. |
|
Is having an Further, I would like an optional second parameter, Also, it could be motivated to add links in the docs to
I've seen STL-references to algorithms elsewhere in the Phobos docs. BTW: Thanks for adding these! |
AFAIK asserting with ranges is the style. |
We need to fix |
|
@9il What do you mean by: fixing |
It uses |
c2ffcfa to
17e5575
Compare
|
Wait a minute please |
assert([2, 1, 4, 3].enumerate.reduce!((a, b) => a.value > b.value ? a : b) == tuple(2, 4));What wrong with this code? |
|
If we are so lazy to write lambdas then just allow min/max to accept map function instead ;-) |
It evaluates the map function twice ;-)
Yes I would love to see
|
|
Another option is add combined |
So we need combined |
|
But please do not rush to write it right now. We need to think how this can be generalized for multidimensional |
|
@9il is this good to go? |
|
@DmitryOlshansky @9il what is your opinion on renaming this to |
|
OK, we can move forward. Looks like |
|
@wilzbach |
|
Same here - minElement is established name, and single ard min is problematic in generic code |
|
Auto-merge toggled on |
|
Hmm... how can I use this new functions with http://dlang.org/phobos/std_math.html#.cmp ? |
For reference: see #4257 where I propose to make extremum public ;-) |
|
Thanks! |
|
Why are minElement/maxElement not in the documentation at https://dlang.org/phobos/std_algorithm_searching.html? |
They are part of the 2.072 release (it's quite big due to the long accumulation period - April to September). Hopefully it can be released soon, but with all the regressions popping up it doesn't look good :/ |
|
Thanks for the info. |
resubmisson of #4019 - as
extremumturned out not to be very useful, I changed it to accept directly the mapping function. This is now more efficient as the mapping function is called only once :)@andralex has already approved the name:
Pinging @JackStouffer @burner @ntrel @klickverbot @JackStouffer