help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : Re: Any faster way to find frequency of words?


From: Jean Louis
Subject: Re: [External] : Re: Any faster way to find frequency of words?
Date: Mon, 10 May 2021 19:26:18 +0300
User-agent: Mutt/2.0.6 (2021-03-06)

* Drew Adams <drew.adams@oracle.com> [2021-05-10 17:03]:
> I use this, FWIW:
> 
> (defun hash-table-to-alist (hash-table)
>   "Create and return an alist created from HASH-TABLE.
> The order of alist entries is undefined, but it seems to be the same
> as the order of hash-table entries (which seems to be the order in
> which the entries were added to the table)."
>   (let ((al  ()))
>     (maphash (lambda (key val) (push (cons key val) al))
>              hash-table)
>     (nreverse al)))

That may be better, nicer.

I wonder if nreverse is really needed as function just returns some
data, is that data anyway destroyed thereafter?

Then I was also using reverse, I will take it out, as I don't think
there is any order in the hash, if I reverse it or not, it does not
matter.

(setq hash (make-hash-table))
(puthash 'Name "Jimmy" hash)
(puthash "City" "New York" hash)
(puthash "Brigade" "II" hash)

hash ⇒ #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 
data (Name "Jimmy" "City" "New York" "Brigade" "II"))

(puthash 'Name "Jimmy2" hash)

hash ⇒ #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8125 
data (Name "Jimmy2" "City" "New York" "Brigade" "II"))

I can see that hash keeps order of entries, but I don't believe
that is guaranteed. Visually it gives us the same order:

(hash-table-to-alist hash) ⇒ ((Name . "Jimmy2") ("City" . "New York") 
("Brigade" . "II"))

(setq alist (hash-table-to-alist hash)) ⇒ ((Name . "Jimmy2") ("City" . "New 
York") ("Brigade" . "II"))

(assoc 'Name alist) ⇒ (Name . "Jimmy2")

I just wonder if the order matters. It should not matter in hash,
alist, plist I guess.




-- 
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 via email to

[Prev in Thread] Current Thread [Next in Thread]