From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Philipp Stephani
> > (when (or use-last-p=C2=A0 (not (gethash key h= t)))
>
> This doesn't work if the value is nil.
I guess you mean that it doesn't DTRT if the cdr of an alist
entry is `nil' - e.g. (foo) aka (foo . nil).
> You need to use an uninterned symbol or some other unique object, e.g.=
> (eq (gethash key ht #1=3D'#:void) #1#)
OK.=C2=A0 Dunno which is clearer or whether there is some
performance difference, but I would probably just bind
a var to a unique cons, e.g. (cons 1 1), outside the
`dolist'.=C2=A0 E.g.:
(let ((ht (make-hash-table :test test :weakness weakness
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0:size size :rehash-size rehash-size
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0:rehash-threshold rehash-threshold))
=C2=A0 =C2=A0 =C2=A0 (uniq-cons (cons 1 1))
=C2=A0 =C2=A0 =C2=A0 key val)
=C2=A0 (dolist (key.val=C2=A0 alist)
=C2=A0 =C2=A0 (setq key=C2=A0 (car key.val)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 val=C2=A0 (cdr key.val))
=C2=A0 =C2=A0 (when (or use-last-p=C2=A0 (not (eq (gethash key ht uniq-cons= ))))
=C2=A0 =C2=A0 =C2=A0 (puthash key val ht)))
=C2=A0 ht))
(With your approach of using an uninterned symbol, wouldn't
you want to do the same thing: bind a var to it outside the
`dolist', or would that make no real difference?)
=C2=A0
> I'd personally make use-last-p another keyword argument, though.
I don't have a strong objection, but why?
> > (defun hash-table-to-alist (hash-table)
> >=C2=A0 "Create and return an alist created from HASH-TABLE. > > The order of alist entries is the same as the order of hash-table=
> > entries (which normally is the order in which the entries were ad= ded
> > to the table)."
> >=C2=A0 (let ((al=C2=A0 ()))
> >=C2=A0 =C2=A0 (maphash (lambda (key val) (push (cons key val) al))= hash-table)
> >=C2=A0 =C2=A0 (nreverse al)))
>
> Hmm, is the order guaranteed? I haven't found anything in the
> Emacs Lisp manual about this, so maybe just leave out the
> parenthetical remark or say that the order is unspecified?
Good question!=C2=A0 But it's not just the parenthetical part.
If we couldn't say anything about the traversal order of
`maphash' then the whole sentence would need to go.
FWIW, I think it's pretty important.=C2=A0 Order is quite
important for alists, generally.
Is there some reason we should not be able to count on
this `maphash' behavior?
That's the behavior I saw, AFAICT, but I didn't test much.
I don't know whether it is guaranteed, but this use case
involving conversion to alist looks like a good argument for
some guarantee wrt order.
I see that in Common Lisp nothing is said about the traversal
by `maphash' over the hash table.=C2=A0 This is all that is said:
=C2=A0"For each entry in hash-table, maphash calls function on two
=C2=A0 arguments: the key of the entry and the value of the entry;
=C2=A0 maphash then returns nil. If entries are added to or deleted
=C2=A0 from the hash table while a maphash is in progress, the results
=C2=A0 are unpredictable, with one exception: if the function calls
=C2=A0 remhash to remove the entry currently being processed by the
=C2=A0 function, or performs a setf of gethash on that entry to change
=C2=A0 the associated value, then those operations will have the
=C2=A0 intended effect."
=C2=A0 - https://www.cs.cmu.edu/Groups/AI= /html/cltl/clm/node155.html
(Emacs doc should also probably say at least something about
what happens when entries are added/removed during a `maphash'
application.)
If Emacs decides not to guarantee the order seen now, which I
think is the order I described in the doc string above, then we
would need to remove that sentence.=C2=A0 That would be too bad for
users of function `hash-table-to-alist', but at least they
would, at least so far (and AFAICT), get that behavior, which
is likely to be expected by them even if it is not documented.
Another possibility (?): Allow _optional_ creation of a hash
table that does preserve such ordering (even if just by
recording order of entry and making that available to `maphash').
E.g., add a keyword arg for `make-hash-table' that does just that.
Even if it turned out that this consistently or occasionally
detracted from performance, it could be a useful option.
Conversion to an alist would be a case in point.