examples for minmax_by and minmax_by_key#1064
examples for minmax_by and minmax_by_key#1064phimuemue merged 3 commits intorust-itertools:masterfrom
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1064 +/- ##
==========================================
- Coverage 94.38% 93.67% -0.72%
==========================================
Files 48 50 +2
Lines 6665 6327 -338
==========================================
- Hits 6291 5927 -364
- Misses 374 400 +26 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Thanks for spotting the missing examples, but could you please replace them by something that uses (1, 'a'), (1, 'b'), (1, 'c'), (0, 'a'), (0, 'b'), (0, 'c'), and replace abs_key=... by |(number, character)| number (or equivalent). Then, the "for minimum, first minimal element wins and for maximum, last maximal element wins" is clearer. (Or, remove the 0 so that -1 or 1 is contained in the resulting MinMax, whichever is clearer to you.)
…ke it clearer which minimum elements and maximum elements are chosen
|
I removed the absolute value comparator and changed the items to (i32, char) so it was clearer which elements are chosen by minmax. I also changed the minmax example cause it's not clear which 1 wins in the last example of a slice of 1s. |
| /// let a: [i32; 0] = []; | ||
| /// let a: [(i32, char); 0] = []; | ||
| /// assert_eq!(a.iter().minmax(), NoElements); | ||
| /// | ||
| /// let a = [1]; | ||
| /// assert_eq!(a.iter().minmax(), OneElement(&1)); | ||
| /// let a = [(1, 'a')]; | ||
| /// assert_eq!(a.iter().minmax(), OneElement(&(1, 'a'))); | ||
| /// | ||
| /// let a = [1, 2, 3, 4, 5]; | ||
| /// assert_eq!(a.iter().minmax(), MinMax(&1, &5)); | ||
| /// let a = [(0, 'a'), (1, 'b')]; | ||
| /// assert_eq!(a.iter().minmax(), MinMax(&(0, 'a'), &(1, 'b'))); | ||
| /// | ||
| /// let a = [1, 1, 1, 1]; | ||
| /// assert_eq!(a.iter().minmax(), MinMax(&1, &1)); | ||
| /// let a = [(1, 'a'), (1, 'b'), (1, 'c')]; | ||
| /// assert_eq!(a.iter().minmax(), MinMax(&(1, 'a'), &(1, 'c'))); |
There was a problem hiding this comment.
Please leave them simple numbers, and add one representative example with tuples as an extra case. (Tuple comparisons are harder to understand, so we should keep the simple cases.)
| /// use itertools::Itertools; | ||
| /// use itertools::MinMaxResult::{MinMax, NoElements, OneElement}; | ||
| /// | ||
| /// let abs_cmp = |x: &&(i32, char), y: &&(i32, char)| x.0.cmp(&y.0); |
There was a problem hiding this comment.
abs_cmp does not seem a reasonable name for the closure.
phimuemue
left a comment
There was a problem hiding this comment.
Thanks for the follow-up, but I still have some nits - sorry for that, but if we already touch it, I don't want to stop half-way.
|
Fixed the closure name and restored the old example for minmax. |
6bd5053
Examples for minmax_by and minmax_by_key.