Project

General

Profile

Actions

Feature #17097

open

`map_min`, `map_max`

Added by sawa (Tsuyoshi Sawada) over 3 years ago. Updated over 1 year ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:99418]

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 3 years ago

Then we'll need map_ versions for all Enumerable methods.

Actions #2

Updated by sawa (Tsuyoshi Sawada) over 3 years ago

  • Description updated (diff)
Actions #3

Updated by sawa (Tsuyoshi Sawada) over 3 years ago

  • Description updated (diff)

Updated by greggzst (Grzegorz Jakubiak) over 3 years ago

nobu (Nobuyoshi Nakada) wrote in #note-1:

Then we'll need map_ versions for all Enumerable 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 3 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 3 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 3 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 3 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) about 3 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) over 1 year 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)

sample

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) over 1 year 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) over 1 year 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.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like1Like1Like0Like0