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