lisp/gnus/{registry.el,gnus-registry.el}: Use slot names in references to object...
[emacs.git] / lisp / emacs-lisp / seq.el
blob59b91408d0941f0d8dc554a96cbf82eb96e34ded
1 ;;; seq.el --- Sequence manipulation functions -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: sequences
7 ;; Version: 1.3
8 ;; Package: seq
10 ;; Maintainer: emacs-devel@gnu.org
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; Sequence-manipulation functions that complement basic functions
30 ;; provided by subr.el.
32 ;; All functions are prefixed with "seq-".
34 ;; All provided functions work on lists, strings and vectors.
36 ;; Functions taking a predicate or a function iterating over the
37 ;; sequence as argument take the function as their first argument and
38 ;; the sequence as their second argument. All other functions take
39 ;; the sequence as their first argument.
41 ;; All functions are tested in test/automated/seq-tests.el
43 ;;; Code:
45 (defmacro seq-doseq (spec &rest body)
46 "Loop over a sequence.
47 Similar to `dolist' but can be applied lists, strings and vectors.
49 Evaluate BODY with VAR bound to each element of SEQ, in turn.
50 Then evaluate RESULT to get return value, default nil.
52 \(fn (VAR SEQ [RESULT]) BODY...)"
53 (declare (indent 1) (debug ((symbolp form &optional form) body)))
54 (let ((is-list (make-symbol "is-list"))
55 (seq (make-symbol "seq"))
56 (index (make-symbol "index")))
57 `(let* ((,seq ,(cadr spec))
58 (,is-list (listp ,seq))
59 (,index (if ,is-list ,seq 0)))
60 (while (if ,is-list
61 (consp ,index)
62 (< ,index (seq-length ,seq)))
63 (let ((,(car spec) (if ,is-list
64 (car ,index)
65 (seq-elt ,seq ,index))))
66 ,@body
67 (setq ,index (if ,is-list
68 (cdr ,index)
69 (+ ,index 1)))))
70 ,@(if (cddr spec)
71 `((setq ,(car spec) nil) ,@(cddr spec))))))
73 (defun seq-drop (seq n)
74 "Return a subsequence of SEQ without its first N elements.
75 The result is a sequence of the same type as SEQ.
77 If N is a negative integer or zero, SEQ is returned."
78 (if (<= n 0)
79 seq
80 (if (listp seq)
81 (seq--drop-list seq n)
82 (let ((length (seq-length seq)))
83 (seq-subseq seq (min n length) length)))))
85 (defun seq-take (seq n)
86 "Return a subsequence of SEQ with its first N elements.
87 The result is a sequence of the same type as SEQ.
89 If N is a negative integer or zero, an empty sequence is
90 returned."
91 (if (listp seq)
92 (seq--take-list seq n)
93 (seq-subseq seq 0 (min (max n 0) (seq-length seq)))))
95 (defun seq-drop-while (pred seq)
96 "Return a sequence from the first element for which (PRED element) is nil in SEQ.
97 The result is a sequence of the same type as SEQ."
98 (if (listp seq)
99 (seq--drop-while-list pred seq)
100 (seq-drop seq (seq--count-successive pred seq))))
102 (defun seq-take-while (pred seq)
103 "Return the successive elements for which (PRED element) is non-nil in SEQ.
104 The result is a sequence of the same type as SEQ."
105 (if (listp seq)
106 (seq--take-while-list pred seq)
107 (seq-take seq (seq--count-successive pred seq))))
109 (defun seq-filter (pred seq)
110 "Return a list of all the elements for which (PRED element) is non-nil in SEQ."
111 (let ((exclude (make-symbol "exclude")))
112 (delq exclude (seq-map (lambda (elt)
113 (if (funcall pred elt)
115 exclude))
116 seq))))
118 (defun seq-remove (pred seq)
119 "Return a list of all the elements for which (PRED element) is nil in SEQ."
120 (seq-filter (lambda (elt) (not (funcall pred elt)))
121 seq))
123 (defun seq-reduce (function seq initial-value)
124 "Reduce the function FUNCTION across SEQ, starting with INITIAL-VALUE.
126 Return the result of calling FUNCTION with INITIAL-VALUE and the
127 first element of SEQ, then calling FUNCTION with that result and
128 the second element of SEQ, then with that result and the third
129 element of SEQ, etc.
131 If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called."
132 (if (seq-empty-p seq)
133 initial-value
134 (let ((acc initial-value))
135 (seq-doseq (elt seq)
136 (setq acc (funcall function acc elt)))
137 acc)))
139 (defun seq-some-p (pred seq)
140 "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise."
141 (catch 'seq--break
142 (seq-doseq (elt seq)
143 (when (funcall pred elt)
144 (throw 'seq--break elt)))
145 nil))
147 (defun seq-every-p (pred seq)
148 "Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ."
149 (catch 'seq--break
150 (seq-doseq (elt seq)
151 (or (funcall pred elt)
152 (throw 'seq--break nil)))
155 (defun seq-count (pred seq)
156 "Return the number of elements for which (PRED element) is non-nil in SEQ."
157 (let ((count 0))
158 (seq-doseq (elt seq)
159 (when (funcall pred elt)
160 (setq count (+ 1 count))))
161 count))
163 (defun seq-empty-p (seq)
164 "Return non-nil if the sequence SEQ is empty, nil otherwise."
165 (if (listp seq)
166 (null seq)
167 (= 0 (seq-length seq))))
169 (defun seq-sort (pred seq)
170 "Return a sorted sequence comparing using PRED the elements of SEQ.
171 The result is a sequence of the same type as SEQ."
172 (if (listp seq)
173 (sort (seq-copy seq) pred)
174 (let ((result (seq-sort pred (append seq nil))))
175 (seq-into result (type-of seq)))))
177 (defun seq-contains-p (seq elt &optional testfn)
178 "Return the first element in SEQ that equals to ELT.
179 Equality is defined by TESTFN if non-nil or by `equal' if nil."
180 (seq-some-p (lambda (e)
181 (funcall (or testfn #'equal) elt e))
182 seq))
184 (defun seq-uniq (seq &optional testfn)
185 "Return a list of the elements of SEQ with duplicates removed.
186 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
187 (let ((result '()))
188 (seq-doseq (elt seq)
189 (unless (seq-contains-p result elt testfn)
190 (setq result (cons elt result))))
191 (nreverse result)))
193 (defun seq-subseq (seq start &optional end)
194 "Return the subsequence of SEQ from START to END.
195 If END is omitted, it defaults to the length of the sequence.
196 If START or END is negative, it counts from the end."
197 (cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
198 ((listp seq)
199 (let (len (errtext (format "Bad bounding indices: %s, %s" start end)))
200 (and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
201 (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq))))))
202 (when (> start 0)
203 (setq seq (nthcdr (1- start) seq))
204 (or seq (error "%s" errtext))
205 (setq seq (cdr seq)))
206 (if end
207 (let ((res nil))
208 (while (and (>= (setq end (1- end)) start) seq)
209 (push (pop seq) res))
210 (or (= (1+ end) start) (error "%s" errtext))
211 (nreverse res))
212 (seq-copy seq))))
213 (t (error "Unsupported sequence: %s" seq))))
215 (defun seq-concatenate (type &rest seqs)
216 "Concatenate, into a sequence of type TYPE, the sequences SEQS.
217 TYPE must be one of following symbols: vector, string or list.
219 \n(fn TYPE SEQUENCE...)"
220 (pcase type
221 (`vector (apply #'vconcat seqs))
222 (`string (apply #'concat seqs))
223 (`list (apply #'append (append seqs '(nil))))
224 (t (error "Not a sequence type name: %s" type))))
226 (defun seq-mapcat (function seq &optional type)
227 "Concatenate the result of applying FUNCTION to each element of SEQ.
228 The result is a sequence of type TYPE, or a list if TYPE is nil."
229 (apply #'seq-concatenate (or type 'list)
230 (seq-map function seq)))
232 (defun seq-partition (seq n)
233 "Return a list of the elements of SEQ grouped into sub-sequences of length N.
234 The last sequence may contain less than N elements. If N is a
235 negative integer or 0, nil is returned."
236 (unless (< n 1)
237 (let ((result '()))
238 (while (not (seq-empty-p seq))
239 (push (seq-take seq n) result)
240 (setq seq (seq-drop seq n)))
241 (nreverse result))))
243 (defun seq-group-by (function seq)
244 "Apply FUNCTION to each element of SEQ.
245 Separate the elements of SEQ into an alist using the results as
246 keys. Keys are compared using `equal'."
247 (seq-reduce
248 (lambda (acc elt)
249 (let* ((key (funcall function elt))
250 (cell (assoc key acc)))
251 (if cell
252 (setcdr cell (push elt (cdr cell)))
253 (push (list key elt) acc))
254 acc))
255 (seq-reverse seq)
256 nil))
258 (defalias 'seq-reverse
259 (if (ignore-errors (reverse [1 2]))
260 #'reverse
261 (lambda (seq)
262 "Return the reversed copy of list, vector, or string SEQ.
263 See also the function `nreverse', which is used more often."
264 (let ((result '()))
265 (seq-map (lambda (elt) (push elt result))
266 seq)
267 (if (listp seq)
268 result
269 (seq-into result (type-of seq)))))))
271 (defun seq-into (seq type)
272 "Convert the sequence SEQ into a sequence of type TYPE.
273 TYPE can be one of the following symbols: vector, string or list."
274 (pcase type
275 (`vector (vconcat seq))
276 (`string (concat seq))
277 (`list (append seq nil))
278 (t (error "Not a sequence type name: %s" type))))
280 (defun seq--drop-list (list n)
281 "Return a list from LIST without its first N elements.
282 This is an optimization for lists in `seq-drop'."
283 (while (and list (> n 0))
284 (setq list (cdr list)
285 n (1- n)))
286 list)
288 (defun seq--take-list (list n)
289 "Return a list from LIST made of its first N elements.
290 This is an optimization for lists in `seq-take'."
291 (let ((result '()))
292 (while (and list (> n 0))
293 (setq n (1- n))
294 (push (pop list) result))
295 (nreverse result)))
297 (defun seq--drop-while-list (pred list)
298 "Return a list from the first element for which (PRED element) is nil in LIST.
299 This is an optimization for lists in `seq-drop-while'."
300 (while (and list (funcall pred (car list)))
301 (setq list (cdr list)))
302 list)
304 (defun seq--take-while-list (pred list)
305 "Return the successive elements for which (PRED element) is non-nil in LIST.
306 This is an optimization for lists in `seq-take-while'."
307 (let ((result '()))
308 (while (and list (funcall pred (car list)))
309 (push (pop list) result))
310 (nreverse result)))
312 (defun seq--count-successive (pred seq)
313 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
314 (let ((n 0)
315 (len (seq-length seq)))
316 (while (and (< n len)
317 (funcall pred (seq-elt seq n)))
318 (setq n (+ 1 n)))
321 (defalias 'seq-copy #'copy-sequence)
322 (defalias 'seq-elt #'elt)
323 (defalias 'seq-length #'length)
324 (defalias 'seq-do #'mapc)
325 (defalias 'seq-each #'seq-do)
326 (defalias 'seq-map #'mapcar)
328 (provide 'seq)
329 ;;; seq.el ends here