ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "alexbarret (Alexandre Barret) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "alexbarret (Alexandre Barret)" <noreply@ruby-lang.org>
Subject: [ruby-core:117243] [Ruby master Feature#8421] add Enumerable#find_map and Enumerable#find_all_map
Date: Tue, 19 Mar 2024 23:12:55 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-107330.20240319231255.3055@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-8421.20130518170348.3055@ruby-lang.org

Issue #8421 has been updated by alexbarret (Alexandre Barret).


zverok (Victor Shepelev) wrote in #note-4:
> @alexbarret There is a somewhat lesser-known trick which looks pretty close to your code:
> ```ruby
> # proposal:
> find_map(emails) do |email|
>   (matches = pattern.match(email)) && matches[:identifier]
> end
> # a "trick"
> emails.find { |email| match = pattern.match(email) and break match[:identifier] }
> # => "thecode"
> ```
> It might even be considered two tricks, depending on your point of view: the control-flow `and` allows to chain any statements to it (note it doesn't need extra parentheses after assignment), and `break value` allows to return a non-standard value from a block.
> 
> Not saying it is beautiful, just one more option.

Thanks I learned something, and it makes sense thinking about it. Both `and` and `break` aren't things I'm used to use in Ruby code but I'm glad I know this trick. Not sure I'll use it much. Breaking with another value than the expected value returned from the enumerable method can be confusing. 

----------------------------------------
Feature #8421: add Enumerable#find_map and Enumerable#find_all_map
https://bugs.ruby-lang.org/issues/8421#change-107330

* Author: Hanmac (Hans Mackowiak)
* Status: Feedback
----------------------------------------
currently if you have an Enumerable and you want to return the return value of #find you need eigther:
  (o = enum.find(block) && block.call(o)) || nil
or
  enum.inject(nil) {|ret,el| ret || block.call(el)}

neigher of them may be better than an directly maked method

same for #find_all_map
  enum.lazy.map(&:block).find_all{|el| el}

it may work but it is not so good



-- 
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:[~2024-03-19 23:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-8421.20130518170348.3055@ruby-lang.org>
2020-11-29 23:40 ` [ruby-core:101145] [Ruby master Feature#8421] add Enumerable#find_map and Enumerable#find_all_map fluff
2024-03-19 20:38 ` [ruby-core:117237] " alexbarret (Alexandre Barret) via ruby-core
2024-03-19 20:50 ` [ruby-core:117239] " zverok (Victor Shepelev) via ruby-core
2024-03-19 22:46 ` [ruby-core:117241] " jeremyevans0 (Jeremy Evans) via ruby-core
2024-03-19 23:08 ` [ruby-core:117242] " alexbarret (Alexandre Barret) via ruby-core
2024-03-19 23:12 ` alexbarret (Alexandre Barret) via ruby-core [this message]

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-107330.20240319231255.3055@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).