]> code.delx.au - gnu-emacs-elpa/blob - packages/seq/seq.el
Merge commit 'b114cf8a93224c85c51e95db52bf359131130476' from ace-window
[gnu-emacs-elpa] / packages / seq / 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.5
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 to lists, strings, and vectors.
48
49 Evaluate BODY with VAR bound to each element of SEQ, in turn.
50
51 \(fn (VAR SEQ) BODY...)"
52 (declare (indent 1) (debug ((symbolp form &optional form) body)))
53 (let ((length (make-symbol "length"))
54 (seq (make-symbol "seq"))
55 (index (make-symbol "index")))
56 `(let* ((,seq ,(cadr spec))
57 (,length (if (listp ,seq) nil (seq-length ,seq)))
58 (,index (if ,length 0 ,seq)))
59 (while (if ,length
60 (< ,index ,length)
61 (consp ,index))
62 (let ((,(car spec) (if ,length
63 (prog1 (seq-elt ,seq ,index)
64 (setq ,index (+ ,index 1)))
65 (pop ,index))))
66 ,@body)))))
67
68 (defun seq-drop (seq n)
69 "Return a subsequence of SEQ without its first N elements.
70 The result is a sequence of the same type as SEQ.
71
72 If N is a negative integer or zero, SEQ is returned."
73 (if (<= n 0)
74 seq
75 (if (listp seq)
76 (seq--drop-list seq n)
77 (let ((length (seq-length seq)))
78 (seq-subseq seq (min n length) length)))))
79
80 (defun seq-take (seq n)
81 "Return a subsequence of SEQ with its first N elements.
82 The result is a sequence of the same type as SEQ.
83
84 If N is a negative integer or zero, an empty sequence is
85 returned."
86 (if (listp seq)
87 (seq--take-list seq n)
88 (seq-subseq seq 0 (min (max n 0) (seq-length seq)))))
89
90 (defun seq-drop-while (pred seq)
91 "Return a sequence from the first element for which (PRED element) is nil in SEQ.
92 The result is a sequence of the same type as SEQ."
93 (if (listp seq)
94 (seq--drop-while-list pred seq)
95 (seq-drop seq (seq--count-successive pred seq))))
96
97 (defun seq-take-while (pred seq)
98 "Return the successive elements for which (PRED element) is non-nil in SEQ.
99 The result is a sequence of the same type as SEQ."
100 (if (listp seq)
101 (seq--take-while-list pred seq)
102 (seq-take seq (seq--count-successive pred seq))))
103
104 (defun seq-filter (pred seq)
105 "Return a list of all the elements for which (PRED element) is non-nil in SEQ."
106 (let ((exclude (make-symbol "exclude")))
107 (delq exclude (seq-map (lambda (elt)
108 (if (funcall pred elt)
109 elt
110 exclude))
111 seq))))
112
113 (defun seq-remove (pred seq)
114 "Return a list of all the elements for which (PRED element) is nil in SEQ."
115 (seq-filter (lambda (elt) (not (funcall pred elt)))
116 seq))
117
118 (defun seq-reduce (function seq initial-value)
119 "Reduce the function FUNCTION across SEQ, starting with INITIAL-VALUE.
120
121 Return the result of calling FUNCTION with INITIAL-VALUE and the
122 first element of SEQ, then calling FUNCTION with that result and
123 the second element of SEQ, then with that result and the third
124 element of SEQ, etc.
125
126 If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called."
127 (if (seq-empty-p seq)
128 initial-value
129 (let ((acc initial-value))
130 (seq-doseq (elt seq)
131 (setq acc (funcall function acc elt)))
132 acc)))
133
134 (defun seq-some-p (pred seq)
135 "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise."
136 (catch 'seq--break
137 (seq-doseq (elt seq)
138 (when (funcall pred elt)
139 (throw 'seq--break elt)))
140 nil))
141
142 (defun seq-every-p (pred seq)
143 "Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ."
144 (catch 'seq--break
145 (seq-doseq (elt seq)
146 (or (funcall pred elt)
147 (throw 'seq--break nil)))
148 t))
149
150 (defun seq-count (pred seq)
151 "Return the number of elements for which (PRED element) is non-nil in SEQ."
152 (let ((count 0))
153 (seq-doseq (elt seq)
154 (when (funcall pred elt)
155 (setq count (+ 1 count))))
156 count))
157
158 (defun seq-empty-p (seq)
159 "Return non-nil if the sequence SEQ is empty, nil otherwise."
160 (if (listp seq)
161 (null seq)
162 (= 0 (seq-length seq))))
163
164 (defun seq-sort (pred seq)
165 "Return a sorted sequence comparing using PRED the elements of SEQ.
166 The result is a sequence of the same type as SEQ."
167 (if (listp seq)
168 (sort (seq-copy seq) pred)
169 (let ((result (seq-sort pred (append seq nil))))
170 (seq-into result (type-of seq)))))
171
172 (defun seq-contains-p (seq elt &optional testfn)
173 "Return the first element in SEQ that equals to ELT.
174 Equality is defined by TESTFN if non-nil or by `equal' if nil."
175 (seq-some-p (lambda (e)
176 (funcall (or testfn #'equal) elt e))
177 seq))
178
179 (defun seq-uniq (seq &optional testfn)
180 "Return a list of the elements of SEQ with duplicates removed.
181 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
182 (let ((result '()))
183 (seq-doseq (elt seq)
184 (unless (seq-contains-p result elt testfn)
185 (setq result (cons elt result))))
186 (nreverse result)))
187
188 (defun seq-subseq (seq start &optional end)
189 "Return the subsequence of SEQ from START to END.
190 If END is omitted, it defaults to the length of the sequence.
191 If START or END is negative, it counts from the end."
192 (cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
193 ((listp seq)
194 (let (len (errtext (format "Bad bounding indices: %s, %s" start end)))
195 (and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
196 (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq))))))
197 (when (> start 0)
198 (setq seq (nthcdr (1- start) seq))
199 (or seq (error "%s" errtext))
200 (setq seq (cdr seq)))
201 (if end
202 (let ((res nil))
203 (while (and (>= (setq end (1- end)) start) seq)
204 (push (pop seq) res))
205 (or (= (1+ end) start) (error "%s" errtext))
206 (nreverse res))
207 (seq-copy seq))))
208 (t (error "Unsupported sequence: %s" seq))))
209
210 (defun seq-concatenate (type &rest seqs)
211 "Concatenate, into a sequence of type TYPE, the sequences SEQS.
212 TYPE must be one of following symbols: vector, string or list.
213
214 \n(fn TYPE SEQUENCE...)"
215 (pcase type
216 (`vector (apply #'vconcat seqs))
217 (`string (apply #'concat seqs))
218 (`list (apply #'append (append seqs '(nil))))
219 (t (error "Not a sequence type name: %S" type))))
220
221 (defun seq-mapcat (function seq &optional type)
222 "Concatenate the result of applying FUNCTION to each element of SEQ.
223 The result is a sequence of type TYPE, or a list if TYPE is nil."
224 (apply #'seq-concatenate (or type 'list)
225 (seq-map function seq)))
226
227 (defun seq-partition (seq n)
228 "Return a list of the elements of SEQ grouped into sub-sequences of length N.
229 The last sequence may contain less than N elements. If N is a
230 negative integer or 0, nil is returned."
231 (unless (< n 1)
232 (let ((result '()))
233 (while (not (seq-empty-p seq))
234 (push (seq-take seq n) result)
235 (setq seq (seq-drop seq n)))
236 (nreverse result))))
237
238 (defun seq-intersection (seq1 seq2 &optional testfn)
239 "Return a list of the elements that appear in both SEQ1 and SEQ2.
240 Equality is defined by TESTFN if non-nil or by `equal' if nil."
241 (seq-reduce (lambda (acc elt)
242 (if (seq-contains-p seq2 elt testfn)
243 (cons elt acc)
244 acc))
245 (seq-reverse seq1)
246 '()))
247
248 (defun seq-difference (seq1 seq2 &optional testfn)
249 "Return a list of th elements that appear in SEQ1 but not in SEQ2.
250 Equality is defined by TESTFN if non-nil or by `equal' if nil."
251 (seq-reduce (lambda (acc elt)
252 (if (not (seq-contains-p seq2 elt testfn))
253 (cons elt acc)
254 acc))
255 (seq-reverse seq1)
256 '()))
257
258 (defun seq-group-by (function seq)
259 "Apply FUNCTION to each element of SEQ.
260 Separate the elements of SEQ into an alist using the results as
261 keys. Keys are compared using `equal'."
262 (seq-reduce
263 (lambda (acc elt)
264 (let* ((key (funcall function elt))
265 (cell (assoc key acc)))
266 (if cell
267 (setcdr cell (push elt (cdr cell)))
268 (push (list key elt) acc))
269 acc))
270 (seq-reverse seq)
271 nil))
272
273 (defalias 'seq-reverse
274 (if (ignore-errors (reverse [1 2]))
275 #'reverse
276 (lambda (seq)
277 "Return the reversed copy of list, vector, or string SEQ.
278 See also the function `nreverse', which is used more often."
279 (let ((result '()))
280 (seq-map (lambda (elt) (push elt result))
281 seq)
282 (if (listp seq)
283 result
284 (seq-into result (type-of seq)))))))
285
286 (defun seq-into (seq type)
287 "Convert the sequence SEQ into a sequence of type TYPE.
288 TYPE can be one of the following symbols: vector, string or list."
289 (pcase type
290 (`vector (vconcat seq))
291 (`string (concat seq))
292 (`list (append seq nil))
293 (t (error "Not a sequence type name: %S" type))))
294
295 (defun seq--drop-list (list n)
296 "Return a list from LIST without its first N elements.
297 This is an optimization for lists in `seq-drop'."
298 (while (and list (> n 0))
299 (setq list (cdr list)
300 n (1- n)))
301 list)
302
303 (defun seq--take-list (list n)
304 "Return a list from LIST made of its first N elements.
305 This is an optimization for lists in `seq-take'."
306 (let ((result '()))
307 (while (and list (> n 0))
308 (setq n (1- n))
309 (push (pop list) result))
310 (nreverse result)))
311
312 (defun seq--drop-while-list (pred list)
313 "Return a list from the first element for which (PRED element) is nil in LIST.
314 This is an optimization for lists in `seq-drop-while'."
315 (while (and list (funcall pred (car list)))
316 (setq list (cdr list)))
317 list)
318
319 (defun seq--take-while-list (pred list)
320 "Return the successive elements for which (PRED element) is non-nil in LIST.
321 This is an optimization for lists in `seq-take-while'."
322 (let ((result '()))
323 (while (and list (funcall pred (car list)))
324 (push (pop list) result))
325 (nreverse result)))
326
327 (defun seq--count-successive (pred seq)
328 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
329 (let ((n 0)
330 (len (seq-length seq)))
331 (while (and (< n len)
332 (funcall pred (seq-elt seq n)))
333 (setq n (+ 1 n)))
334 n))
335
336 (defun seq--activate-font-lock-keywords ()
337 "Activate font-lock keywords for some symbols defined in seq."
338 (font-lock-add-keywords 'emacs-lisp-mode
339 '("\\<seq-doseq\\>")))
340
341 (defalias 'seq-copy #'copy-sequence)
342 (defalias 'seq-elt #'elt)
343 (defalias 'seq-length #'length)
344 (defalias 'seq-do #'mapc)
345 (defalias 'seq-each #'seq-do)
346 (defalias 'seq-map #'mapcar)
347
348 (unless (fboundp 'elisp--font-lock-flush-elisp-buffers)
349 ;; In Emacsā‰„25, (via elisp--font-lock-flush-elisp-buffers and a few others)
350 ;; we automatically highlight macros.
351 (add-to-list 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
352
353 (provide 'seq)
354 ;;; seq.el ends here