;;; Haven't we all felt the need to sanitize your sequences and making each ;;; element "unique"? I haven't, but I had some spare minutes waiting for ;;; a friend over lunch and since I'd been running the unix command uniq(1) ;;; and been irritated at its shortcomings, this came to be:
(defun unique (data &key (test #'eql) (key #'identity)) (let ((acc nil) (type (typecase data (vector 'vector) (string 'string) (list 'list)))) (flet ((frob (data) (unless (member (funcall key data) acc :test test :key key) (push data acc)))) (map nil #'frob data)) (coerce (nreverse acc) type)))
Ingvar writes:
;;; Haven't we all felt the need to sanitize your sequences and making each ;;; element "unique"? I haven't, but I had some spare minutes waiting for ;;; a friend over lunch and since I'd been running the unix command uniq(1) ;;; and been irritated at its shortcomings, this came to be:
(defun unique (data &key (test #'eql) (key #'identity)) (let ((acc nil) (type (typecase data (vector 'vector) (string 'string) (list 'list)))) (flet ((frob (data) (unless (member (funcall key data) acc :test test :key key) (push data acc)))) (map nil #'frob data)) (coerce (nreverse acc) type)))
(defun unique (data &key (test #'eql) (key #'identity)) (remove-duplicates data :test test :key key))