[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Is there any default way to append hashes?
From: |
Jean Louis |
Subject: |
Re: Is there any default way to append hashes? |
Date: |
Tue, 4 May 2021 00:30:33 +0300 |
User-agent: |
Mutt/2.0.6 (2021-03-06) |
* Stefan Monnier <monnier@iro.umontreal.ca> [2021-05-03 19:18]:
> > I am using this function to append hashes, is there maybe some better
> > or default method?
>
> Maybe you're looking for `map-merge`?
Thank you.
I have tried best to find something like that but could not. In the
mean time I made the function below, and it does append or join keys
and values from other hashes to the first one.
Yes, I needed function to append or join other hashes to the first
one which is being used. Now I know `map-merge' I can use it.
(defun hash-append (h1 &rest hashes)
"Return H1 hash appende with HASHES."
(mapc
(lambda (hash)
(maphash
(lambda (key value) (puthash key value h1)) hash))
hashes)
h1)
(setq h1 (make-hash-table :test 'equal))
(setq h2 (make-hash-table :test 'equal))
(puthash 'name "Jimmy" h1)
(puthash "last" "Lorence" h1)
(puthash "age" 12 h2)
(puthash "city" "New York" h2)
(hash-append h1 h2) ⇒ #s(hash-table size 65 test equal rehash-size 1.5
rehash-threshold 0.8125 data (name "Jimmy" "last" "Lorence" "age" 12 "city"
"New York"))
h1 ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125
data (name "Jimmy" "last" "Lorence" "age" 12 "city" "New York"))
h2 ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125
data ("age" 12 "city" "New York"))
The difference is that map-merge creates new hash-table without
modification.
(map-merge 'hash-table h1 h2) ⇒ #s(hash-table size 4 test equal rehash-size 1.5
rehash-threshold 0.8125 data (name "Jimmy" "last" "Lorence" "age" 12 "city"
"New York"))
h1 ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125
data (name "Jimmy" "last" "Lorence"))
h2 ⇒ #s(hash-table size 65 test equal rehash-size 1.5 rehash-threshold 0.8125
data ("age" 12 "city" "New York"))
--
Jean
Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns
Sign an open letter in support of Richard M. Stallman
https://stallmansupport.org/
https://rms-support-letter.github.io/