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=0.1 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_BL_SPAMCOP_NET,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 Received: from nue.mailmanlists.eu (nue.mailmanlists.eu [IPv6:2a01:4f8:1c0c:6b10::1]) (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 872C71F44D for ; Thu, 14 Mar 2024 15:06:28 +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=CRvdXHgb; 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=SIURygii; dkim-atps=neutral Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id 62E6E833D8; Thu, 14 Mar 2024 15:06:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1710428782; bh=o0XENifnd2ul5TT9RWMr79NGdbTRF+lE6EQHMW6zbaA=; 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=CRvdXHgb9msgICKUIC8uIFboVvsayOGj4rwtNFUUwUrSYB72mqNeV1Or5uDkVX1cD QBmHY268uL3x/h/PNP7oo8AyPMmNUVr/X6wh92ppuN65uRmxDhDwiDqhB9nIzjQwIE kq7Ax5fe72Ij+nFKS5IqxzV6SxBMwtBWs8w8ySkQ= Received: from s.wrqvtzvf.outbound-mail.sendgrid.net (s.wrqvtzvf.outbound-mail.sendgrid.net [149.72.126.143]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 4EF3C833D3 for ; Thu, 14 Mar 2024 15:06:19 +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=SIURygii; 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=cWv7TuGbbGC2r1Y2toX4fvC+RZK1i5ygOAHzeHfoZEY=; b=SIURygiin54YNQGk9yLwGcGPmVSUtQph2sW00aZXVwYqUCLD+uObjARr6/cahrwkL587 L+Sbtnq6w4T4C3g5OGETdyBFENjqAo4KZcfqfezKCptFsKnHoMK0a/WtmBxRMk/bjz/LzU 7gSIKhgyP4d0BH6ObwxWiGMOlgCpymgXUjKSKcCj7WthxQwzTOSgRBbj/ZpnV6TReLk6eX nsoW+xr8Pn2USuFww6lEvLnnG1yFhljFttQ3EWQ1/bS4zLnNb6EtEq87ztSKcgiTEyJew0 /IGdv7kEvc48QKpkswT1u860+S10g+5SROSwAokwaiC4tUYr5cTXeecZDtG+4g6Q== Received: by recvd-6449d6bd6c-lcghs with SMTP id recvd-6449d6bd6c-lcghs-1-65F3126A-4 2024-03-14 15:06:18.208392225 +0000 UTC m=+1271746.270667232 Received: from herokuapp.com (unknown) by geopod-ismtpd-13 (SG) with ESMTP id oy8qfx6YTRe2GIqVU5BLug for ; Thu, 14 Mar 2024 15:06:18.180 +0000 (UTC) Date: Thu, 14 Mar 2024 15:06:18 +0000 (UTC) Message-ID: References: Mime-Version: 1.0 X-Redmine-Project: ruby-master X-Redmine-Issue-Tracker: Feature X-Redmine-Issue-Id: 20300 X-Redmine-Issue-Author: AMomchilov X-Redmine-Issue-Priority: Normal X-Redmine-Sender: Eregon 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: 93745 X-SG-EID: =?us-ascii?Q?u001=2EByjZWvxTCjdoV8K03xEuhE7KqN4thWULFLM7+oH78KY30oYB3qFthsDpL?= =?us-ascii?Q?4w4cbYa3ttBh8bAHPOnE=2FkzPba67JNu7Lnrked2?= =?us-ascii?Q?O7K9VQ=2FJax1dkAm1TSphwp4HAClIGkkkCuwewxl?= =?us-ascii?Q?BIWbpV27mKD0ULkNzilPeda9xE5+LVAJiTqqBYt?= =?us-ascii?Q?z+aOd8F9bpzrNZ89kPWDuuftdv18bnq4T8BohXs?= =?us-ascii?Q?AqVMZMvZ6=2FMGQjJGQkJ14iuDhlGHVLNvNXr71Hb?= =?us-ascii?Q?mqw2Aor1OXX2xiI0njFlaKpmyg=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: u001.I8uzylDtAfgbeCOeLBYDww== Message-ID-Hash: UL2FRPTDAHN5QX2WCIUBBZ3KPTNWYWLT X-Message-ID-Hash: UL2FRPTDAHN5QX2WCIUBBZ3KPTNWYWLT 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:117179] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call List-Id: Ruby developers Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: "Eregon (Benoit Daloze) via ruby-core" Cc: "Eregon (Benoit Daloze)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by Eregon (Benoit Daloze). nobu (Nobuyoshi Nakada) wrote in #note-17: > I want `ENV.exchange_value` rather than `Hash#exchange_value`. Agreed it's a generally useful thing. So I think we should add it for both Hash and ENV (ENV has all `Hash` methods anyway). ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107269 * Author: AMomchilov (Alexander Momchilov) * Status: Open ---------------------------------------- When using a Hash, sometimes you want to set a new value, **and** see what was already there. Today, you **have** to do this in two steps: ```ruby h = { k: "old value" } # 1. Do a look-up for `:k`. old_value = h[:k] # 2. Do another look-up for `:k`, even though we just did that! h[:k] = "new value" use(old_value) ``` This requires two separate `Hash` look-ups for `:k`. This is fine for symbols, but is expensive if computing `#hash` or `#eql?` is expensive for the key. It's impossible to work around this today from pure Ruby code. One example use case is `Set#add?`. See https://bugs.ruby-lang.org/issues/20301 for more details. I propose adding `Hash#exchange_value`, which has semantics are similar to this Ruby snippet: ```ruby class Hash # Exact method name TBD. def exchange_value(key, new_value) old_value = self[key] self[key] = new_value old_value end end ``` ... except it'll be implemented in C, with modifications to `tbl_update` that achieves this with a hash-lookup. I'm opening to alternative name suggestions. @nobu came up with `exchange_value`, which I think is great. Here's a PR with a PoC implementation: https://github.com/ruby/ruby/pull/10092 ```ruby h = { k: "old value" } # Does only a single hash look-up old_value = h.exchange_value(:k, "new value") use(old_value) ``` -- 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/