Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

remove sublist

6 views
Skip to first unread message

Emanuel Berg

unread,
Apr 2, 2018, 8:09:27 AM4/2/18
to
Better/built-in/semi-built-in way to do:

(cl-remove-if
(lambda (s) (or (string= "a" s)
(string= "b" s) ))
'("0" "a" "b" "c" "d") )

(cl-remove-if
(lambda (s) (pcase s
("a" t)
("b" t) ))
'("0" "a" "b" "c" "d") )

?

--
underground experts united
http://user.it.uu.se/~embe8573

Yuri Khan

unread,
Apr 2, 2018, 9:07:10 AM4/2/18
to Emanuel Berg, help-gnu-emacs
On Mon, Apr 2, 2018 at 7:09 PM, Emanuel Berg <moa...@zoho.com> wrote:
> Better/built-in/semi-built-in way to do:
>
> (cl-remove-if
> (lambda (s) (or (string= "a" s)
> (string= "b" s) ))
> '("0" "a" "b" "c" "d") )
>
> (cl-remove-if
> (lambda (s) (pcase s
> ("a" t)
> ("b" t) ))
> '("0" "a" "b" "c" "d") )
>
> ?

Maybe

(cl-set-difference '("0" "a" "b" "c" "d")
'("a" "b")
:test 'string=)

?

Emanuel Berg

unread,
Apr 2, 2018, 9:29:00 AM4/2/18
to
Yuri Khan wrote:

> Maybe
>
> (cl-set-difference '("0" "a" "b" "c" "d")
> '("a" "b")
> :test 'string=)
>
> ?

Excellent, thanks!

You got the right idea, "sublist" as in the
math unordered set sense - and not "set" as in
the serpent god of the paleo-pharaohs, here!

And items in the sublist that do not appear
should be dropped, as happens.

Should the test function be hash-quoted?

Only strange thing is the order is reversed,
except for the empty list as sublist! I just
said it was in the unordered sense, however one
might just as well keep the order, especially
since even/particularly in the math unordered
set sense, {1 2 3} = {3 2 1}, right? So it
should work both ways; and it might be useful
for example if you have a list of functions,
(f1 f2 f3 f4), and it is a priority list so if
f1 is defined, run it, if not, try/run f2,
etc., here, if the user dislikes f2 and f3, but
have them defined for other purposes, s/he can
remove them from the list without changing
the priority!

However how is that supposed to be done?
What about

(defun drop-sublist (l sl)
(if sl
(reverse (cl-set-difference l sl :test #'string-equal))
l))
;; (drop-sublist '("0" "a" "b" "c" "d") '("a" "b" "x"))
;; (drop-sublist '("0" "a" "b" "c" "d") '(x))
;; (drop-sublist '("0" "a" "b" "c" "d") '())

?

It sort of goes against grain to reverse to
*preserve* the order, but I suppose this is
what makes programming an engineering and not
a scientific endeavor...

Yuri Khan

unread,
Apr 2, 2018, 10:05:47 AM4/2/18
to Emanuel Berg, help-gnu-emacs
On Mon, Apr 2, 2018 at 8:28 PM, Emanuel Berg <moa...@zoho.com> wrote:

>> (cl-set-difference '("0" "a" "b" "c" "d")
>> '("a" "b")
>> :test 'string=)
>
> Excellent, thanks!
>
> You got the right idea, "sublist" as in the
> math unordered set sense - and not "set" as in
> the serpent god of the paleo-pharaohs, here!

Wasn’t that Seth? (oh, Wikipedia says both variants are used.)

> Should the test function be hash-quoted?

Probably.

> Only strange thing is the order is reversed,
> except for the empty list as sublist!

For me it isn’t, but I’m on 25.1. It’s not strange though — it’s more
efficient to traverse the original list first-to-last and build the
result list last-to-first; and it’s even more efficient to detect that
the second argument is empty and return the first argument as result.

If you need to preserve the original list’s order, you should probably
use remove-if, as nothing in the name or documentation of
set-difference will or should guarantee order.

(cl-remove-if (lambda (s) (member s '("a" "b")))
'("0" "a" "b" "c" "d"))

(For different types of elements, take your choice of member, memq or memql.)

Emanuel Berg

unread,
Apr 2, 2018, 10:41:52 AM4/2/18
to
Yuri Khan wrote:

> (cl-remove-if (lambda (s) (member s '("a" "b")))
> '("0" "a" "b" "c" "d"))

`member'! I had forgotten about that. Now I can
use my original code only with `member' instead
of `if'/`pcase'...

Stefan Monnier

unread,
Apr 2, 2018, 12:51:43 PM4/2/18
to help-gn...@gnu.org
> Only strange thing is the order is reversed,

That'd be a bug (which I don't see here).


Stefan


Emanuel Berg

unread,
Apr 2, 2018, 4:20:36 PM4/2/18
to
Stefan Monnier wrote:

>> Only strange thing is the order is reversed,
>
> That'd be a bug (which I don't see here).

OK, does that mean, what works for me (last),
gets reversed for you?

I'm on

GNU Emacs 24.4.1
(arm-unknown-linux-gnueabihf, GTK+ Version
3.14.5) of 2015-03-10 on bm-wb-01, modified
by Debian

I think the `member' solution is better, more
generic, but it can be interesting to track the
bug nonetheless.

BTW if it fixed in 25.1 but not in 24.4, how
about running diff(1) on cl-seq.el and check it
out around line ~835?

Second, emacs-24 is the most recent version
I get from


deb [arch=armhf] http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb-src http://archive.raspbian.org/raspbian/ jessie main contrib non-free rpi


(yanking /etc/apt/sources.list )

Because it is still what is offered from the
main repos, does that mean bugs are bothered
with even tho there is a newer version? Boy,
that sounds like a handful of work :) Or
perhaps just bugs which are considered
critical? Or not at all? Sounds like very
demoralizing work to say the least, fixing bugs
that are already fixed, and can introduce new
bugs or be dependent on ever newer material,
not available at that version, and so on
forever, like everyone else is using the new
versions, only one or two guys never get there
as stuck, they just fix bugs in obsolete
version all day long. Ha ha ha :D

(require 'cl-lib)
(defun drop-sublist (l sl)
(if sl
(reverse (cl-set-difference l sl :test #'string-equal))
l))
;; (drop-sublist '("0" "a" "b" "c" "d") '("a" "b" "x"))
;; (drop-sublist '("0" "a" "b" "c" "d") '(x))
;; (drop-sublist '("0" "a" "b" "c" "d") '())
;; (drop-sublist '() '(x))

Robert L.

unread,
Apr 7, 2018, 6:55:24 PM4/7/18
to
On 4/2/2018, Emanuel Berg wrote:

>
> (cl-remove-if
> (lambda (s) (or (string= "a" s)
> (string= "b" s) ))
> '("0" "a" "b" "c" "d") )

(defun remove-these (targets xs)
(let (result)
(dolist (x xs (reverse result))
(unless (member x targets)
(push x result)))))

(remove-these '(a e) '(a b e f o a g))
===>
(b f o g)

--
[Amazon bans history book after it received 300 5-star reviews.]
http://archive.org/details/nolies

Robert L.

unread,
Apr 12, 2018, 5:36:15 AM4/12/18
to
On 4/7/2018, Robert L. wrote:

> On 4/2/2018, Emanuel Berg wrote:
>
> >
> > (cl-remove-if
> > (lambda (s) (or (string= "a" s)
> > (string= "b" s) ))
> > '("0" "a" "b" "c" "d") )
>
> (defun remove-these (targets xs)
> (let (result)
> (dolist (x xs (reverse result))
> (unless (member x targets)
> (push x result)))))
>
> (remove-these '(a e) '(a b e f o a g))
> ===>
> (b f o g)


(defun remove-these (targets xs)
(if targets
(remove-these (cdr targets) (remove (car targets) xs))
xs))

(remove-these '(a e) '(a b e f o a g))
===>
(b f o g)

--
When the Israeli bombers and torpedo-planes were sent to attack and destroy the
ship, the Jewish commander, seeing that it was an American vessel, had
misgivings and reported to the High Command, which simply repeated the orders
to attack and sink the Liberty.
I have a graphic from ... the oldest daily newspaper of Israel, ... Haaretz....
The headline reads, "But, sir, it's an American ship." "Never mind. Hit her."
http://archive.org/details/nolies

Emanuel Berg

unread,
Apr 12, 2018, 11:10:24 AM4/12/18
to
Robert L. wrote:

> (defun remove-these (targets xs)
> (if targets
> (remove-these (cdr targets) (remove (car targets) xs))
> xs))
>
> (remove-these '(a e) '(a b e f o a g))
> ===>
> (b f o g)

(require 'cl-lib)
(defun drop-sublist (l subl)
"Every element of L that isn't also in SUBL."
(cl-remove-if (lambda (e) (member e subl)) l) )

Eli Zaretskii

unread,
Apr 12, 2018, 11:28:05 AM4/12/18
to help-gn...@gnu.org
> From: Emanuel Berg <moa...@zoho.com>
> Date: Thu, 12 Apr 2018 17:10:21 +0200
>
> Robert L. wrote:
>
> > (defun remove-these (targets xs)
> > (if targets
> > (remove-these (cdr targets) (remove (car targets) xs))
> > xs))
> >
> > (remove-these '(a e) '(a b e f o a g))
> > ===>
> > (b f o g)
>
> (require 'cl-lib)
> (defun drop-sublist (l subl)
> "Every element of L that isn't also in SUBL."
> (cl-remove-if (lambda (e) (member e subl)) l) )

The future is here:

(require 'seq)
(seq-difference '(a b e f o a g) '(a e))
=> (b f o g)

Emanuel Berg

unread,
Apr 12, 2018, 1:43:46 PM4/12/18
to
Eli Zaretskii wrote:

>> (require 'cl-lib)
>> (defun drop-sublist (l subl)
>> "Every element of L that isn't also in SUBL."
>> (cl-remove-if (lambda (e) (member e subl)) l) )
>
> The future is here:
>
> (require 'seq)
> (seq-difference '(a b e f o a g) '(a e))
> => (b f o g)

Added "drop drop-sublist" to my TODO file.
0 new messages