unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* `remove-duplicates'
@ 2011-07-10 15:05 Lars Magne Ingebrigtsen
  2011-07-10 15:27 ` `remove-duplicates' Juanma Barranquero
  2011-07-12  3:48 ` `remove-duplicates' Stefan Monnier
  0 siblings, 2 replies; 6+ messages in thread
From: Lars Magne Ingebrigtsen @ 2011-07-10 15:05 UTC (permalink / raw)
  To: emacs-devel

Would anybody mind if I add a simple version of `remove-duplicates' to
subr.el?  I'm tired of rewriting the same loop...

It'll be one of those

(if (null (featurep 'cl))
    (progn
(defun remove-duplicates ...)

things.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




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

* Re: `remove-duplicates'
  2011-07-10 15:05 `remove-duplicates' Lars Magne Ingebrigtsen
@ 2011-07-10 15:27 ` Juanma Barranquero
  2011-07-10 16:06   ` `remove-duplicates' David Kastrup
  2011-07-12  3:48 ` `remove-duplicates' Stefan Monnier
  1 sibling, 1 reply; 6+ messages in thread
From: Juanma Barranquero @ 2011-07-10 15:27 UTC (permalink / raw)
  To: emacs-devel

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))

    Juanma



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

* Re: `remove-duplicates'
  2011-07-10 15:27 ` `remove-duplicates' Juanma Barranquero
@ 2011-07-10 16:06   ` David Kastrup
  2011-07-10 16:37     ` `remove-duplicates' Drew Adams
  0 siblings, 1 reply; 6+ messages in thread
From: David Kastrup @ 2011-07-10 16:06 UTC (permalink / raw)
  To: emacs-devel

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




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

* RE: `remove-duplicates'
  2011-07-10 16:06   ` `remove-duplicates' David Kastrup
@ 2011-07-10 16:37     ` Drew Adams
  0 siblings, 0 replies; 6+ messages in thread
From: Drew Adams @ 2011-07-10 16:37 UTC (permalink / raw)
  To: 'David Kastrup', emacs-devel

One comment wrt adding another Emacs-Lisp function (e.g. `remove-duplicates')
that has the same name as an existing Emacs-Lisp function provided by cl.el:
Please don't do it; instead, change the name so there is no clash.

It's too late to rename the cl.el function by adding a `*' suffix or something,
unless you want to break backward compatibility.

The problem with having the same name, which already happens in a couple of
cases, is that the functions have different behavior (e.g. keywords), or at a
minimum different doc.  This is a source of confusion, if not errors (which may
not be immediately obvious).

If you call your new function `remove-dups' or something there will be no
problem.  But I'm really not trying to say anything particular about what the
proper solution is; just pointing to a potential problem that is worth avoiding.





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

* Re: `remove-duplicates'
  2011-07-10 15:05 `remove-duplicates' Lars Magne Ingebrigtsen
  2011-07-10 15:27 ` `remove-duplicates' Juanma Barranquero
@ 2011-07-12  3:48 ` Stefan Monnier
  2011-07-12  7:22   ` `remove-duplicates' Lars Magne Ingebrigtsen
  1 sibling, 1 reply; 6+ messages in thread
From: Stefan Monnier @ 2011-07-12  3:48 UTC (permalink / raw)
  To: emacs-devel

> Would anybody mind if I add a simple version of `remove-duplicates' to
> subr.el?  I'm tired of rewriting the same loop...

We have delete-dups which seems to be good enough for most uses so far.


        Stefan



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

* Re: `remove-duplicates'
  2011-07-12  3:48 ` `remove-duplicates' Stefan Monnier
@ 2011-07-12  7:22   ` Lars Magne Ingebrigtsen
  0 siblings, 0 replies; 6+ messages in thread
From: Lars Magne Ingebrigtsen @ 2011-07-12  7:22 UTC (permalink / raw)
  To: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> We have delete-dups which seems to be good enough for most uses so far.

Ah, right.  I'll use that instead.

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




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

end of thread, other threads:[~2011-07-12  7:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-07-10 15:05 `remove-duplicates' Lars Magne Ingebrigtsen
2011-07-10 15:27 ` `remove-duplicates' Juanma Barranquero
2011-07-10 16:06   ` `remove-duplicates' David Kastrup
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

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).