]> code.delx.au - gnu-emacs-elpa/blob - packages/seq/seq.el
Merge commit 'f4e1cbc5386fbf197ff8bcb6a9275f17c9cbe8e4' from context-coloring
[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.2
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 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.
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-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))
257
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)))))))
270
271 (defun seq--into (seq type)
272 "Convert the sequence SEQ into a sequence of type TYPE."
273 (pcase type
274 (`vector (vconcat seq))
275 (`string (concat seq))
276 (`list (append seq nil))
277 (t (error "Not a sequence type name: %s" type))))
278
279 (defun seq--drop-list (list n)
280 "Return a list from LIST without its first N elements.
281 This is an optimization for lists in `seq-drop'."
282 (while (and list (> n 0))
283 (setq list (cdr list)
284 n (1- n)))
285 list)
286
287 (defun seq--take-list (list n)
288 "Return a list from LIST made of its first N elements.
289 This is an optimization for lists in `seq-take'."
290 (let ((result '()))
291 (while (and list (> n 0))
292 (setq n (1- n))
293 (push (pop list) result))
294 (nreverse result)))
295
296 (defun seq--drop-while-list (pred list)
297 "Return a list from the first element for which (PRED element) is nil in LIST.
298 This is an optimization for lists in `seq-drop-while'."
299 (while (and list (funcall pred (car list)))
300 (setq list (cdr list)))
301 list)
302
303 (defun seq--take-while-list (pred list)
304 "Return the successive elements for which (PRED element) is non-nil in LIST.
305 This is an optimization for lists in `seq-take-while'."
306 (let ((result '()))
307 (while (and list (funcall pred (car list)))
308 (push (pop list) result))
309 (nreverse result)))
310
311 (defun seq--count-successive (pred seq)
312 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
313 (let ((n 0)
314 (len (seq-length seq)))
315 (while (and (< n len)
316 (funcall pred (seq-elt seq n)))
317 (setq n (+ 1 n)))
318 n))
319
320 (defalias 'seq-copy #'copy-sequence)
321 (defalias 'seq-elt #'elt)
322 (defalias 'seq-length #'length)
323 (defalias 'seq-do #'mapc)
324 (defalias 'seq-each #'seq-do)
325 (defalias 'seq-map #'mapcar)
326
327 (provide 'seq)
328 ;;; seq.el ends here