From: David Kastrup <dak@gnu.org>
To: emacs-devel@gnu.org
Subject: Re: `remove-duplicates'
Date: Sun, 10 Jul 2011 18:06:14 +0200 [thread overview]
Message-ID: <877h7q6ryh.fsf@fencepost.gnu.org> (raw)
In-Reply-To: CAAeL0ST=+SE32aaMiC1xC9J_pg=gwsrR3cwUAuGgoANBCuep_A@mail.gmail.com
Juanma Barranquero <lekktu@gmail.com> writes:
> On Sun, Jul 10, 2011 at 17:05, Lars Magne Ingebrigtsen <larsi@gnus.org> wrote:
>
>> Would anybody mind if I add a simple version of `remove-duplicates' to
>> subr.el? I'm tired of rewriting the same loop...
>
> I think there are also quite a few cases in the sources of destructive
> deleting of *consecutive* duplicates. I once proposed this:
>
> (defun uniqify (list)
> "Destructively remove consecutive `equal' duplicates from LIST.
> Store the result in LIST and return it. LIST must be a proper list."
> (let ((l list))
> (while (cdr l)
> (if (equal (car l) (cadr l))
> (setcdr l (cddr l))
> (setq l (cdr l))))
> list))
I have something like this here:
(defun uniquify (list predicate)
(let* ((p list) lst (x1 (make-symbol "x1"))
(x2 (make-symbol "x2")))
(while p
(push p lst)
(setq p (cdr p)))
;;; (princ lst)(princ "\n")
(setq lst
(sort lst `(lambda(,x1 ,x2)
(funcall ',predicate (car ,x1) (car ,x2)))))
;;; lst now contains all sorted sublists, with equal cars being
;;; sorted in order of increasing length (from end of list to start).
;;
(while (cdr lst)
(unless (funcall predicate (car (car lst)) (car (cadr lst)))
(setcar (car lst) x1))
(setq lst (cdr lst)))
(delq x1 list)))
One could turn the predicate into an optional argument. The idea is
that with an order relation (in this case "less"), the behavior can be
turned from O(n^2) to O(n log n). Another possibility would be to
remove duplicates by using hashes.
--
David Kastrup
next prev parent reply other threads:[~2011-07-10 16:06 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-07-10 15:05 `remove-duplicates' Lars Magne Ingebrigtsen
2011-07-10 15:27 ` `remove-duplicates' Juanma Barranquero
2011-07-10 16:06 ` David Kastrup [this message]
2011-07-10 16:37 ` `remove-duplicates' Drew Adams
2011-07-12 3:48 ` `remove-duplicates' Stefan Monnier
2011-07-12 7:22 ` `remove-duplicates' Lars Magne Ingebrigtsen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877h7q6ryh.fsf@fencepost.gnu.org \
--to=dak@gnu.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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.