all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Jean Louis <bugs@gnu.support>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: help-gnu-emacs@gnu.org
Subject: Re: Is there any default way to append hashes?
Date: Tue, 4 May 2021 00:30:33 +0300	[thread overview]
Message-ID: <YJBreTAkrrZkH2ja@protected.localdomain> (raw)
In-Reply-To: <jwveeenu73m.fsf-monnier+emacs@gnu.org>

* 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/




      reply	other threads:[~2021-05-03 21:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-03 15:23 Is there any default way to append hashes? Jean Louis
2021-05-03 16:17 ` Stefan Monnier
2021-05-03 21:30   ` Jean Louis [this message]

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-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YJBreTAkrrZkH2ja@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=help-gnu-emacs@gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.