From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on starla X-Spam-Level: X-Spam-Status: No, score=-1.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [94.130.110.93]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id B73811F44D for ; Tue, 19 Mar 2024 23:13:08 +0000 (UTC) Authentication-Results: dcvr.yhbt.net; dkim=pass (1024-bit key; secure) header.d=ml.ruby-lang.org header.i=@ml.ruby-lang.org header.a=rsa-sha256 header.s=mail header.b=c29Tajt+; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=ZoaW13p3; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 5A55F83610; Tue, 19 Mar 2024 23:13:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1710889980; bh=A/XLg5RRDXDHnrRWbozruPQbfhlBYks1JZwqlDbmfSM=; h=Date:References:To:Reply-To:Subject:List-Id:List-Archive: List-Help:List-Owner:List-Post:List-Subscribe:List-Unsubscribe: From:Cc:From; b=c29Tajt+IPLgGy2nenQDgk8lm+WLaU1bRlZWTMQVwI08BD2PZ0deM1XWwAysi0jel nTh8D7OoamIbHTfcUcm4uD6ST9Bc75MOXbeushAWAlpnjEdcZk2sqZu6TencSpLP3X 4vMucZ9qg/TueQDDJWWmBhG5pXTu0PHTYDjGE8yk= Received: from s.wfbtzhsw.outbound-mail.sendgrid.net (s.wfbtzhsw.outbound-mail.sendgrid.net [159.183.224.105]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 2E4848360E for ; Tue, 19 Mar 2024 23:12:57 +0000 (UTC) Authentication-Results: nue.mailmanlists.eu; dkim=pass (2048-bit key; unprotected) header.d=ruby-lang.org header.i=@ruby-lang.org header.a=rsa-sha256 header.s=s1 header.b=ZoaW13p3; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ruby-lang.org; h=from:references:subject:mime-version:content-type: content-transfer-encoding:list-id:to:cc:content-type:from:subject:to; s=s1; bh=OuquCcKYTgaof0TsMD+eI9yj/Hbg0zYyXEfNzbVxolY=; b=ZoaW13p3t5CeUtuJspLlXPsWYkYeJhDqgogQc9y9H9sqrh7/wuwZVQspupiTSE/N89p0 K19lhblLX6x3eKC3Uc85J7igOnpPaSbqOBglqqxxx+lMhuzszEJEcZB4769ffWFKWIl9bB rv4qfzLwLFt2gfr7kNkU0NsxrUb4XwPO8WAWIfX4CQKoEavNiqZ1kPT/U9mLwym1+ZDZ3g IwtF84rswmOxzs/dyUr6KlB8xogQNvZYmpYLDs4k3uPkoDFLoImklV4t6k6BwCe1EiMmUx KUbUIA6BBXGaNMSr5R57OA9ObKDtI9ioSmvjplaAhIxxAczfAtIdUrlSjcGfAA7A== Received: by filterdrecv-55f965f85f-cjngn with SMTP id filterdrecv-55f965f85f-cjngn-1-65FA1BF7-8 2024-03-19 23:12:55.592951526 +0000 UTC m=+96418.906517373 Received: from herokuapp.com (unknown) by geopod-ismtpd-27 (SG) with ESMTP id KmCKZEuOQAOfvEKXE8JeqA for ; Tue, 19 Mar 2024 23:12:55.424 +0000 (UTC) Date: Tue, 19 Mar 2024 23:12:55 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 8421 X-Redmine-Issue-Author: Hanmac X-Redmine-Issue-Priority: Normal X-Redmine-Sender: alexbarret X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-Redmine-MailingListIntegration-Message-Ids: 93809 X-SG-EID: =?us-ascii?Q?u001=2EPM56nzipI1Vd7ui7CP31DJqqvsOIjA73JfRmigQQ9dPaWHeyXCWGhoVui?= =?us-ascii?Q?kkHu841NlYGcH8HwcKlD0CLzQYIYvyXTrIL+MSM?= =?us-ascii?Q?uZRa0HUaqzz7Fy0k5DRGMtFDforDQkaFCOKkq1g?= =?us-ascii?Q?1rqkpR8Oluu397JnjkonDinHGngSh68gUI=2FS+nM?= =?us-ascii?Q?ogEgVu=2FR7G+Vb2uBgx4EwElBFRbgCkjDav986pH?= =?us-ascii?Q?9yT9qdPovSouQs7VhIWxflsNofQY83oqv7Da1k=2F?= =?us-ascii?Q?KX=2FSyHokoYsl7C=2Ft8F1OpsZBfw=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: YFGFRNS2Q2SB2AMGKHG3V2M7DN4CV55S X-Message-ID-Hash: YFGFRNS2Q2SB2AMGKHG3V2M7DN4CV55S X-MailFrom: bounces+313651-b711-ruby-core=ml.ruby-lang.org@em5188.ruby-lang.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list Reply-To: Ruby developers Subject: [ruby-core:117243] [Ruby master Feature#8421] add Enumerable#find_map and Enumerable#find_all_map List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "alexbarret (Alexandre Barret) via ruby-core" Cc: "alexbarret (Alexandre Barret)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit 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/