From: "AMomchilov (Alexander Momchilov) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "AMomchilov (Alexander Momchilov)" <noreply@ruby-lang.org>
Subject: [ruby-core:117193] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call
Date: Fri, 15 Mar 2024 01:49:49 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-107282.20240315014949.51722@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-20300.20240226024229.51722@ruby-lang.org
Issue #20300 has been updated by AMomchilov (Alexander Momchilov).
nobu (Nobuyoshi Nakada) wrote in #note-17:
> I want `ENV.exchange_value` rather than `Hash#exchange_value`.
Is there a reason to put it on `ENV`, but *not* on `Hash`? That seems like a needless restriction to me.
----------------------------------------
Feature #20300: Hash: set value and get pre-existing value in one call
https://bugs.ruby-lang.org/issues/20300#change-107282
* 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/
next prev parent reply other threads:[~2024-03-15 1:50 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-02-26 2:42 [ruby-core:116940] [Ruby master Feature#20300] Hash: set value and get pre-existing value in one call AMomchilov (Alexander Momchilov) via ruby-core
2024-02-26 4:18 ` [ruby-core:116943] " nobu (Nobuyoshi Nakada) via ruby-core
2024-02-26 10:16 ` [ruby-core:116947] " Eregon (Benoit Daloze) via ruby-core
2024-02-26 10:38 ` [ruby-core:116948] " rubyFeedback (robert heiler) via ruby-core
2024-02-26 16:21 ` [ruby-core:116958] " MaxLap (Maxime Lapointe) via ruby-core
2024-02-27 16:42 ` [ruby-core:116973] " matheusrich (Matheus Richard) via ruby-core
2024-02-27 16:46 ` [ruby-core:116975] " AMomchilov (Alexander Momchilov) via ruby-core
2024-02-27 17:34 ` [ruby-core:116978] " matheusrich (Matheus Richard) via ruby-core
2024-02-27 21:30 ` [ruby-core:116981] " AMomchilov (Alexander Momchilov) via ruby-core
2024-02-27 21:39 ` [ruby-core:116982] " matheusrich (Matheus Richard) via ruby-core
2024-03-14 5:22 ` [ruby-core:117136] " nobu (Nobuyoshi Nakada) via ruby-core
2024-03-14 10:12 ` [ruby-core:117156] " mame (Yusuke Endoh) via ruby-core
2024-03-14 15:05 ` [ruby-core:117178] " Eregon (Benoit Daloze) via ruby-core
2024-03-14 15:06 ` [ruby-core:117179] " Eregon (Benoit Daloze) via ruby-core
2024-03-15 1:49 ` AMomchilov (Alexander Momchilov) via ruby-core [this message]
2024-03-15 2:46 ` [ruby-core:117194] " shyouhei (Shyouhei Urabe) via ruby-core
2024-04-17 4:30 ` [ruby-core:117545] " nobu (Nobuyoshi Nakada) via ruby-core
2024-04-17 10:57 ` [ruby-core:117560] " mame (Yusuke Endoh) via ruby-core
2024-05-11 16:21 ` [ruby-core:117832] " AMomchilov (Alexander Momchilov) 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-107282.20240315014949.51722@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).