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) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id 75CA81F44D for ; Mon, 26 Feb 2024 16:21:44 +0000 (UTC) Received: from nue.mailmanlists.eu (localhost [127.0.0.1]) by nue.mailmanlists.eu (Postfix) with ESMTP id C96D682C87; Mon, 26 Feb 2024 16:21:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ml.ruby-lang.org; s=mail; t=1708964496; bh=8lJASvegCqX2mzroFZxvPUKeWpMcVPk9IeN5xNcMnzc=; 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=Rwd25ETJiNoUVMeaD300Ix9xfvzssISSqzx3rKdCBdUzFvFq30w69G6NtANdw9qP5 GC1blloVaK4Z/IZzxc23Vxd6m2t+/LKSLRIiCK7bPgSJL4o0V4vqKN1N/20Igeni7d S4t0aCHsnkq3fgwfNZHrIMikGzCjh8Kyk5Uib+Hs= Received: from s.csnrwnwx.outbound-mail.sendgrid.net (s.csnrwnwx.outbound-mail.sendgrid.net [198.37.146.154]) by nue.mailmanlists.eu (Postfix) with ESMTPS id 26CD682C0A for ; Mon, 26 Feb 2024 16:21:32 +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=S7qiLCwN; 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=vb8p896D9TyVNj/jkDIjG+KqDOs/iruDqGSc2C7wfeY=; b=S7qiLCwN+hFICrtx8wztfkYRdBoOOsz0UOl0tt9OsBx+4DwPoxdpVWy3KBoLKBxTil5S XwVJDFYbWN9sGyR4JwpD1QA72E+4Y9LDEzi0OL35+8mUzvvLt0+SiDy8l/3v1wRmpZ0/oi L/KV+c/6mAS4wkrZl+/G+l1Q2lB3wTt0JfD8tyiF/6oOQjEpkNgsr0skT9rfNhogCaoLoy 6m7Qd8VelY3LHFGnIQGFR7GbZ4CsGFx2YnKVf3/MQrxdwh9uZQ6rmcOR9ftsNIM+qXbGpT K0I7OcsiyyYf8MV2FeGGruJPXZ2b0Yf70uK0Zj9nes7S16S8ObrtKZnZ4zIjIUmA== Received: by filterdrecv-655bd866f5-tzxlm with SMTP id filterdrecv-655bd866f5-tzxlm-1-65DCBA8A-45 2024-02-26 16:21:30.881690635 +0000 UTC m=+1541099.279831555 Received: from herokuapp.com (unknown) by geopod-ismtpd-32 (SG) with ESMTP id 8j8d-QUpTCmbmJncBbq1hA for ; Mon, 26 Feb 2024 16:21:30.871 +0000 (UTC) Date: Mon, 26 Feb 2024 16:21:30 +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: MaxLap 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: 93514 X-SG-EID: =?us-ascii?Q?GyFSwSSybvcCq8fHR1H2JrhnsVG9iXzGuy0x7JqYf=2FlIXtED6wSggf7HFMZTl5?= =?us-ascii?Q?cUXooF=2FmFWWjj5oUTbC681KuDSxB9b1lvjuaz2i?= =?us-ascii?Q?KIvR=2F9cd4r2Pqw9h0VAE2hHyDql8XoUQy4jdJUI?= =?us-ascii?Q?EmMRolLXLV+L7OybMBB+4Hgvnv33tAX124qcEwE?= =?us-ascii?Q?wXFV907NCvhU598kAMJZjZFUQJj36o1cgcAihtl?= =?us-ascii?Q?aQsgaXNethiRO3HsS4E2hFkS4aJOfeuwuIsK26X?= =?us-ascii?Q?xtHErR8Owj2UkScGzD1eg=3D=3D?= To: ruby-core@ml.ruby-lang.org X-Entity-ID: b/2+PoftWZ6GuOu3b0IycA== Message-ID-Hash: 4X57SG73CIRHFFDPCPSO2IYJUBQFX3WW X-Message-ID-Hash: 4X57SG73CIRHFFDPCPSO2IYJUBQFX3WW 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:116958] [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: "MaxLap (Maxime Lapointe) via ruby-core" Cc: "MaxLap (Maxime Lapointe)" Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Issue #20300 has been updated by MaxLap (Maxime Lapointe). In my mind, `get_and_set` is clear as it is in the order the operations are done. But if you want clearer, `get_then_set` puts emphasis on the order. Things like `exchange` and `swap` can sound like swapping the value between two keys. Honestly, I'm not sure I ever needed this. If `fetch_set` was refused, which I frequently need, this likely won't pass either. ---------------------------------------- Feature #20300: Hash: set value and get pre-existing value in one call https://bugs.ruby-lang.org/issues/20300#change-107001 * 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#update_value`, which has semantics are similar to this Ruby snippet: ```ruby class Hash # Exact method name TBD. def update_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.update_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/