Mercurial > emacs
annotate lisp/emacs-lisp/cl-seq.el @ 64461:f05e7fd81880
(Emacs 18 MUAs): Delete duplicate words.
author | Juri Linkov <juri@jurta.org> |
---|---|
date | Mon, 18 Jul 2005 16:10:59 +0000 |
parents | 18a818a2ee7c |
children | 5b1a238fcbb4 f9a65d7ebd29 |
rev | line source |
---|---|
16057
a74507d555ba
Turn on byte-compile-dynamic.
Richard M. Stallman <rms@gnu.org>
parents:
14169
diff
changeset
|
1 ;;; cl-seq.el --- Common Lisp features, part 3 -*-byte-compile-dynamic: t;-*- |
4355 | 2 |
3 ;; Copyright (C) 1993 Free Software Foundation, Inc. | |
4 | |
5 ;; Author: Dave Gillespie <daveg@synaptics.com> | |
6 ;; Version: 2.02 | |
7 ;; Keywords: extensions | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
12244 | 13 ;; the Free Software Foundation; either version 2, or (at your option) |
4355 | 14 ;; any later version. |
15 | |
16 ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 | |
21 ;; You should have received a copy of the GNU General Public License | |
14169 | 22 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
4355 | 25 |
7942 | 26 ;;; Commentary: |
4355 | 27 |
28 ;; These are extensions to Emacs Lisp that provide a degree of | |
29 ;; Common Lisp compatibility, beyond what is already built-in | |
30 ;; in Emacs Lisp. | |
31 ;; | |
32 ;; This package was written by Dave Gillespie; it is a complete | |
33 ;; rewrite of Cesar Quiroz's original cl.el package of December 1986. | |
34 ;; | |
35 ;; This package works with Emacs 18, Emacs 19, and Lucid Emacs 19. | |
36 ;; | |
37 ;; Bug reports, comments, and suggestions are welcome! | |
38 | |
39 ;; This file contains the Common Lisp sequence and list functions | |
40 ;; which take keyword arguments. | |
41 | |
42 ;; See cl.el for Change Log. | |
43 | |
44 | |
7942 | 45 ;;; Code: |
4355 | 46 |
47 (or (memq 'cl-19 features) | |
48 (error "Tried to load `cl-seq' before `cl'!")) | |
49 | |
50 | |
51 ;;; Keyword parsing. This is special-cased here so that we can compile | |
52 ;;; this file independent from cl-macs. | |
53 | |
54 (defmacro cl-parsing-keywords (kwords other-keys &rest body) | |
55 (cons | |
56 'let* | |
57 (cons (mapcar | |
58 (function | |
59 (lambda (x) | |
60 (let* ((var (if (consp x) (car x) x)) | |
61 (mem (list 'car (list 'cdr (list 'memq (list 'quote var) | |
62 'cl-keys))))) | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
63 (if (eq var :test-not) |
4355 | 64 (setq mem (list 'and mem (list 'setq 'cl-test mem) t))) |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
65 (if (eq var :if-not) |
4355 | 66 (setq mem (list 'and mem (list 'setq 'cl-if mem) t))) |
67 (list (intern | |
68 (format "cl-%s" (substring (symbol-name var) 1))) | |
69 (if (consp x) (list 'or mem (car (cdr x))) mem))))) | |
70 kwords) | |
71 (append | |
72 (and (not (eq other-keys t)) | |
73 (list | |
74 (list 'let '((cl-keys-temp cl-keys)) | |
75 (list 'while 'cl-keys-temp | |
76 (list 'or (list 'memq '(car cl-keys-temp) | |
77 (list 'quote | |
78 (mapcar | |
79 (function | |
80 (lambda (x) | |
81 (if (consp x) | |
82 (car x) x))) | |
83 (append kwords | |
84 other-keys)))) | |
85 '(car (cdr (memq (quote :allow-other-keys) | |
86 cl-keys))) | |
87 '(error "Bad keyword argument %s" | |
88 (car cl-keys-temp))) | |
89 '(setq cl-keys-temp (cdr (cdr cl-keys-temp))))))) | |
90 body)))) | |
91 (put 'cl-parsing-keywords 'lisp-indent-function 2) | |
92 (put 'cl-parsing-keywords 'edebug-form-spec '(sexp sexp &rest form)) | |
93 | |
94 (defmacro cl-check-key (x) | |
95 (list 'if 'cl-key (list 'funcall 'cl-key x) x)) | |
96 | |
97 (defmacro cl-check-test-nokey (item x) | |
98 (list 'cond | |
99 (list 'cl-test | |
100 (list 'eq (list 'not (list 'funcall 'cl-test item x)) | |
101 'cl-test-not)) | |
102 (list 'cl-if | |
103 (list 'eq (list 'not (list 'funcall 'cl-if x)) 'cl-if-not)) | |
104 (list 't (list 'if (list 'numberp item) | |
105 (list 'equal item x) (list 'eq item x))))) | |
106 | |
107 (defmacro cl-check-test (item x) | |
108 (list 'cl-check-test-nokey item (list 'cl-check-key x))) | |
109 | |
110 (defmacro cl-check-match (x y) | |
111 (setq x (list 'cl-check-key x) y (list 'cl-check-key y)) | |
112 (list 'if 'cl-test | |
113 (list 'eq (list 'not (list 'funcall 'cl-test x y)) 'cl-test-not) | |
114 (list 'if (list 'numberp x) | |
115 (list 'equal x y) (list 'eq x y)))) | |
116 | |
117 (put 'cl-check-key 'edebug-form-spec 'edebug-forms) | |
118 (put 'cl-check-test 'edebug-form-spec 'edebug-forms) | |
119 (put 'cl-check-test-nokey 'edebug-form-spec 'edebug-forms) | |
120 (put 'cl-check-match 'edebug-form-spec 'edebug-forms) | |
121 | |
122 (defvar cl-test) (defvar cl-test-not) | |
123 (defvar cl-if) (defvar cl-if-not) | |
124 (defvar cl-key) | |
125 | |
126 | |
127 (defun reduce (cl-func cl-seq &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
128 "Reduce two-argument FUNCTION across SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
129 \nKeywords supported: :start :end :from-end :initial-value :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
130 \n(fn FUNCTION SEQ [KEYWORD VALUE]...)" |
4355 | 131 (cl-parsing-keywords (:from-end (:start 0) :end :initial-value :key) () |
132 (or (listp cl-seq) (setq cl-seq (append cl-seq nil))) | |
133 (setq cl-seq (subseq cl-seq cl-start cl-end)) | |
134 (if cl-from-end (setq cl-seq (nreverse cl-seq))) | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
135 (let ((cl-accum (cond ((memq :initial-value cl-keys) cl-initial-value) |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
136 (cl-seq (cl-check-key (pop cl-seq))) |
4355 | 137 (t (funcall cl-func))))) |
138 (if cl-from-end | |
139 (while cl-seq | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
140 (setq cl-accum (funcall cl-func (cl-check-key (pop cl-seq)) |
4355 | 141 cl-accum))) |
142 (while cl-seq | |
143 (setq cl-accum (funcall cl-func cl-accum | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
144 (cl-check-key (pop cl-seq)))))) |
4355 | 145 cl-accum))) |
146 | |
147 (defun fill (seq item &rest cl-keys) | |
148 "Fill the elements of SEQ with ITEM. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
149 \nKeywords supported: :start :end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
150 \n(fn SEQ ITEM [KEYWORD VALUE]...)" |
4355 | 151 (cl-parsing-keywords ((:start 0) :end) () |
152 (if (listp seq) | |
153 (let ((p (nthcdr cl-start seq)) | |
154 (n (if cl-end (- cl-end cl-start) 8000000))) | |
155 (while (and p (>= (setq n (1- n)) 0)) | |
156 (setcar p item) | |
157 (setq p (cdr p)))) | |
158 (or cl-end (setq cl-end (length seq))) | |
159 (if (and (= cl-start 0) (= cl-end (length seq))) | |
160 (fillarray seq item) | |
161 (while (< cl-start cl-end) | |
162 (aset seq cl-start item) | |
163 (setq cl-start (1+ cl-start))))) | |
164 seq)) | |
165 | |
166 (defun replace (cl-seq1 cl-seq2 &rest cl-keys) | |
167 "Replace the elements of SEQ1 with the elements of SEQ2. | |
168 SEQ1 is destructively modified, then returned. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
169 \nKeywords supported: :start1 :end1 :start2 :end2 |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
170 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" |
4355 | 171 (cl-parsing-keywords ((:start1 0) :end1 (:start2 0) :end2) () |
172 (if (and (eq cl-seq1 cl-seq2) (<= cl-start2 cl-start1)) | |
173 (or (= cl-start1 cl-start2) | |
174 (let* ((cl-len (length cl-seq1)) | |
175 (cl-n (min (- (or cl-end1 cl-len) cl-start1) | |
176 (- (or cl-end2 cl-len) cl-start2)))) | |
177 (while (>= (setq cl-n (1- cl-n)) 0) | |
178 (cl-set-elt cl-seq1 (+ cl-start1 cl-n) | |
179 (elt cl-seq2 (+ cl-start2 cl-n)))))) | |
180 (if (listp cl-seq1) | |
181 (let ((cl-p1 (nthcdr cl-start1 cl-seq1)) | |
182 (cl-n1 (if cl-end1 (- cl-end1 cl-start1) 4000000))) | |
183 (if (listp cl-seq2) | |
184 (let ((cl-p2 (nthcdr cl-start2 cl-seq2)) | |
185 (cl-n (min cl-n1 | |
186 (if cl-end2 (- cl-end2 cl-start2) 4000000)))) | |
187 (while (and cl-p1 cl-p2 (>= (setq cl-n (1- cl-n)) 0)) | |
188 (setcar cl-p1 (car cl-p2)) | |
189 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2)))) | |
190 (setq cl-end2 (min (or cl-end2 (length cl-seq2)) | |
191 (+ cl-start2 cl-n1))) | |
192 (while (and cl-p1 (< cl-start2 cl-end2)) | |
193 (setcar cl-p1 (aref cl-seq2 cl-start2)) | |
194 (setq cl-p1 (cdr cl-p1) cl-start2 (1+ cl-start2))))) | |
195 (setq cl-end1 (min (or cl-end1 (length cl-seq1)) | |
196 (+ cl-start1 (- (or cl-end2 (length cl-seq2)) | |
197 cl-start2)))) | |
198 (if (listp cl-seq2) | |
199 (let ((cl-p2 (nthcdr cl-start2 cl-seq2))) | |
200 (while (< cl-start1 cl-end1) | |
201 (aset cl-seq1 cl-start1 (car cl-p2)) | |
202 (setq cl-p2 (cdr cl-p2) cl-start1 (1+ cl-start1)))) | |
203 (while (< cl-start1 cl-end1) | |
204 (aset cl-seq1 cl-start1 (aref cl-seq2 cl-start2)) | |
205 (setq cl-start2 (1+ cl-start2) cl-start1 (1+ cl-start1)))))) | |
206 cl-seq1)) | |
207 | |
208 (defun remove* (cl-item cl-seq &rest cl-keys) | |
209 "Remove all occurrences of ITEM in SEQ. | |
210 This is a non-destructive function; it makes a copy of SEQ if necessary | |
211 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
212 \nKeywords supported: :test :test-not :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
213 \n(fn ITEM SEQ [KEYWORD VALUE]...)" |
4355 | 214 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
215 (:start 0) :end) () | |
216 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
217 cl-seq | |
218 (if (or (nlistp cl-seq) (and cl-from-end (< cl-count 4000000))) | |
219 (let ((cl-i (cl-position cl-item cl-seq cl-start cl-end | |
220 cl-from-end))) | |
221 (if cl-i | |
222 (let ((cl-res (apply 'delete* cl-item (append cl-seq nil) | |
223 (append (if cl-from-end | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
224 (list :end (1+ cl-i)) |
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
225 (list :start cl-i)) |
4355 | 226 cl-keys)))) |
227 (if (listp cl-seq) cl-res | |
228 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res)))) | |
229 cl-seq)) | |
230 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
231 (if (= cl-start 0) | |
232 (while (and cl-seq (> cl-end 0) | |
233 (cl-check-test cl-item (car cl-seq)) | |
234 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
235 (> (setq cl-count (1- cl-count)) 0)))) | |
236 (if (and (> cl-count 0) (> cl-end 0)) | |
237 (let ((cl-p (if (> cl-start 0) (nthcdr cl-start cl-seq) | |
238 (setq cl-end (1- cl-end)) (cdr cl-seq)))) | |
239 (while (and cl-p (> cl-end 0) | |
240 (not (cl-check-test cl-item (car cl-p)))) | |
241 (setq cl-p (cdr cl-p) cl-end (1- cl-end))) | |
242 (if (and cl-p (> cl-end 0)) | |
243 (nconc (ldiff cl-seq cl-p) | |
244 (if (= cl-count 1) (cdr cl-p) | |
245 (and (cdr cl-p) | |
246 (apply 'delete* cl-item | |
247 (copy-sequence (cdr cl-p)) | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
248 :start 0 :end (1- cl-end) |
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
249 :count (1- cl-count) cl-keys)))) |
4355 | 250 cl-seq)) |
251 cl-seq))))) | |
252 | |
253 (defun remove-if (cl-pred cl-list &rest cl-keys) | |
254 "Remove all items satisfying PREDICATE in SEQ. | |
255 This is a non-destructive function; it makes a copy of SEQ if necessary | |
256 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
257 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
258 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
259 (apply 'remove* nil cl-list :if cl-pred cl-keys)) |
4355 | 260 |
261 (defun remove-if-not (cl-pred cl-list &rest cl-keys) | |
262 "Remove all items not satisfying PREDICATE in SEQ. | |
263 This is a non-destructive function; it makes a copy of SEQ if necessary | |
264 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
265 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
266 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
267 (apply 'remove* nil cl-list :if-not cl-pred cl-keys)) |
4355 | 268 |
269 (defun delete* (cl-item cl-seq &rest cl-keys) | |
270 "Remove all occurrences of ITEM in SEQ. | |
271 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
272 \nKeywords supported: :test :test-not :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
273 \n(fn ITEM SEQ [KEYWORD VALUE]...)" |
4355 | 274 (cl-parsing-keywords (:test :test-not :key :if :if-not :count :from-end |
275 (:start 0) :end) () | |
276 (if (<= (or cl-count (setq cl-count 8000000)) 0) | |
277 cl-seq | |
278 (if (listp cl-seq) | |
279 (if (and cl-from-end (< cl-count 4000000)) | |
280 (let (cl-i) | |
281 (while (and (>= (setq cl-count (1- cl-count)) 0) | |
282 (setq cl-i (cl-position cl-item cl-seq cl-start | |
283 cl-end cl-from-end))) | |
284 (if (= cl-i 0) (setq cl-seq (cdr cl-seq)) | |
285 (let ((cl-tail (nthcdr (1- cl-i) cl-seq))) | |
286 (setcdr cl-tail (cdr (cdr cl-tail))))) | |
287 (setq cl-end cl-i)) | |
288 cl-seq) | |
289 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
290 (if (= cl-start 0) | |
291 (progn | |
292 (while (and cl-seq | |
293 (> cl-end 0) | |
294 (cl-check-test cl-item (car cl-seq)) | |
295 (setq cl-end (1- cl-end) cl-seq (cdr cl-seq)) | |
296 (> (setq cl-count (1- cl-count)) 0))) | |
297 (setq cl-end (1- cl-end))) | |
298 (setq cl-start (1- cl-start))) | |
299 (if (and (> cl-count 0) (> cl-end 0)) | |
300 (let ((cl-p (nthcdr cl-start cl-seq))) | |
301 (while (and (cdr cl-p) (> cl-end 0)) | |
302 (if (cl-check-test cl-item (car (cdr cl-p))) | |
303 (progn | |
304 (setcdr cl-p (cdr (cdr cl-p))) | |
305 (if (= (setq cl-count (1- cl-count)) 0) | |
306 (setq cl-end 1))) | |
307 (setq cl-p (cdr cl-p))) | |
308 (setq cl-end (1- cl-end))))) | |
309 cl-seq) | |
310 (apply 'remove* cl-item cl-seq cl-keys))))) | |
311 | |
312 (defun delete-if (cl-pred cl-list &rest cl-keys) | |
313 "Remove all items satisfying PREDICATE in SEQ. | |
314 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
315 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
316 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
317 (apply 'delete* nil cl-list :if cl-pred cl-keys)) |
4355 | 318 |
319 (defun delete-if-not (cl-pred cl-list &rest cl-keys) | |
320 "Remove all items not satisfying PREDICATE in SEQ. | |
321 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
322 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
323 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
324 (apply 'delete* nil cl-list :if-not cl-pred cl-keys)) |
4355 | 325 |
326 (defun remove-duplicates (cl-seq &rest cl-keys) | |
327 "Return a copy of SEQ with all duplicate elements removed. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
328 \nKeywords supported: :test :test-not :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
329 \n(fn SEQ [KEYWORD VALUE]...)" |
4355 | 330 (cl-delete-duplicates cl-seq cl-keys t)) |
331 | |
332 (defun delete-duplicates (cl-seq &rest cl-keys) | |
333 "Remove all duplicate elements from SEQ (destructively). | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
334 \nKeywords supported: :test :test-not :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
335 \n(fn SEQ [KEYWORD VALUE]...)" |
4355 | 336 (cl-delete-duplicates cl-seq cl-keys nil)) |
337 | |
338 (defun cl-delete-duplicates (cl-seq cl-keys cl-copy) | |
339 (if (listp cl-seq) | |
340 (cl-parsing-keywords (:test :test-not :key (:start 0) :end :from-end :if) | |
341 () | |
342 (if cl-from-end | |
343 (let ((cl-p (nthcdr cl-start cl-seq)) cl-i) | |
344 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
345 (while (> cl-end 1) | |
346 (setq cl-i 0) | |
347 (while (setq cl-i (cl-position (cl-check-key (car cl-p)) | |
348 (cdr cl-p) cl-i (1- cl-end))) | |
349 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
350 cl-p (nthcdr cl-start cl-seq) cl-copy nil)) | |
351 (let ((cl-tail (nthcdr cl-i cl-p))) | |
352 (setcdr cl-tail (cdr (cdr cl-tail)))) | |
353 (setq cl-end (1- cl-end))) | |
354 (setq cl-p (cdr cl-p) cl-end (1- cl-end) | |
355 cl-start (1+ cl-start))) | |
356 cl-seq) | |
357 (setq cl-end (- (or cl-end (length cl-seq)) cl-start)) | |
358 (while (and (cdr cl-seq) (= cl-start 0) (> cl-end 1) | |
359 (cl-position (cl-check-key (car cl-seq)) | |
360 (cdr cl-seq) 0 (1- cl-end))) | |
361 (setq cl-seq (cdr cl-seq) cl-end (1- cl-end))) | |
362 (let ((cl-p (if (> cl-start 0) (nthcdr (1- cl-start) cl-seq) | |
363 (setq cl-end (1- cl-end) cl-start 1) cl-seq))) | |
364 (while (and (cdr (cdr cl-p)) (> cl-end 1)) | |
365 (if (cl-position (cl-check-key (car (cdr cl-p))) | |
366 (cdr (cdr cl-p)) 0 (1- cl-end)) | |
367 (progn | |
368 (if cl-copy (setq cl-seq (copy-sequence cl-seq) | |
369 cl-p (nthcdr (1- cl-start) cl-seq) | |
370 cl-copy nil)) | |
371 (setcdr cl-p (cdr (cdr cl-p)))) | |
372 (setq cl-p (cdr cl-p))) | |
373 (setq cl-end (1- cl-end) cl-start (1+ cl-start))) | |
374 cl-seq))) | |
375 (let ((cl-res (cl-delete-duplicates (append cl-seq nil) cl-keys nil))) | |
376 (if (stringp cl-seq) (concat cl-res) (vconcat cl-res))))) | |
377 | |
378 (defun substitute (cl-new cl-old cl-seq &rest cl-keys) | |
379 "Substitute NEW for OLD in SEQ. | |
380 This is a non-destructive function; it makes a copy of SEQ if necessary | |
381 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
382 \nKeywords supported: :test :test-not :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
383 \n(fn NEW OLD SEQ [KEYWORD VALUE]...)" |
4355 | 384 (cl-parsing-keywords (:test :test-not :key :if :if-not :count |
385 (:start 0) :end :from-end) () | |
386 (if (or (eq cl-old cl-new) | |
387 (<= (or cl-count (setq cl-from-end nil cl-count 8000000)) 0)) | |
388 cl-seq | |
389 (let ((cl-i (cl-position cl-old cl-seq cl-start cl-end))) | |
390 (if (not cl-i) | |
391 cl-seq | |
392 (setq cl-seq (copy-sequence cl-seq)) | |
393 (or cl-from-end | |
394 (progn (cl-set-elt cl-seq cl-i cl-new) | |
395 (setq cl-i (1+ cl-i) cl-count (1- cl-count)))) | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
396 (apply 'nsubstitute cl-new cl-old cl-seq :count cl-count |
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
397 :start cl-i cl-keys)))))) |
4355 | 398 |
399 (defun substitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
400 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
401 This is a non-destructive function; it makes a copy of SEQ if necessary | |
402 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
403 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
404 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
405 (apply 'substitute cl-new nil cl-list :if cl-pred cl-keys)) |
4355 | 406 |
407 (defun substitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
408 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
409 This is a non-destructive function; it makes a copy of SEQ if necessary | |
410 to avoid corrupting the original SEQ. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
411 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
412 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
413 (apply 'substitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
4355 | 414 |
415 (defun nsubstitute (cl-new cl-old cl-seq &rest cl-keys) | |
416 "Substitute NEW for OLD in SEQ. | |
417 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
418 \nKeywords supported: :test :test-not :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
419 \n(fn NEW OLD SEQ [KEYWORD VALUE]...)" |
4355 | 420 (cl-parsing-keywords (:test :test-not :key :if :if-not :count |
421 (:start 0) :end :from-end) () | |
422 (or (eq cl-old cl-new) (<= (or cl-count (setq cl-count 8000000)) 0) | |
423 (if (and (listp cl-seq) (or (not cl-from-end) (> cl-count 4000000))) | |
424 (let ((cl-p (nthcdr cl-start cl-seq))) | |
425 (setq cl-end (- (or cl-end 8000000) cl-start)) | |
426 (while (and cl-p (> cl-end 0) (> cl-count 0)) | |
427 (if (cl-check-test cl-old (car cl-p)) | |
428 (progn | |
429 (setcar cl-p cl-new) | |
430 (setq cl-count (1- cl-count)))) | |
431 (setq cl-p (cdr cl-p) cl-end (1- cl-end)))) | |
432 (or cl-end (setq cl-end (length cl-seq))) | |
433 (if cl-from-end | |
434 (while (and (< cl-start cl-end) (> cl-count 0)) | |
435 (setq cl-end (1- cl-end)) | |
436 (if (cl-check-test cl-old (elt cl-seq cl-end)) | |
437 (progn | |
438 (cl-set-elt cl-seq cl-end cl-new) | |
439 (setq cl-count (1- cl-count))))) | |
440 (while (and (< cl-start cl-end) (> cl-count 0)) | |
441 (if (cl-check-test cl-old (aref cl-seq cl-start)) | |
442 (progn | |
443 (aset cl-seq cl-start cl-new) | |
444 (setq cl-count (1- cl-count)))) | |
445 (setq cl-start (1+ cl-start)))))) | |
446 cl-seq)) | |
447 | |
448 (defun nsubstitute-if (cl-new cl-pred cl-list &rest cl-keys) | |
449 "Substitute NEW for all items satisfying PREDICATE in SEQ. | |
450 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
451 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
452 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
453 (apply 'nsubstitute cl-new nil cl-list :if cl-pred cl-keys)) |
4355 | 454 |
455 (defun nsubstitute-if-not (cl-new cl-pred cl-list &rest cl-keys) | |
456 "Substitute NEW for all items not satisfying PREDICATE in SEQ. | |
457 This is a destructive function; it reuses the storage of SEQ whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
458 \nKeywords supported: :key :count :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
459 \n(fn NEW PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
460 (apply 'nsubstitute cl-new nil cl-list :if-not cl-pred cl-keys)) |
4355 | 461 |
462 (defun find (cl-item cl-seq &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
463 "Find the first occurrence of ITEM in SEQ. |
4355 | 464 Return the matching ITEM, or nil if not found. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
465 \nKeywords supported: :test :test-not :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
466 \n(fn ITEM SEQ [KEYWORD VALUE]...)" |
4355 | 467 (let ((cl-pos (apply 'position cl-item cl-seq cl-keys))) |
468 (and cl-pos (elt cl-seq cl-pos)))) | |
469 | |
470 (defun find-if (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
471 "Find the first item satisfying PREDICATE in SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
472 Return the matching item, or nil if not found. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
473 \nKeywords supported: :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
474 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
475 (apply 'find nil cl-list :if cl-pred cl-keys)) |
4355 | 476 |
477 (defun find-if-not (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
478 "Find the first item not satisfying PREDICATE in SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
479 Return the matching item, or nil if not found. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
480 \nKeywords supported: :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
481 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
482 (apply 'find nil cl-list :if-not cl-pred cl-keys)) |
4355 | 483 |
484 (defun position (cl-item cl-seq &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
485 "Find the first occurrence of ITEM in SEQ. |
4355 | 486 Return the index of the matching item, or nil if not found. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
487 \nKeywords supported: :test :test-not :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
488 \n(fn ITEM SEQ [KEYWORD VALUE]...)" |
4355 | 489 (cl-parsing-keywords (:test :test-not :key :if :if-not |
490 (:start 0) :end :from-end) () | |
491 (cl-position cl-item cl-seq cl-start cl-end cl-from-end))) | |
492 | |
493 (defun cl-position (cl-item cl-seq cl-start &optional cl-end cl-from-end) | |
494 (if (listp cl-seq) | |
495 (let ((cl-p (nthcdr cl-start cl-seq))) | |
496 (or cl-end (setq cl-end 8000000)) | |
497 (let ((cl-res nil)) | |
498 (while (and cl-p (< cl-start cl-end) (or (not cl-res) cl-from-end)) | |
499 (if (cl-check-test cl-item (car cl-p)) | |
500 (setq cl-res cl-start)) | |
501 (setq cl-p (cdr cl-p) cl-start (1+ cl-start))) | |
502 cl-res)) | |
503 (or cl-end (setq cl-end (length cl-seq))) | |
504 (if cl-from-end | |
505 (progn | |
506 (while (and (>= (setq cl-end (1- cl-end)) cl-start) | |
507 (not (cl-check-test cl-item (aref cl-seq cl-end))))) | |
508 (and (>= cl-end cl-start) cl-end)) | |
509 (while (and (< cl-start cl-end) | |
510 (not (cl-check-test cl-item (aref cl-seq cl-start)))) | |
511 (setq cl-start (1+ cl-start))) | |
512 (and (< cl-start cl-end) cl-start)))) | |
513 | |
514 (defun position-if (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
515 "Find the first item satisfying PREDICATE in SEQ. |
4355 | 516 Return the index of the matching item, or nil if not found. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
517 \nKeywords supported: :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
518 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
519 (apply 'position nil cl-list :if cl-pred cl-keys)) |
4355 | 520 |
521 (defun position-if-not (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
522 "Find the first item not satisfying PREDICATE in SEQ. |
4355 | 523 Return the index of the matching item, or nil if not found. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
524 \nKeywords supported: :key :start :end :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
525 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
526 (apply 'position nil cl-list :if-not cl-pred cl-keys)) |
4355 | 527 |
528 (defun count (cl-item cl-seq &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
529 "Count the number of occurrences of ITEM in SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
530 \nKeywords supported: :test :test-not :key :start :end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
531 \n(fn ITEM SEQ [KEYWORD VALUE]...)" |
4355 | 532 (cl-parsing-keywords (:test :test-not :key :if :if-not (:start 0) :end) () |
533 (let ((cl-count 0) cl-x) | |
534 (or cl-end (setq cl-end (length cl-seq))) | |
535 (if (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq))) | |
536 (while (< cl-start cl-end) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
537 (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start))) |
4355 | 538 (if (cl-check-test cl-item cl-x) (setq cl-count (1+ cl-count))) |
539 (setq cl-start (1+ cl-start))) | |
540 cl-count))) | |
541 | |
542 (defun count-if (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
543 "Count the number of items satisfying PREDICATE in SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
544 \nKeywords supported: :key :start :end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
545 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
546 (apply 'count nil cl-list :if cl-pred cl-keys)) |
4355 | 547 |
548 (defun count-if-not (cl-pred cl-list &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
549 "Count the number of items not satisfying PREDICATE in SEQ. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
550 \nKeywords supported: :key :start :end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
551 \n(fn PREDICATE SEQ [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
552 (apply 'count nil cl-list :if-not cl-pred cl-keys)) |
4355 | 553 |
554 (defun mismatch (cl-seq1 cl-seq2 &rest cl-keys) | |
555 "Compare SEQ1 with SEQ2, return index of first mismatching element. | |
556 Return nil if the sequences match. If one sequence is a prefix of the | |
42964 | 557 other, the return value indicates the end of the shorter sequence. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
558 \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
559 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" |
4355 | 560 (cl-parsing-keywords (:test :test-not :key :from-end |
561 (:start1 0) :end1 (:start2 0) :end2) () | |
562 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
563 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
564 (if cl-from-end | |
565 (progn | |
566 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
567 (cl-check-match (elt cl-seq1 (1- cl-end1)) | |
568 (elt cl-seq2 (1- cl-end2)))) | |
569 (setq cl-end1 (1- cl-end1) cl-end2 (1- cl-end2))) | |
570 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
571 (1- cl-end1))) | |
572 (let ((cl-p1 (and (listp cl-seq1) (nthcdr cl-start1 cl-seq1))) | |
573 (cl-p2 (and (listp cl-seq2) (nthcdr cl-start2 cl-seq2)))) | |
574 (while (and (< cl-start1 cl-end1) (< cl-start2 cl-end2) | |
575 (cl-check-match (if cl-p1 (car cl-p1) | |
576 (aref cl-seq1 cl-start1)) | |
577 (if cl-p2 (car cl-p2) | |
578 (aref cl-seq2 cl-start2)))) | |
579 (setq cl-p1 (cdr cl-p1) cl-p2 (cdr cl-p2) | |
580 cl-start1 (1+ cl-start1) cl-start2 (1+ cl-start2))) | |
581 (and (or (< cl-start1 cl-end1) (< cl-start2 cl-end2)) | |
582 cl-start1))))) | |
583 | |
584 (defun search (cl-seq1 cl-seq2 &rest cl-keys) | |
585 "Search for SEQ1 as a subsequence of SEQ2. | |
586 Return the index of the leftmost element of the first match found; | |
587 return nil if there are no matches. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
588 \nKeywords supported: :test :test-not :key :start1 :end1 :start2 :end2 :from-end |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
589 \n(fn SEQ1 SEQ2 [KEYWORD VALUE]...)" |
4355 | 590 (cl-parsing-keywords (:test :test-not :key :from-end |
591 (:start1 0) :end1 (:start2 0) :end2) () | |
592 (or cl-end1 (setq cl-end1 (length cl-seq1))) | |
593 (or cl-end2 (setq cl-end2 (length cl-seq2))) | |
594 (if (>= cl-start1 cl-end1) | |
595 (if cl-from-end cl-end2 cl-start2) | |
596 (let* ((cl-len (- cl-end1 cl-start1)) | |
597 (cl-first (cl-check-key (elt cl-seq1 cl-start1))) | |
598 (cl-if nil) cl-pos) | |
599 (setq cl-end2 (- cl-end2 (1- cl-len))) | |
600 (while (and (< cl-start2 cl-end2) | |
601 (setq cl-pos (cl-position cl-first cl-seq2 | |
602 cl-start2 cl-end2 cl-from-end)) | |
603 (apply 'mismatch cl-seq1 cl-seq2 | |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
604 :start1 (1+ cl-start1) :end1 cl-end1 |
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
605 :start2 (1+ cl-pos) :end2 (+ cl-pos cl-len) |
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
606 :from-end nil cl-keys)) |
4355 | 607 (if cl-from-end (setq cl-end2 cl-pos) (setq cl-start2 (1+ cl-pos)))) |
608 (and (< cl-start2 cl-end2) cl-pos))))) | |
609 | |
610 (defun sort* (cl-seq cl-pred &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
611 "Sort the argument SEQ according to PREDICATE. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
612 This is a destructive function; it reuses the storage of SEQ if possible. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
613 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
614 \n(fn SEQ PREDICATE [KEYWORD VALUE]...)" |
4355 | 615 (if (nlistp cl-seq) |
616 (replace cl-seq (apply 'sort* (append cl-seq nil) cl-pred cl-keys)) | |
617 (cl-parsing-keywords (:key) () | |
618 (if (memq cl-key '(nil identity)) | |
619 (sort cl-seq cl-pred) | |
620 (sort cl-seq (function (lambda (cl-x cl-y) | |
621 (funcall cl-pred (funcall cl-key cl-x) | |
622 (funcall cl-key cl-y))))))))) | |
623 | |
624 (defun stable-sort (cl-seq cl-pred &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
625 "Sort the argument SEQ stably according to PREDICATE. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
626 This is a destructive function; it reuses the storage of SEQ if possible. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
627 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
628 \n(fn SEQ PREDICATE [KEYWORD VALUE]...)" |
4355 | 629 (apply 'sort* cl-seq cl-pred cl-keys)) |
630 | |
631 (defun merge (cl-type cl-seq1 cl-seq2 cl-pred &rest cl-keys) | |
632 "Destructively merge the two sequences to produce a new sequence. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
633 TYPE is the sequence type to return, SEQ1 and SEQ2 are the two argument |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
634 sequences, and PREDICATE is a `less-than' predicate on the elements. |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
635 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
636 \n(fn TYPE SEQ1 SEQ2 PREDICATE [KEYWORD VALUE]...)" |
4355 | 637 (or (listp cl-seq1) (setq cl-seq1 (append cl-seq1 nil))) |
638 (or (listp cl-seq2) (setq cl-seq2 (append cl-seq2 nil))) | |
639 (cl-parsing-keywords (:key) () | |
640 (let ((cl-res nil)) | |
641 (while (and cl-seq1 cl-seq2) | |
642 (if (funcall cl-pred (cl-check-key (car cl-seq2)) | |
643 (cl-check-key (car cl-seq1))) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
644 (push (pop cl-seq2) cl-res) |
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
645 (push (pop cl-seq1) cl-res))) |
4355 | 646 (coerce (nconc (nreverse cl-res) cl-seq1 cl-seq2) cl-type)))) |
647 | |
648 ;;; See compiler macro in cl-macs.el | |
649 (defun member* (cl-item cl-list &rest cl-keys) | |
650 "Find the first occurrence of ITEM in LIST. | |
651 Return the sublist of LIST whose car is ITEM. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
652 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
653 \n(fn ITEM LIST [KEYWORD VALUE]...)" |
4355 | 654 (if cl-keys |
655 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
656 (while (and cl-list (not (cl-check-test cl-item (car cl-list)))) | |
657 (setq cl-list (cdr cl-list))) | |
658 cl-list) | |
659 (if (and (numberp cl-item) (not (integerp cl-item))) | |
660 (member cl-item cl-list) | |
661 (memq cl-item cl-list)))) | |
662 | |
663 (defun member-if (cl-pred cl-list &rest cl-keys) | |
664 "Find the first item satisfying PREDICATE in LIST. | |
665 Return the sublist of LIST whose car matches. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
666 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
667 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
668 (apply 'member* nil cl-list :if cl-pred cl-keys)) |
4355 | 669 |
670 (defun member-if-not (cl-pred cl-list &rest cl-keys) | |
671 "Find the first item not satisfying PREDICATE in LIST. | |
672 Return the sublist of LIST whose car matches. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
673 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
674 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
675 (apply 'member* nil cl-list :if-not cl-pred cl-keys)) |
4355 | 676 |
677 (defun cl-adjoin (cl-item cl-list &rest cl-keys) | |
678 (if (cl-parsing-keywords (:key) t | |
679 (apply 'member* (cl-check-key cl-item) cl-list cl-keys)) | |
680 cl-list | |
681 (cons cl-item cl-list))) | |
682 | |
683 ;;; See compiler macro in cl-macs.el | |
684 (defun assoc* (cl-item cl-alist &rest cl-keys) | |
685 "Find the first item whose car matches ITEM in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
686 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
687 \n(fn ITEM LIST [KEYWORD VALUE]...)" |
4355 | 688 (if cl-keys |
689 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
690 (while (and cl-alist | |
691 (or (not (consp (car cl-alist))) | |
692 (not (cl-check-test cl-item (car (car cl-alist)))))) | |
693 (setq cl-alist (cdr cl-alist))) | |
694 (and cl-alist (car cl-alist))) | |
695 (if (and (numberp cl-item) (not (integerp cl-item))) | |
696 (assoc cl-item cl-alist) | |
697 (assq cl-item cl-alist)))) | |
698 | |
699 (defun assoc-if (cl-pred cl-list &rest cl-keys) | |
700 "Find the first item whose car satisfies PREDICATE in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
701 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
702 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
703 (apply 'assoc* nil cl-list :if cl-pred cl-keys)) |
4355 | 704 |
705 (defun assoc-if-not (cl-pred cl-list &rest cl-keys) | |
706 "Find the first item whose car does not satisfy PREDICATE in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
707 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
708 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
709 (apply 'assoc* nil cl-list :if-not cl-pred cl-keys)) |
4355 | 710 |
711 (defun rassoc* (cl-item cl-alist &rest cl-keys) | |
712 "Find the first item whose cdr matches ITEM in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
713 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
714 \n(fn ITEM LIST [KEYWORD VALUE]...)" |
4355 | 715 (if (or cl-keys (numberp cl-item)) |
716 (cl-parsing-keywords (:test :test-not :key :if :if-not) () | |
717 (while (and cl-alist | |
718 (or (not (consp (car cl-alist))) | |
719 (not (cl-check-test cl-item (cdr (car cl-alist)))))) | |
720 (setq cl-alist (cdr cl-alist))) | |
721 (and cl-alist (car cl-alist))) | |
722 (rassq cl-item cl-alist))) | |
723 | |
724 (defun rassoc-if (cl-pred cl-list &rest cl-keys) | |
725 "Find the first item whose cdr satisfies PREDICATE in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
726 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
727 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
728 (apply 'rassoc* nil cl-list :if cl-pred cl-keys)) |
4355 | 729 |
730 (defun rassoc-if-not (cl-pred cl-list &rest cl-keys) | |
731 "Find the first item whose cdr does not satisfy PREDICATE in LIST. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
732 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
733 \n(fn PREDICATE LIST [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
734 (apply 'rassoc* nil cl-list :if-not cl-pred cl-keys)) |
4355 | 735 |
736 (defun union (cl-list1 cl-list2 &rest cl-keys) | |
737 "Combine LIST1 and LIST2 using a set-union operation. | |
738 The result list contains all items that appear in either LIST1 or LIST2. | |
739 This is a non-destructive function; it makes a copy of the data if necessary | |
740 to avoid corrupting the original LIST1 and LIST2. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
741 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
742 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 743 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
744 ((equal cl-list1 cl-list2) cl-list1) | |
745 (t | |
746 (or (>= (length cl-list1) (length cl-list2)) | |
747 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
748 (while cl-list2 | |
749 (if (or cl-keys (numberp (car cl-list2))) | |
750 (setq cl-list1 (apply 'adjoin (car cl-list2) cl-list1 cl-keys)) | |
751 (or (memq (car cl-list2) cl-list1) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
752 (push (car cl-list2) cl-list1))) |
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
753 (pop cl-list2)) |
4355 | 754 cl-list1))) |
755 | |
756 (defun nunion (cl-list1 cl-list2 &rest cl-keys) | |
757 "Combine LIST1 and LIST2 using a set-union operation. | |
758 The result list contains all items that appear in either LIST1 or LIST2. | |
759 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
760 whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
761 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
762 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 763 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
764 (t (apply 'union cl-list1 cl-list2 cl-keys)))) | |
765 | |
766 (defun intersection (cl-list1 cl-list2 &rest cl-keys) | |
767 "Combine LIST1 and LIST2 using a set-intersection operation. | |
768 The result list contains all items that appear in both LIST1 and LIST2. | |
769 This is a non-destructive function; it makes a copy of the data if necessary | |
770 to avoid corrupting the original LIST1 and LIST2. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
771 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
772 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 773 (and cl-list1 cl-list2 |
774 (if (equal cl-list1 cl-list2) cl-list1 | |
775 (cl-parsing-keywords (:key) (:test :test-not) | |
776 (let ((cl-res nil)) | |
777 (or (>= (length cl-list1) (length cl-list2)) | |
778 (setq cl-list1 (prog1 cl-list2 (setq cl-list2 cl-list1)))) | |
779 (while cl-list2 | |
780 (if (if (or cl-keys (numberp (car cl-list2))) | |
781 (apply 'member* (cl-check-key (car cl-list2)) | |
782 cl-list1 cl-keys) | |
783 (memq (car cl-list2) cl-list1)) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
784 (push (car cl-list2) cl-res)) |
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
785 (pop cl-list2)) |
4355 | 786 cl-res))))) |
787 | |
788 (defun nintersection (cl-list1 cl-list2 &rest cl-keys) | |
789 "Combine LIST1 and LIST2 using a set-intersection operation. | |
790 The result list contains all items that appear in both LIST1 and LIST2. | |
791 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
792 whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
793 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
794 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 795 (and cl-list1 cl-list2 (apply 'intersection cl-list1 cl-list2 cl-keys))) |
796 | |
797 (defun set-difference (cl-list1 cl-list2 &rest cl-keys) | |
798 "Combine LIST1 and LIST2 using a set-difference operation. | |
799 The result list contains all items that appear in LIST1 but not LIST2. | |
800 This is a non-destructive function; it makes a copy of the data if necessary | |
801 to avoid corrupting the original LIST1 and LIST2. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
802 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
803 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 804 (if (or (null cl-list1) (null cl-list2)) cl-list1 |
805 (cl-parsing-keywords (:key) (:test :test-not) | |
806 (let ((cl-res nil)) | |
807 (while cl-list1 | |
808 (or (if (or cl-keys (numberp (car cl-list1))) | |
809 (apply 'member* (cl-check-key (car cl-list1)) | |
810 cl-list2 cl-keys) | |
811 (memq (car cl-list1) cl-list2)) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
812 (push (car cl-list1) cl-res)) |
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
813 (pop cl-list1)) |
4355 | 814 cl-res)))) |
815 | |
816 (defun nset-difference (cl-list1 cl-list2 &rest cl-keys) | |
817 "Combine LIST1 and LIST2 using a set-difference operation. | |
818 The result list contains all items that appear in LIST1 but not LIST2. | |
819 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
820 whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
821 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
822 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 823 (if (or (null cl-list1) (null cl-list2)) cl-list1 |
824 (apply 'set-difference cl-list1 cl-list2 cl-keys))) | |
825 | |
826 (defun set-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
827 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
828 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
829 This is a non-destructive function; it makes a copy of the data if necessary | |
830 to avoid corrupting the original LIST1 and LIST2. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
831 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
832 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 833 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
834 ((equal cl-list1 cl-list2) nil) | |
835 (t (append (apply 'set-difference cl-list1 cl-list2 cl-keys) | |
836 (apply 'set-difference cl-list2 cl-list1 cl-keys))))) | |
837 | |
838 (defun nset-exclusive-or (cl-list1 cl-list2 &rest cl-keys) | |
839 "Combine LIST1 and LIST2 using a set-exclusive-or operation. | |
840 The result list contains all items that appear in exactly one of LIST1, LIST2. | |
841 This is a destructive function; it reuses the storage of LIST1 and LIST2 | |
842 whenever possible. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
843 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
844 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 845 (cond ((null cl-list1) cl-list2) ((null cl-list2) cl-list1) |
846 ((equal cl-list1 cl-list2) nil) | |
847 (t (nconc (apply 'nset-difference cl-list1 cl-list2 cl-keys) | |
848 (apply 'nset-difference cl-list2 cl-list1 cl-keys))))) | |
849 | |
850 (defun subsetp (cl-list1 cl-list2 &rest cl-keys) | |
62426
c5a3c48f99b5
(subsetp, tree-equal): Doc fixes.
Juanma Barranquero <lekktu@gmail.com>
parents:
52401
diff
changeset
|
851 "Return true if LIST1 is a subset of LIST2. |
4355 | 852 I.e., if every element of LIST1 also appears in LIST2. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
853 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
854 \n(fn LIST1 LIST2 [KEYWORD VALUE]...)" |
4355 | 855 (cond ((null cl-list1) t) ((null cl-list2) nil) |
856 ((equal cl-list1 cl-list2) t) | |
857 (t (cl-parsing-keywords (:key) (:test :test-not) | |
858 (while (and cl-list1 | |
859 (apply 'member* (cl-check-key (car cl-list1)) | |
860 cl-list2 cl-keys)) | |
47661
025c490d435b
(cl-push, cl-pop): Remove.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
42964
diff
changeset
|
861 (pop cl-list1)) |
4355 | 862 (null cl-list1))))) |
863 | |
864 (defun subst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
865 "Substitute NEW for elements matching PREDICATE in TREE (non-destructively). | |
866 Return a copy of TREE with all matching elements replaced by NEW. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
867 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
868 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
869 (apply 'sublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
4355 | 870 |
871 (defun subst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
872 "Substitute NEW for elts not matching PREDICATE in TREE (non-destructively). | |
873 Return a copy of TREE with all non-matching elements replaced by NEW. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
874 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
875 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
876 (apply 'sublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
4355 | 877 |
878 (defun nsubst (cl-new cl-old cl-tree &rest cl-keys) | |
879 "Substitute NEW for OLD everywhere in TREE (destructively). | |
880 Any element of TREE which is `eql' to OLD is changed to NEW (via a call | |
881 to `setcar'). | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
882 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
883 \n(fn NEW OLD TREE [KEYWORD VALUE]...)" |
4355 | 884 (apply 'nsublis (list (cons cl-old cl-new)) cl-tree cl-keys)) |
885 | |
886 (defun nsubst-if (cl-new cl-pred cl-tree &rest cl-keys) | |
887 "Substitute NEW for elements matching PREDICATE in TREE (destructively). | |
888 Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
889 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
890 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
891 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if cl-pred cl-keys)) |
4355 | 892 |
893 (defun nsubst-if-not (cl-new cl-pred cl-tree &rest cl-keys) | |
894 "Substitute NEW for elements not matching PREDICATE in TREE (destructively). | |
895 Any element of TREE which matches is changed to NEW (via a call to `setcar'). | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
896 \nKeywords supported: :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
897 \n(fn NEW PREDICATE TREE [KEYWORD VALUE]...)" |
28824
add63b27c709
Doc fixes; mainly avoid duplicating arg
Dave Love <fx@gnu.org>
parents:
16057
diff
changeset
|
898 (apply 'nsublis (list (cons nil cl-new)) cl-tree :if-not cl-pred cl-keys)) |
4355 | 899 |
900 (defun sublis (cl-alist cl-tree &rest cl-keys) | |
901 "Perform substitutions indicated by ALIST in TREE (non-destructively). | |
902 Return a copy of TREE with all matching elements replaced. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
903 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
904 \n(fn ALIST TREE [KEYWORD VALUE]...)" |
4355 | 905 (cl-parsing-keywords (:test :test-not :key :if :if-not) () |
906 (cl-sublis-rec cl-tree))) | |
907 | |
908 (defvar cl-alist) | |
909 (defun cl-sublis-rec (cl-tree) ; uses cl-alist/key/test*/if* | |
910 (let ((cl-temp (cl-check-key cl-tree)) (cl-p cl-alist)) | |
911 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
912 (setq cl-p (cdr cl-p))) | |
913 (if cl-p (cdr (car cl-p)) | |
914 (if (consp cl-tree) | |
915 (let ((cl-a (cl-sublis-rec (car cl-tree))) | |
916 (cl-d (cl-sublis-rec (cdr cl-tree)))) | |
917 (if (and (eq cl-a (car cl-tree)) (eq cl-d (cdr cl-tree))) | |
918 cl-tree | |
919 (cons cl-a cl-d))) | |
920 cl-tree)))) | |
921 | |
922 (defun nsublis (cl-alist cl-tree &rest cl-keys) | |
923 "Perform substitutions indicated by ALIST in TREE (destructively). | |
924 Any matching element of TREE is changed via a call to `setcar'. | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
925 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
926 \n(fn ALIST TREE [KEYWORD VALUE]...)" |
4355 | 927 (cl-parsing-keywords (:test :test-not :key :if :if-not) () |
928 (let ((cl-hold (list cl-tree))) | |
929 (cl-nsublis-rec cl-hold) | |
930 (car cl-hold)))) | |
931 | |
932 (defun cl-nsublis-rec (cl-tree) ; uses cl-alist/temp/p/key/test*/if* | |
933 (while (consp cl-tree) | |
934 (let ((cl-temp (cl-check-key (car cl-tree))) (cl-p cl-alist)) | |
935 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
936 (setq cl-p (cdr cl-p))) | |
937 (if cl-p (setcar cl-tree (cdr (car cl-p))) | |
938 (if (consp (car cl-tree)) (cl-nsublis-rec (car cl-tree)))) | |
939 (setq cl-temp (cl-check-key (cdr cl-tree)) cl-p cl-alist) | |
940 (while (and cl-p (not (cl-check-test-nokey (car (car cl-p)) cl-temp))) | |
941 (setq cl-p (cdr cl-p))) | |
942 (if cl-p | |
943 (progn (setcdr cl-tree (cdr (car cl-p))) (setq cl-tree nil)) | |
944 (setq cl-tree (cdr cl-tree)))))) | |
945 | |
946 (defun tree-equal (cl-x cl-y &rest cl-keys) | |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
947 "Return t if trees TREE1 and TREE2 have `eql' leaves. |
4355 | 948 Atoms are compared by `eql'; cons cells are compared recursively. |
62555
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
949 \nKeywords supported: :test :test-not :key |
e74fa19e333d
(reduce, fill, replace, remove*, remove-if, remove-if-not, delete*, delete-if,
Juanma Barranquero <lekktu@gmail.com>
parents:
62426
diff
changeset
|
950 \n(fn TREE1 TREE2 [KEYWORD VALUE]...)" |
4355 | 951 (cl-parsing-keywords (:test :test-not :key) () |
952 (cl-tree-equal-rec cl-x cl-y))) | |
953 | |
954 (defun cl-tree-equal-rec (cl-x cl-y) | |
955 (while (and (consp cl-x) (consp cl-y) | |
956 (cl-tree-equal-rec (car cl-x) (car cl-y))) | |
957 (setq cl-x (cdr cl-x) cl-y (cdr cl-y))) | |
958 (and (not (consp cl-x)) (not (consp cl-y)) (cl-check-match cl-x cl-y))) | |
959 | |
960 | |
961 (run-hooks 'cl-seq-load-hook) | |
962 | |
52401 | 963 ;;; arch-tag: ec1cc072-9006-4225-b6ba-d6b07ed1710c |
4355 | 964 ;;; cl-seq.el ends here |