]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/seq.el
* lisp/emacs-lisp/map.el: Better docstring for the map pcase macro.
[gnu-emacs] / lisp / emacs-lisp / seq.el
1 ;;; seq.el --- Sequence manipulation functions -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
6 ;; Keywords: sequences
7 ;; Version: 1.4
8 ;; Package: seq
9
10 ;; Maintainer: emacs-devel@gnu.org
11
12 ;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;; Sequence-manipulation functions that complement basic functions
30 ;; provided by subr.el.
31 ;;
32 ;; All functions are prefixed with "seq-".
33 ;;
34 ;; All provided functions work on lists, strings and vectors.
35 ;;
36 ;; Functions taking a predicate or iterating over a sequence using a
37 ;; function 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.
40 ;;
41 ;; All functions are tested in test/automated/seq-tests.el
42
43 ;;; Code:
44
45 (defmacro seq-doseq (spec &rest body)
46 "Loop over a sequence.
47 Similar to `dolist' but can be applied lists, strings and vectors.
48
49 Evaluate BODY with VAR bound to each element of SEQ, in turn.
50 Then evaluate RESULT to get return value, default nil.
51
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))))))
72
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.
76
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)))))
84
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.
88
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)))))
94
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))))
101
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))))
108
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)
114 elt
115 exclude))
116 seq))))
117
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))
122
123 (defun seq-reduce (function seq initial-value)
124 "Reduce the function FUNCTION across SEQ, starting with INITIAL-VALUE.
125
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.
130
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)))
138
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))
146
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)))
153 t))
154
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))
162
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))))
168
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)))))
176
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))
183
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)))
192
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))))
214
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.
218
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))))
225
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)))
231
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))))
242
243 (defun seq-intersection (seq1 seq2 &optional testfn)
244 "Return a list of the elements that appear in both SEQ1 and SEQ2.
245 Equality is defined by TESTFN if non-nil or by `equal' if nil."
246 (seq-reduce (lambda (acc elt)
247 (if (seq-contains-p seq2 elt testfn)
248 (cons elt acc)
249 acc))
250 (seq-reverse seq1)
251 '()))
252
253 (defun seq-difference (seq1 seq2 &optional testfn)
254 "Return a list of th elements that appear in SEQ1 but not in SEQ2.
255 Equality is defined by TESTFN if non-nil or by `equal' if nil."
256 (seq-reduce (lambda (acc elt)
257 (if (not (seq-contains-p seq2 elt testfn))
258 (cons elt acc)
259 acc))
260 (seq-reverse seq1)
261 '()))
262
263 (defun seq-group-by (function seq)
264 "Apply FUNCTION to each element of SEQ.
265 Separate the elements of SEQ into an alist using the results as
266 keys. Keys are compared using `equal'."
267 (seq-reduce
268 (lambda (acc elt)
269 (let* ((key (funcall function elt))
270 (cell (assoc key acc)))
271 (if cell
272 (setcdr cell (push elt (cdr cell)))
273 (push (list key elt) acc))
274 acc))
275 (seq-reverse seq)
276 nil))
277
278 (defalias 'seq-reverse
279 (if (ignore-errors (reverse [1 2]))
280 #'reverse
281 (lambda (seq)
282 "Return the reversed copy of list, vector, or string SEQ.
283 See also the function `nreverse', which is used more often."
284 (let ((result '()))
285 (seq-map (lambda (elt) (push elt result))
286 seq)
287 (if (listp seq)
288 result
289 (seq-into result (type-of seq)))))))
290
291 (defun seq-into (seq type)
292 "Convert the sequence SEQ into a sequence of type TYPE.
293 TYPE can be one of the following symbols: vector, string or list."
294 (pcase type
295 (`vector (vconcat seq))
296 (`string (concat seq))
297 (`list (append seq nil))
298 (t (error "Not a sequence type name: %s" type))))
299
300 (defun seq--drop-list (list n)
301 "Return a list from LIST without its first N elements.
302 This is an optimization for lists in `seq-drop'."
303 (while (and list (> n 0))
304 (setq list (cdr list)
305 n (1- n)))
306 list)
307
308 (defun seq--take-list (list n)
309 "Return a list from LIST made of its first N elements.
310 This is an optimization for lists in `seq-take'."
311 (let ((result '()))
312 (while (and list (> n 0))
313 (setq n (1- n))
314 (push (pop list) result))
315 (nreverse result)))
316
317 (defun seq--drop-while-list (pred list)
318 "Return a list from the first element for which (PRED element) is nil in LIST.
319 This is an optimization for lists in `seq-drop-while'."
320 (while (and list (funcall pred (car list)))
321 (setq list (cdr list)))
322 list)
323
324 (defun seq--take-while-list (pred list)
325 "Return the successive elements for which (PRED element) is non-nil in LIST.
326 This is an optimization for lists in `seq-take-while'."
327 (let ((result '()))
328 (while (and list (funcall pred (car list)))
329 (push (pop list) result))
330 (nreverse result)))
331
332 (defun seq--count-successive (pred seq)
333 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
334 (let ((n 0)
335 (len (seq-length seq)))
336 (while (and (< n len)
337 (funcall pred (seq-elt seq n)))
338 (setq n (+ 1 n)))
339 n))
340
341 (defun seq--activate-font-lock-keywords ()
342 "Activate font-lock keywords for some symbols defined in seq."
343 (font-lock-add-keywords 'emacs-lisp-mode
344 '("\\<seq-doseq\\>")))
345
346 (defalias 'seq-copy #'copy-sequence)
347 (defalias 'seq-elt #'elt)
348 (defalias 'seq-length #'length)
349 (defalias 'seq-do #'mapc)
350 (defalias 'seq-each #'seq-do)
351 (defalias 'seq-map #'mapcar)
352
353 (add-to-list 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords)
354
355 (provide 'seq)
356 ;;; seq.el ends here