all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: maphash: improve docstring
@ 2016-03-29 18:58 Paul Pogonyshev
  2016-03-29 22:44 ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Pogonyshev @ 2016-03-29 18:58 UTC (permalink / raw)
  To: emacs-devel, Stefan Monnier

[I didn't receive the answer because I wasn't subscribed to the list,
 so the thread will be broken]

> > Docstring for `maphash' doesn't give any answer to the following
> > question: is the callback FUNCTION allowed to access the hash
> > table in read mode (I guess yes) and write mode?  About the
> > latter I have no idea without studying internal implementation.
>
> > E.g., is the following code going to work as a way to filter a
> > hash table?
>
> Yes, you are allowed to modify the table while iterating over it and the
> resulting behavior should be sane.
>
> And it should have the usual desired properties: The set of elements
> passed to the function is a superset of all the elements that aren't
> removed during the iteration, and a subset of all the elements that were
> present at the beginning or added during the loop.  IOW if an entry is
> untouched during the loop, then it will be passed (exactly once) to the
> function.  If an entry is added or removed during the loop, then maybe
> it will be passed to the function, but maybe not.  And if an entry is
> modified during the loop, then it will be passed to the function
> (exactly once) and the value passed will be the one that happens to be
> current when the function is called.
>
> More specifically, you're guaranteed when entering the function that the
> key you've received does currently exist in the table and is currently
> associated with the value you just received.

Could one expand documentation of `maphash' like this?  I'm not
sure it follows from what you wrote, but I understood it like
this:

    Call FUNCTION for all entries in hash table TABLE.
    FUNCTION is called with two arguments, KEY and VALUE.
    `maphash' always returns nil.

    FUNCTION will usually just inspect its arguments, but may also
    alter TABLE and this will not cause `maphash' to malfunction.
    However, some effects are not fully defined, see below.

    If FUNCTION adds an entry to TABLE, it may or may not be called
    with the added key/value pair.

    If FUNCTION changes value already associated with a key and it
    has not been called with that key yet, it will be called with key
    and the new value during the iteration later.  Otherwise it will
    not be called for that key again.

    If FUNCTION removes a key and it has not been called with it yet,
    it will not be called for the removed key in the future either.

    Note that if FUNCTION removes or changes value only for KEY it is
    called with, the behavior is completely defined.

Another option is to add this (probably with more explanation) to
the manual and add just a sentence to the tune of "see manual for
details" to the docstring.

Paul



^ permalink raw reply	[flat|nested] 7+ messages in thread
* maphash: improve docstring
@ 2016-03-28 20:31 Paul Pogonyshev
  2016-03-28 21:57 ` Stefan Monnier
  0 siblings, 1 reply; 7+ messages in thread
From: Paul Pogonyshev @ 2016-03-28 20:31 UTC (permalink / raw)
  To: emacs-devel

Docstring for `maphash' doesn't give any answer to the following
question: is the callback FUNCTION allowed to access the hash
table in read mode (I guess yes) and write mode?  About the
latter I have no idea without studying internal implementation.

E.g., is the following code going to work as a way to filter a
hash table?

    (maphash (lambda (key value)
               (unless (i-like-it value)
                 (remhash key hash-table)))
             hash-table)

Paul



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

end of thread, other threads:[~2016-04-02 21:44 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-29 18:58 maphash: improve docstring Paul Pogonyshev
2016-03-29 22:44 ` Stefan Monnier
2016-04-02 12:13   ` Paul Pogonyshev
2016-04-02 21:44     ` Stefan Monnier
  -- strict thread matches above, loose matches on Subject: below --
2016-03-28 20:31 Paul Pogonyshev
2016-03-28 21:57 ` Stefan Monnier
2016-03-29 12:55   ` Stefan Monnier

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.