Feature #17097
open`map_min`, `map_max`
Description
min
, min_by
, max
, max_by
return the element that leads to the minimum or the maximum value, but I think it is as, or even more, frequent that we are interested in the minimum or the maximum value itself rather than the element. For example, to get the length of the longest string in an array, we do:
%w[aa b cccc dd].max_by(&:length).length # => 4
%w[aa b cccc dd].map(&:length).max # => 4
I propose to have methods that return the minimum or the maximum value. Temporarily calling them map_min
, map_max
, they should work like this:
%w[aa b cccc dd].map_max(&:length) # => 4
map_min
, map_max
are implementation-centered names, so perhaps better names should replace them, just like yield_self
was replaced by then
.
Updated by nobu (Nobuyoshi Nakada) over 4 years ago
Then we'll need map_
versions for all Enumerable
methods.
Updated by greggzst (Grzegorz Jakubiak) over 4 years ago
nobu (Nobuyoshi Nakada) wrote in #note-1:
Then we'll need
map_
versions for allEnumerable
methods.
Exactly, I don't see any good use case in that apart from being lazy and just using one method call. It seems to me that this kind of proposal is too much. I mean there are more pressing issues or features of the language that the team has to focus on instead of dealing with proposals like map_tally
or whatever.
Updated by Eregon (Benoit Daloze) over 4 years ago
I actually regularly wanted such functionality, but min_by/max_by do not return the result of the block but the element (which makes sense).
And .map {}.min/max
works but is inefficient by generating an intermediate Array.
Maybe we should add some keyword argument to min_by/max_by?
Updated by Eregon (Benoit Daloze) over 4 years ago
To put in context, consider that the expression might be much longer than .length
.
Then repeating it is not elegant and is duplicated code.
Updated by marcandre (Marc-Andre Lafortune) over 4 years ago
@nobu (Nobuyoshi Nakada) is right, we're not going to add map_
for everything.
Eregon (Benoit Daloze) wrote in #note-6:
Then repeating it is not elegant and is duplicated code.
I don't see why there would be repetition.
Just do enum.map { very_long_expression }.max
...
Please benchmark the time it takes to generate the intermediate array. My guess is that it's typically negligible.
Updated by sawa (Tsuyoshi Sawada) over 4 years ago
I do not understand why the proposal has to be extended to all other Enumerable
methods.
My point is semantic. I do not see that there are significantly more use cases where I am interested in the element that is related to the min/max value but am not interested in the min/max value than the use cases of the converse.
Updated by universato (Yoshimine Sato) almost 4 years ago
https://github.com/crystal-lang/crystal/pull/365
In Crystal language, these proposed methods are called max_of
, min_of
.
Note: Crystal has max
, max_by
, max_of
.
This feature may not be innovative, but I think many Ruby users have the opportunity to use these methods.
Updated by jnchito (Junichi Ito) almost 2 years ago
max_of
method is very elegant! I really want the Ruby version of this method.
For example, I need to write column_width = @ls_files.map { |ls_file| ls_file.name.size }.max
to determine column width according to the longest file name (in the image below, it would be 19 because "credentials.yml.enc" is the longest, and see also this)
I'd be pretty happy if I could write column_width = @ls_files.max_of { |ls_file| ls_file.name.size }
.
Updated by baweaver (Brandon Weaver) almost 2 years ago
For the sake of naming conventions I would personally lean towards map_max
to match methods like filter_map
, and while this does not necessarily extend to all Enumerable methods I would wonder if there's another conversation later to be had about composite Enumerable methods.
On this particular ticket though I believe that the use-case is common enough, and in many cases may be what the developer intends.
EDIT - Hrm, group_by
does complicate that a bit in conjunction with max_by
and other methods, so the naming may be a bit interesting. Given that there may be a case for of
, but that'd also intro a third pattern.
Updated by Eregon (Benoit Daloze) almost 2 years ago
IMHO we should just add max_of
and min_of
, like Crystal has.
map
in the name feels out of place here because this method does not return a new array, it's just execute the block and remember the max/min value returned by the block for all elements.