unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* inverse of add-to-list: remove-from-list
@ 2020-10-13  8:42 ` Uwe Brauer
  2020-10-13  9:09   ` Andreas Schwab
                     ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Uwe Brauer @ 2020-10-13  8:42 UTC (permalink / raw)
  To: emacs-devel



Hi

This has always bothered me: for example 

(add-to-list 'org-export-filter-src-block-functions
         'my-html-filter-src-blocks)

Adds a function to the variable org-export-filter-src-block-function

But there is, as far as I know, no inverse functionality 

(remove-from-list 'org-export-filter-src-block-functions
         'my-html-filter-src-blocks)

Does not exist. Wouldn't that be useful?

Reason there is add-hook and remove-hook

Regards

Uwe Brauer 




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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13  8:42 ` Uwe Brauer
@ 2020-10-13  9:09   ` Andreas Schwab
  2020-10-13  9:34     ` Thibaut Verron
  2020-10-13 13:57   ` Stefan Monnier
  2020-10-14  5:28   ` Teemu Likonen
  2 siblings, 1 reply; 14+ messages in thread
From: Andreas Schwab @ 2020-10-13  9:09 UTC (permalink / raw)
  To: emacs-devel

On Okt 13 2020, Uwe Brauer wrote:

> But there is, as far as I know, no inverse functionality 
>
> (remove-from-list 'org-export-filter-src-block-functions
>          'my-html-filter-src-blocks)
>
> Does not exist. Wouldn't that be useful?

Doesn't remove do what you need?

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13  9:09   ` Andreas Schwab
@ 2020-10-13  9:34     ` Thibaut Verron
  2020-10-13 11:24       ` Uwe Brauer
  0 siblings, 1 reply; 14+ messages in thread
From: Thibaut Verron @ 2020-10-13  9:34 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: emacs-devel

Le mar. 13 oct. 2020 à 11:10, Andreas Schwab <schwab@linux-m68k.org> a écrit :
>
> On Okt 13 2020, Uwe Brauer wrote:
>
> > But there is, as far as I know, no inverse functionality
> >
> > (remove-from-list 'org-export-filter-src-block-functions
> >          'my-html-filter-src-blocks)
> >
> > Does not exist. Wouldn't that be useful?
>
> Doesn't remove do what you need?

Note that the syntax is slightly different: the element comes second,
and the list is unquoted.



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13  9:34     ` Thibaut Verron
@ 2020-10-13 11:24       ` Uwe Brauer
  0 siblings, 0 replies; 14+ messages in thread
From: Uwe Brauer @ 2020-10-13 11:24 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 954 bytes --]

>>> "TV" == Thibaut Verron <thibaut.verron@gmail.com> writes:

> Le mar. 13 oct. 2020 à 11:10, Andreas Schwab <schwab@linux-m68k.org> a écrit :
>> 
>> On Okt 13 2020, Uwe Brauer wrote:
>> 
>> > But there is, as far as I know, no inverse functionality
>> >
>> > (remove-from-list 'org-export-filter-src-block-functions
>> >          'my-html-filter-src-blocks)
>> >
>> > Does not exist. Wouldn't that be useful?
>> 
>> Doesn't remove do what you need?

> Note that the syntax is slightly different: the element comes second,
> and the list is unquoted.

Do you mean 

(setq org-export-filter-src-block-functions (remove 'my-html-filter-src-blocks org-export-filter-src-block-functions))

While:

(add-to-list 'org-export-filter-src-block-functions
         'my-html-filter-src-blocks)


I think 

(remove-from-list 'org-export-filter-src-block-functions
         'my-html-filter-src-blocks)

Would be easier to recall.

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13  8:42 ` Uwe Brauer
  2020-10-13  9:09   ` Andreas Schwab
@ 2020-10-13 13:57   ` Stefan Monnier
  2020-10-14  5:58     ` Thibaut Verron
  2020-10-14  5:28   ` Teemu Likonen
  2 siblings, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2020-10-13 13:57 UTC (permalink / raw)
  To: emacs-devel

Uwe Brauer [2020-10-13 10:42:46] wrote:
> This has always bothered me: for example 
>
> (add-to-list 'org-export-filter-src-block-functions
>          'my-html-filter-src-blocks)

I think this reflects a misdesign in `org-export-filters-alist`.
It should use hooks rather than lists of functions.  This way you'd use
`add-hook` instead of `add-to-list`.

> (remove-from-list 'org-export-filter-src-block-functions
>          'my-html-filter-src-blocks)
>
> Does not exist. Wouldn't that be useful?

I think if we want to add something to remove an element from a variable
holding a list, then we should make it accept a "place" rather than
a symbol as argument.  I.e. make it the opposite of `push` (or
`cl-pushnew`) rather than the opposite of `add-to-list`.


        Stefan "who dislikes `add-to-list`"





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

* Re: inverse of add-to-list: remove-from-list
@ 2020-10-13 19:34 Boruch Baum
  2020-10-13  8:42 ` Uwe Brauer
  2020-10-13 20:01 ` Andreas Schwab
  0 siblings, 2 replies; 14+ messages in thread
From: Boruch Baum @ 2020-10-13 19:34 UTC (permalink / raw)
  To: Emacs-Devel List

> On Okt 13 2020, Uwe Brauer wrote:
> > But there is, as far as I know, no inverse functionality

1] Similar to what Andreas suggested, try this:

     (let ((aa '(1 2 3 1 4 1 2)))
       (delete 2 aa)
       aa)

   And note that the fine distinction made in the docstring for the
   `delete' function:

     "If SEQ is not a list, deletion is never performed destructively;
      instead this function creates and returns a new vector or string.

      Write ‘(setq foo (delete element foo))’ to be sure of correctly
      changing the value of a sequence ‘foo’.

   So, in the case of SEQ being a list, there is no need to `setq foo'.

2] That really could/should be made clearer in the docstring; something
   like:

  "If SEQ is a list, the original symbol is modified in place, ie.
   destructively. If SEQ is not a list, eg. a vector or string, this
   function creates and returns a new vector or string, and you must
   explicitly ‘(setq foo (delete element foo))’ to alter the original."

  If there's agreement on this docstring change, can it be committed?

3] Emacs also offers a more flexible function `cl-delete'. See there.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13 19:34 inverse of add-to-list: remove-from-list Boruch Baum
  2020-10-13  8:42 ` Uwe Brauer
@ 2020-10-13 20:01 ` Andreas Schwab
  2020-10-13 20:07   ` Boruch Baum
  1 sibling, 1 reply; 14+ messages in thread
From: Andreas Schwab @ 2020-10-13 20:01 UTC (permalink / raw)
  To: Boruch Baum; +Cc: Emacs-Devel List

On Okt 13 2020, Boruch Baum wrote:

>> On Okt 13 2020, Uwe Brauer wrote:
>> > But there is, as far as I know, no inverse functionality
>
> 1] Similar to what Andreas suggested, try this:
>
>      (let ((aa '(1 2 3 1 4 1 2)))
>        (delete 2 aa)
>        aa)
>
>    And note that the fine distinction made in the docstring for the
>    `delete' function:
>
>      "If SEQ is not a list, deletion is never performed destructively;
>       instead this function creates and returns a new vector or string.
>
>       Write ‘(setq foo (delete element foo))’ to be sure of correctly
>       changing the value of a sequence ‘foo’.
>
>    So, in the case of SEQ being a list, there is no need to `setq foo'.

This is not true.  If the element to delete is the first element of the
list, it returns the cdr of the list.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13 20:01 ` Andreas Schwab
@ 2020-10-13 20:07   ` Boruch Baum
  2020-10-13 20:41     ` Andreas Schwab
  0 siblings, 1 reply; 14+ messages in thread
From: Boruch Baum @ 2020-10-13 20:07 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Emacs-Devel List

On 2020-10-13 22:01, Andreas Schwab wrote:
> On Okt 13 2020, Boruch Baum wrote:
>
> >> On Okt 13 2020, Uwe Brauer wrote:
> >> > But there is, as far as I know, no inverse functionality
> >
> > 1] Similar to what Andreas suggested, try this:
> >
> >      (let ((aa '(1 2 3 1 4 1 2)))
> >        (delete 2 aa)
> >        aa)
> >
> >    And note that the fine distinction made in the docstring for the
> >    `delete' function:
> >
> >      "If SEQ is not a list, deletion is never performed destructively;
> >       instead this function creates and returns a new vector or string.
> >
> >       Write ‘(setq foo (delete element foo))’ to be sure of correctly
> >       changing the value of a sequence ‘foo’.
> >
> >    So, in the case of SEQ being a list, there is no need to `setq foo'.
>
> This is not true.

Oops. You're correct...

>  If the element to delete is the first element of the list, it returns
> the cdr of the list.

... but to quibble, that's not exactly what I'm getting

  (let ((aa '(1 2 3 1 4 1 2)))
    (delete 1 aa)
    aa)

returns (1 2 3 4 2), so it seems to destructively modify the cdr of the
list.

--
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13 20:07   ` Boruch Baum
@ 2020-10-13 20:41     ` Andreas Schwab
  0 siblings, 0 replies; 14+ messages in thread
From: Andreas Schwab @ 2020-10-13 20:41 UTC (permalink / raw)
  To: Boruch Baum; +Cc: Emacs-Devel List

On Okt 13 2020, Boruch Baum wrote:

> ... but to quibble, that's not exactly what I'm getting
>
>   (let ((aa '(1 2 3 1 4 1 2)))
>     (delete 1 aa)
>     aa)
>
> returns (1 2 3 4 2),

QED.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13  8:42 ` Uwe Brauer
  2020-10-13  9:09   ` Andreas Schwab
  2020-10-13 13:57   ` Stefan Monnier
@ 2020-10-14  5:28   ` Teemu Likonen
  2 siblings, 0 replies; 14+ messages in thread
From: Teemu Likonen @ 2020-10-14  5:28 UTC (permalink / raw)
  To: Boruch Baum, Emacs-Devel List

[-- Attachment #1: Type: text/plain, Size: 2214 bytes --]

* 2020-10-13 15:34:00-04, Boruch Baum wrote:

>      (let ((aa '(1 2 3 1 4 1 2)))
>        (delete 2 aa)
>        aa)

In Lisp world it is not recommended to use destructive operations like
DELETE for literal objects like list created with QUOTE operator '(1 2)
or a string created with quotation marks ("abc"). Compilers can create a
single object for all literally defined object. Emacs Lisp manual
"(elisp) Equality Predicates" says:

    The Emacs Lisp byte compiler may collapse identical literal objects,
    such as literal strings, into references to the same object, with
    the effect that the byte-compiled code will compare such objects as
    ‘eq’, while the interpreted version of the same code will not.

For example:

    (defvar foo '(1 2 3))

    (let ((bar '(1 2 3)))
      (eq foo bar)               ; This may return T.
      (setq bar (delete 3 bar))) ; This may also modify FOO.

So, using a destructive operation for a literal object may change it
everywhere. To create a modifiable objects we should use LIST,
MAKE-LIST, MAKE-STRING, COPY-SEQUENCE, CL-COPY-SEQ, for example.

>    And note that the fine distinction made in the docstring for the
>    `delete' function:
>
>      "If SEQ is not a list, deletion is never performed destructively;
>       instead this function creates and returns a new vector or string.
>
>       Write ‘(setq foo (delete element foo))’ to be sure of correctly
>       changing the value of a sequence ‘foo’.
>
>    So, in the case of SEQ being a list, there is no need to `setq foo'.

In Lisp manuals "destructive" means that the operation may modify the
original object given as an argument. It doesn't promise anything else.
Sometimes the original object is modified, sometimes it is not. Even if
the original object is modified it doesn't promise that it's the same as
the return value of a destructive function. The original object and the
return value can be partially the same: lists may share some cons cells.
We must always use the return value of a destructive function.

-- 
/// Teemu Likonen - .-.. https://www.iki.fi/tlikonen/
// OpenPGP: 4E1055DC84E9DFF613D78557719D69D324539450

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 251 bytes --]

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

* Re: inverse of add-to-list: remove-from-list
  2020-10-13 13:57   ` Stefan Monnier
@ 2020-10-14  5:58     ` Thibaut Verron
  2020-10-15 15:28       ` Uwe Brauer
  2020-10-15 16:31       ` Stefan Monnier
  0 siblings, 2 replies; 14+ messages in thread
From: Thibaut Verron @ 2020-10-14  5:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Le mar. 13 oct. 2020 à 15:58, Stefan Monnier
<monnier@iro.umontreal.ca> a écrit :
>
> Uwe Brauer [2020-10-13 10:42:46] wrote:
> > This has always bothered me: for example
> >
> > (add-to-list 'org-export-filter-src-block-functions
> >          'my-html-filter-src-blocks)
>
> I think this reflects a misdesign in `org-export-filters-alist`.
> It should use hooks rather than lists of functions.  This way you'd use
> `add-hook` instead of `add-to-list`.

I might be missing something, but where would the harm be in using

> (add-hook 'org-export-filter-src-block-functions 'my-html-filter-src-blocks)

even if it is not a hook?

> I think if we want to add something to remove an element from a variable
> holding a list, then we should make it accept a "place" rather than
> a symbol as argument.  I.e. make it the opposite of `push` (or
> `cl-pushnew`) rather than the opposite of `add-to-list`.
>
> Stefan "who dislikes `add-to-list`"

It looks like for that we already have remove and cl-remove indeed.

So basically the answer would be, instead of looking for an opposite
to add-to-list, use cl-pushnew which has a natural opposite in
cl-remove. Is that so?

For an exact opposite to remove, should there be an alias to the wrapper below?

> (lambda (elt place) (pushnew elt place :test 'equal))



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

* Re: inverse of add-to-list: remove-from-list
  2020-10-14  5:58     ` Thibaut Verron
@ 2020-10-15 15:28       ` Uwe Brauer
  2020-10-15 16:31       ` Stefan Monnier
  1 sibling, 0 replies; 14+ messages in thread
From: Uwe Brauer @ 2020-10-15 15:28 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 277 bytes --]

>>> "TV" == Thibaut Verron <thibaut.verron@gmail.com> writes:

> I might be missing something, but where would the harm be in using

> (add-hook 'org-export-filter-src-block-functions 'my-html-filter-src-blocks)

> even if it is not a hook?

I'd love to clarify that question!

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 5673 bytes --]

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

* Re: inverse of add-to-list: remove-from-list
  2020-10-14  5:58     ` Thibaut Verron
  2020-10-15 15:28       ` Uwe Brauer
@ 2020-10-15 16:31       ` Stefan Monnier
  2020-10-16 11:14         ` Thibaut Verron
  1 sibling, 1 reply; 14+ messages in thread
From: Stefan Monnier @ 2020-10-15 16:31 UTC (permalink / raw)
  To: Thibaut Verron; +Cc: emacs-devel

> I might be missing something, but where would the harm be in using
>> (add-hook 'org-export-filter-src-block-functions 'my-html-filter-src-blocks)
> even if it is not a hook?

I think none, but it would be harmful to do:

    (add-hook 'org-export-filter-src-block-functions
              #'my-html-filter-src-blocks
              nil t)

> So basically the answer would be, instead of looking for an opposite
> to add-to-list, use cl-pushnew which has a natural opposite in
> cl-remove. Is that so?

`cl-remove` is not the opposite of `cl-pushnew`, because

    (cl-pushnew V X :test F)
    (cl-remove  V X :test F)

will usually result in X being different from what it started (because
`cl-remove` does not modify its argument X).


        Stefan




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

* Re: inverse of add-to-list: remove-from-list
  2020-10-15 16:31       ` Stefan Monnier
@ 2020-10-16 11:14         ` Thibaut Verron
  0 siblings, 0 replies; 14+ messages in thread
From: Thibaut Verron @ 2020-10-16 11:14 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Le jeu. 15 oct. 2020 à 18:31, Stefan Monnier
<monnier@iro.umontreal.ca> a écrit :
>
> > I might be missing something, but where would the harm be in using
> >> (add-hook 'org-export-filter-src-block-functions 'my-html-filter-src-blocks)
> > even if it is not a hook?
>
> I think none, but it would be harmful to do:
>
>     (add-hook 'org-export-filter-src-block-functions
>               #'my-html-filter-src-blocks
>               nil t)

Oh, right, thanks.

> > So basically the answer would be, instead of looking for an opposite
> > to add-to-list, use cl-pushnew which has a natural opposite in
> > cl-remove. Is that so?
>
> `cl-remove` is not the opposite of `cl-pushnew`, because
>
>     (cl-pushnew V X :test F)
>     (cl-remove  V X :test F)
>
> will usually result in X being different from what it started (because
> `cl-remove` does not modify its argument X).

Oops. I guess it goes to say how confusing the situation is. :)



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

end of thread, other threads:[~2020-10-16 11:14 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-13 19:34 inverse of add-to-list: remove-from-list Boruch Baum
2020-10-13  8:42 ` Uwe Brauer
2020-10-13  9:09   ` Andreas Schwab
2020-10-13  9:34     ` Thibaut Verron
2020-10-13 11:24       ` Uwe Brauer
2020-10-13 13:57   ` Stefan Monnier
2020-10-14  5:58     ` Thibaut Verron
2020-10-15 15:28       ` Uwe Brauer
2020-10-15 16:31       ` Stefan Monnier
2020-10-16 11:14         ` Thibaut Verron
2020-10-14  5:28   ` Teemu Likonen
2020-10-13 20:01 ` Andreas Schwab
2020-10-13 20:07   ` Boruch Baum
2020-10-13 20:41     ` Andreas Schwab

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).