https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-07-31T12:37:53ZRuby Issue Tracking SystemRuby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868702020-07-31T12:37:53Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Then we'll need <code>map_</code> versions for all <code>Enumerable</code> methods.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868712020-07-31T12:41:21Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86871/diff?detail_id=57663">diff</a>)</li></ul> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868722020-07-31T12:43:28Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/86872/diff?detail_id=57664">diff</a>)</li></ul> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868732020-07-31T13:51:52Zgreggzst (Grzegorz Jakubiak)
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote in <a href="#note-1">#note-1</a>:</p>
<blockquote>
<p>Then we'll need <code>map_</code> versions for all <code>Enumerable</code> methods.</p>
</blockquote>
<p>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 <code>map_tally</code> or whatever.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868742020-07-31T18:03:11ZEregon (Benoit Daloze)
<ul></ul><p>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).<br>
And <code>.map {}.min/max</code> works but is inefficient by generating an intermediate Array.</p>
<p>Maybe we should add some keyword argument to min_by/max_by?</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868752020-07-31T18:06:21ZEregon (Benoit Daloze)
<ul></ul><p>To put in context, consider that the expression might be much longer than <code>.length</code>.<br>
Then repeating it is not elegant and is duplicated code.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868782020-08-01T00:56:31Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p><a class="user active user-mention" href="https://redmine.ruby-lang.org/users/4">@nobu (Nobuyoshi Nakada)</a> is right, we're not going to add <code>map_</code> for everything.</p>
<p>Eregon (Benoit Daloze) wrote in <a href="#note-6">#note-6</a>:</p>
<blockquote>
<p>Then repeating it is not elegant and is duplicated code.</p>
</blockquote>
<p>I don't see why there would be repetition.</p>
<p>Just do <code>enum.map { very_long_expression }.max</code>...</p>
<p>Please benchmark the time it takes to generate the intermediate array. My guess is that it's typically negligible.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=868802020-08-01T03:53:18Zsawa (Tsuyoshi Sawada)
<ul></ul><p>I do not understand why the proposal has to be extended to all other <code>Enumerable</code> methods.</p>
<p>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.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=898932021-01-13T01:24:25Zuniversato (Yoshimine Sato)
<ul></ul><p><a href="https://github.com/crystal-lang/crystal/pull/365" class="external">https://github.com/crystal-lang/crystal/pull/365</a><br>
In Crystal language, these proposed methods are called <code>max_of</code>, <code>min_of</code>.<br>
Note: Crystal has <code>max</code>, <code>max_by</code>, <code>max_of</code>.</p>
<p>This feature may not be innovative, but I think many Ruby users have the opportunity to use these methods.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=1007622022-12-23T02:14:30Zjnchito (Junichi Ito)
<ul></ul><p><code>max_of</code> method is very elegant! I really want the Ruby version of this method.</p>
<p>For example, I need to write <code>column_width = @ls_files.map { |ls_file| ls_file.name.size }.max</code> 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 <a href="https://github.com/JunichiIto/ruby-practices/pull/2/files#diff-50ea26a97f1139dc7a41d65762e37a2ba50a801e32fb6af28b4a6f6263ed3b97R11" class="external">this</a>)</p>
<p><img src="https://user-images.githubusercontent.com/1148320/209256935-e849003a-1ac1-4244-a4c8-d21cb53aa5c5.png" alt="sample"></p>
<p>I'd be pretty happy if I could write <code>column_width = @ls_files.max_of { |ls_file| ls_file.name.size }</code> .</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=1007652022-12-23T04:30:58Zbaweaver (Brandon Weaver)keystonelemur@gmail.com
<ul></ul><p>For the sake of naming conventions I would personally lean towards <code>map_max</code> to match methods like <code>filter_map</code>, and while this does not necessarily extend to <em>all</em> Enumerable methods I would wonder if there's another conversation later to be had about composite Enumerable methods.</p>
<p>On this particular ticket though I believe that the use-case is common enough, and in many cases may be what the developer intends.</p>
<p>EDIT - Hrm, <code>group_by</code> does complicate that a bit in conjunction with <code>max_by</code> and other methods, so the naming may be a bit interesting. Given that there may be a case for <code>of</code>, but that'd also intro a third pattern.</p> Ruby master - Feature #17097: `map_min`, `map_max`https://redmine.ruby-lang.org/issues/17097?journal_id=1007782022-12-23T17:19:02ZEregon (Benoit Daloze)
<ul></ul><p>IMHO we should just add <code>max_of</code> and <code>min_of</code>, like Crystal has.<br>
<code>map</code> 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.</p>