all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Is there any default way to append hashes?
@ 2021-05-03 15:23 Jean Louis
  2021-05-03 16:17 ` Stefan Monnier
  0 siblings, 1 reply; 3+ messages in thread
From: Jean Louis @ 2021-05-03 15:23 UTC (permalink / raw)
  To: Help GNU Emacs

I am using this function to append hashes, is there maybe some better
or default method?

(defun hash-append (h1 h2)
  "Append hash H2 to H1 and return H1."
  (maphash (lambda (key value) (puthash key value h1)) h2)
  h1)



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/




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is there any default way to append hashes?
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Stefan Monnier @ 2021-05-03 16:17 UTC (permalink / raw)
  To: help-gnu-emacs

> I am using this function to append hashes, is there maybe some better
> or default method?

Maybe you're looking for `map-merge`?


        Stefan


PS: I actually had to look at your code to understand what you meant,
    because to me this is not "appending" and it's not operating on
    "hash"es (but on "hash tables").




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Is there any default way to append hashes?
  2021-05-03 16:17 ` Stefan Monnier
@ 2021-05-03 21:30   ` Jean Louis
  0 siblings, 0 replies; 3+ messages in thread
From: Jean Louis @ 2021-05-03 21:30 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

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




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-03 21:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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

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.