ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "jnchito (Junichi Ito) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "jnchito (Junichi Ito)" <noreply@ruby-lang.org>
Subject: [ruby-core:111387] [Ruby master Feature#17097] `map_min`, `map_max`
Date: Fri, 23 Dec 2022 02:14:31 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-100762.20221223021430.2963@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17097.20200731123339.2963@ruby-lang.org

Issue #17097 has been updated by jnchito (Junichi Ito).


`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](https://github.com/JunichiIto/ruby-practices/pull/2/files#diff-50ea26a97f1139dc7a41d65762e37a2ba50a801e32fb6af28b4a6f6263ed3b97R11))

![sample](https://user-images.githubusercontent.com/1148320/209256935-e849003a-1ac1-4244-a4c8-d21cb53aa5c5.png)

I'd be pretty if I could write `column_width = @ls_files.max_of { |ls_file| ls_file.name.size }` .

----------------------------------------
Feature #17097: `map_min`, `map_max`
https://bugs.ruby-lang.org/issues/17097#change-100762

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
----------------------------------------
`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:

```ruby
%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:

```ruby
%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`.



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

  parent reply	other threads:[~2022-12-23  2:14 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-07-31 12:33 [ruby-core:99418] [Ruby master Feature#17097] `map_min`, `map_max` sawadatsuyoshi
2020-07-31 12:37 ` [ruby-core:99419] " nobu
2020-07-31 13:51 ` [ruby-core:99420] " grzegorz.jakubiak
2020-07-31 18:03 ` [ruby-core:99421] " eregontp
2020-07-31 18:06 ` [ruby-core:99422] " eregontp
2020-08-01  0:56 ` [ruby-core:99425] " marcandre-ruby-core
2020-08-01  3:53 ` [ruby-core:99428] " sawadatsuyoshi
2021-01-13  1:24 ` [ruby-core:102041] " universato
2022-12-23  2:14 ` jnchito (Junichi Ito) via ruby-core [this message]
2022-12-23  4:30 ` [ruby-core:111390] " baweaver (Brandon Weaver) via ruby-core
2022-12-23 17:19 ` [ruby-core:111406] " Eregon (Benoit Daloze) via ruby-core

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-100762.20221223021430.2963@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    --cc=noreply@ruby-lang.org \
    --cc=ruby-core@ml.ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).