]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/seq.el
Add seq-find
[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: 2.0
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 ;; While seq.el version 1.8 is in GNU ELPA for convenience, seq.el
42 ;; version 2.0 requires Emacs>=25.1.
43 ;;
44 ;; seq.el can be extended to support new type of sequences. Here are
45 ;; the generic functions that must be implemented by new seq types:
46 ;; - `seq-elt'
47 ;; - `seq-length'
48 ;; - `seq-do'
49 ;; - `seq-p'
50 ;; - `seq-subseq'
51 ;; - `seq-into-sequence'
52 ;; - `seq-copy'
53 ;; - `seq-into'
54 ;;
55 ;; All functions are tested in test/automated/seq-tests.el
56
57 ;;; Code:
58
59 (eval-when-compile (require 'cl-generic))
60 (require 'cl-extra) ;; for cl-subseq
61
62 (defmacro seq-doseq (spec &rest body)
63 "Loop over a sequence.
64 Similar to `dolist' but can be applied to lists, strings, and vectors.
65
66 Evaluate BODY with VAR bound to each element of SEQ, in turn.
67
68 \(fn (VAR SEQ) BODY...)"
69 (declare (indent 1) (debug ((symbolp form &optional form) body)))
70 `(seq-do (lambda (,(car spec))
71 ,@body)
72 ,(cadr spec)))
73
74 (pcase-defmacro seq (&rest args)
75 "pcase pattern matching sequence elements.
76 Matches if the object is a sequence (list, string or vector), and
77 binds each element of ARGS to the corresponding element of the
78 sequence."
79 `(and (pred seq-p)
80 ,@(seq--make-pcase-bindings args)))
81
82 (defmacro seq-let (args seq &rest body)
83 "Bind the variables in ARGS to the elements of SEQ then evaluate BODY.
84
85 ARGS can also include the `&rest' marker followed by a variable
86 name to be bound to the rest of SEQ."
87 (declare (indent 2) (debug t))
88 `(pcase-let ((,(seq--make-pcase-patterns args) ,seq))
89 ,@body))
90 \f
91
92 ;;; Basic seq functions that have to be implemented by new seq types
93 (cl-defgeneric seq-elt (seq n)
94 "Return the element of SEQ at index N."
95 (elt seq n))
96
97 ;; Default gv setters for `seq-elt'.
98 ;; It can be a good idea for new sequence implementations to provide a
99 ;; "gv-setter" for `seq-elt'.
100 (cl-defmethod (setf seq-elt) (store (seq array) n)
101 (aset seq n store))
102
103 (cl-defmethod (setf seq-elt) (store (seq cons) n)
104 (setcar (nthcdr n seq) store))
105
106 (cl-defgeneric seq-length (seq)
107 "Return the length of the sequence SEQ."
108 (length seq))
109
110 (cl-defgeneric seq-do (function seq)
111 "Apply FUNCTION to each element of SEQ, presumably for side effects.
112 Return SEQ."
113 (mapc function seq))
114
115 (defalias 'seq-each #'seq-do)
116
117 (cl-defgeneric seq-p (seq)
118 "Return non-nil if SEQ is a sequence, nil otherwise."
119 (sequencep seq))
120
121 (cl-defgeneric seq-copy (seq)
122 "Return a shallow copy of SEQ."
123 (copy-sequence seq))
124
125 (cl-defgeneric seq-subseq (seq start &optional end)
126 "Return the subsequence of SEQ from START to END.
127 If END is omitted, it defaults to the length of the sequence.
128 If START or END is negative, it counts from the end.
129 Signal an error if START or END are outside of the sequence (i.e
130 too large if positive or too small if negative)."
131 (cl-subseq seq start end))
132
133 \f
134 (cl-defgeneric seq-map (function seq)
135 "Return the result of applying FUNCTION to each element of SEQ."
136 (let (result)
137 (seq-do (lambda (elt)
138 (push (funcall function elt) result))
139 seq)
140 (nreverse result)))
141
142 ;; faster implementation for sequences (sequencep)
143 (cl-defmethod seq-map (function (seq sequence))
144 (mapcar function seq))
145
146 (cl-defgeneric seq-drop (seq n)
147 "Return a subsequence of SEQ without its first N elements.
148 The result is a sequence of the same type as SEQ.
149
150 If N is a negative integer or zero, SEQ is returned."
151 (if (<= n 0)
152 seq
153 (let ((length (seq-length seq)))
154 (seq-subseq seq (min n length) length))))
155
156 (cl-defgeneric seq-take (seq n)
157 "Return a subsequence of SEQ with its first N elements.
158 The result is a sequence of the same type as SEQ.
159
160 If N is a negative integer or zero, an empty sequence is
161 returned."
162 (seq-subseq seq 0 (min (max n 0) (seq-length seq))))
163
164 (cl-defgeneric seq-drop-while (pred seq)
165 "Return a sequence from the first element for which (PRED element) is nil in SEQ.
166 The result is a sequence of the same type as SEQ."
167 (seq-drop seq (seq--count-successive pred seq)))
168
169 (cl-defgeneric seq-take-while (pred seq)
170 "Return the successive elements for which (PRED element) is non-nil in SEQ.
171 The result is a sequence of the same type as SEQ."
172 (seq-take seq (seq--count-successive pred seq)))
173
174 (cl-defgeneric seq-empty-p (seq)
175 "Return non-nil if the sequence SEQ is empty, nil otherwise."
176 (= 0 (seq-length seq)))
177
178 (cl-defgeneric seq-sort (pred seq)
179 "Return a sorted sequence comparing using PRED the elements of SEQ.
180 The result is a sequence of the same type as SEQ."
181 (let ((result (seq-sort pred (append seq nil))))
182 (seq-into result (type-of seq))))
183
184 (cl-defmethod seq-sort (pred (list list))
185 (sort (seq-copy list) pred))
186
187 (cl-defgeneric seq-reverse (seq)
188 "Return the reversed shallow copy of SEQ."
189 (let ((result '()))
190 (seq-map (lambda (elt)
191 (push elt result))
192 seq)
193 (seq-into result (type-of seq))))
194
195 ;; faster implementation for sequences (sequencep)
196 (cl-defmethod seq-reverse ((seq sequence))
197 (reverse seq))
198
199 (cl-defgeneric seq-concatenate (type &rest seqs)
200 "Concatenate, into a sequence of type TYPE, the sequences SEQS.
201 TYPE must be one of following symbols: vector, string or list.
202
203 \n(fn TYPE SEQUENCE...)"
204 (apply #'cl-concatenate type (seq-map #'seq-into-sequence seqs)))
205
206 (cl-defgeneric seq-into-sequence (seq)
207 "Convert SEQ into a sequence.
208
209 The default implementation is to signal an error if SEQ is not a
210 sequence, specific functions should be implemented for new types
211 of seq."
212 (unless (sequencep seq)
213 (error "Cannot convert %S into a sequence" seq))
214 seq)
215
216 (cl-defgeneric seq-into (seq type)
217 "Convert the sequence SEQ into a sequence of type TYPE.
218 TYPE can be one of the following symbols: vector, string or list."
219 (pcase type
220 (`vector (vconcat seq))
221 (`string (concat seq))
222 (`list (append seq nil))
223 (_ (error "Not a sequence type name: %S" type))))
224
225 (cl-defgeneric seq-filter (pred seq)
226 "Return a list of all the elements for which (PRED element) is non-nil in SEQ."
227 (let ((exclude (make-symbol "exclude")))
228 (delq exclude (seq-map (lambda (elt)
229 (if (funcall pred elt)
230 elt
231 exclude))
232 seq))))
233
234 (cl-defgeneric seq-remove (pred seq)
235 "Return a list of all the elements for which (PRED element) is nil in SEQ."
236 (seq-filter (lambda (elt) (not (funcall pred elt)))
237 seq))
238
239 (cl-defgeneric seq-reduce (function seq initial-value)
240 "Reduce the function FUNCTION across SEQ, starting with INITIAL-VALUE.
241
242 Return the result of calling FUNCTION with INITIAL-VALUE and the
243 first element of SEQ, then calling FUNCTION with that result and
244 the second element of SEQ, then with that result and the third
245 element of SEQ, etc.
246
247 If SEQ is empty, return INITIAL-VALUE and FUNCTION is not called."
248 (if (seq-empty-p seq)
249 initial-value
250 (let ((acc initial-value))
251 (seq-doseq (elt seq)
252 (setq acc (funcall function acc elt)))
253 acc)))
254
255 (cl-defgeneric seq-every-p (pred seq)
256 "Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ."
257 (catch 'seq--break
258 (seq-doseq (elt seq)
259 (or (funcall pred elt)
260 (throw 'seq--break nil)))
261 t))
262
263 (cl-defgeneric seq-some (pred seq)
264 "Return non-nil if (PRED element) is non-nil for any element in SEQ, nil otherwise.
265 If so, return the non-nil value returned by PRED."
266 (catch 'seq--break
267 (seq-doseq (elt seq)
268 (let ((result (funcall pred elt)))
269 (when result
270 (throw 'seq--break result))))
271 nil))
272
273 (cl-defgeneric seq-find (pred seq &optional sentinel)
274 "Return the first element for which (PRED element) is non-nil in SEQ.
275 If no element is found, return SENTINEL or nil.
276
277 Note that `seq-find' has an ambiguity if the found element is nil
278 and if no SENTINEL is specified, as it cannot be known if an
279 element was found or not."
280 (catch 'seq--break
281 (seq-doseq (elt seq)
282 (when (funcall pred elt)
283 (throw 'seq--break elt)))
284 sentinel))
285
286 (cl-defgeneric seq-count (pred seq)
287 "Return the number of elements for which (PRED element) is non-nil in SEQ."
288 (let ((count 0))
289 (seq-doseq (elt seq)
290 (when (funcall pred elt)
291 (setq count (+ 1 count))))
292 count))
293
294 (cl-defgeneric seq-contains (seq elt &optional testfn)
295 "Return the first element in SEQ that equals to ELT.
296 Equality is defined by TESTFN if non-nil or by `equal' if nil."
297 (seq-some (lambda (e)
298 (funcall (or testfn #'equal) elt e))
299 seq))
300
301 (cl-defgeneric seq-uniq (seq &optional testfn)
302 "Return a list of the elements of SEQ with duplicates removed.
303 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
304 (let ((result '()))
305 (seq-doseq (elt seq)
306 (unless (seq-contains result elt testfn)
307 (setq result (cons elt result))))
308 (nreverse result)))
309
310 (cl-defgeneric seq-mapcat (function seq &optional type)
311 "Concatenate the result of applying FUNCTION to each element of SEQ.
312 The result is a sequence of type TYPE, or a list if TYPE is nil."
313 (apply #'seq-concatenate (or type 'list)
314 (seq-map function seq)))
315
316 (cl-defgeneric seq-partition (seq n)
317 "Return a list of the elements of SEQ grouped into sub-sequences of length N.
318 The last sequence may contain less than N elements. If N is a
319 negative integer or 0, nil is returned."
320 (unless (< n 1)
321 (let ((result '()))
322 (while (not (seq-empty-p seq))
323 (push (seq-take seq n) result)
324 (setq seq (seq-drop seq n)))
325 (nreverse result))))
326
327 (cl-defgeneric seq-intersection (seq1 seq2 &optional testfn)
328 "Return a list of the elements that appear in both SEQ1 and SEQ2.
329 Equality is defined by TESTFN if non-nil or by `equal' if nil."
330 (seq-reduce (lambda (acc elt)
331 (if (seq-contains seq2 elt testfn)
332 (cons elt acc)
333 acc))
334 (seq-reverse seq1)
335 '()))
336
337 (cl-defgeneric seq-difference (seq1 seq2 &optional testfn)
338 "Return a list of the elements that appear in SEQ1 but not in SEQ2.
339 Equality is defined by TESTFN if non-nil or by `equal' if nil."
340 (seq-reduce (lambda (acc elt)
341 (if (not (seq-contains seq2 elt testfn))
342 (cons elt acc)
343 acc))
344 (seq-reverse seq1)
345 '()))
346
347 (cl-defgeneric seq-group-by (function seq)
348 "Apply FUNCTION to each element of SEQ.
349 Separate the elements of SEQ into an alist using the results as
350 keys. Keys are compared using `equal'."
351 (seq-reduce
352 (lambda (acc elt)
353 (let* ((key (funcall function elt))
354 (cell (assoc key acc)))
355 (if cell
356 (setcdr cell (push elt (cdr cell)))
357 (push (list key elt) acc))
358 acc))
359 (seq-reverse seq)
360 nil))
361
362 (cl-defgeneric seq-min (seq)
363 "Return the smallest element of SEQ.
364 SEQ must be a sequence of numbers or markers."
365 (apply #'min (seq-into seq 'list)))
366
367 (cl-defgeneric seq-max (seq)
368 "Return the largest element of SEQ.
369 SEQ must be a sequence of numbers or markers."
370 (apply #'max (seq-into seq 'list)))
371
372 (defun seq--count-successive (pred seq)
373 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
374 (let ((n 0)
375 (len (seq-length seq)))
376 (while (and (< n len)
377 (funcall pred (seq-elt seq n)))
378 (setq n (+ 1 n)))
379 n))
380
381 (defun seq--make-pcase-bindings (args)
382 "Return a list of bindings of the variables in ARGS to the elements of a sequence."
383 (let ((bindings '())
384 (index 0)
385 (rest-marker nil))
386 (seq-doseq (name args)
387 (unless rest-marker
388 (pcase name
389 (`&rest
390 (progn (push `(app (pcase--flip seq-drop ,index)
391 ,(seq--elt-safe args (1+ index)))
392 bindings)
393 (setq rest-marker t)))
394 (_
395 (push `(app (pcase--flip seq--elt-safe ,index) ,name) bindings))))
396 (setq index (1+ index)))
397 bindings))
398
399 (defun seq--make-pcase-patterns (args)
400 "Return a list of `(seq ...)' pcase patterns from the argument list ARGS."
401 (cons 'seq
402 (seq-map (lambda (elt)
403 (if (seq-p elt)
404 (seq--make-pcase-patterns elt)
405 elt))
406 args)))
407
408 ;; TODO: make public?
409 (defun seq--elt-safe (seq n)
410 "Return element of SEQ at the index N.
411 If no element is found, return nil."
412 (ignore-errors (seq-elt seq n)))
413 \f
414
415 ;;; Optimized implementations for lists
416
417 (cl-defmethod seq-drop ((list list) n)
418 "Optimized implementation of `seq-drop' for lists."
419 (while (and list (> n 0))
420 (setq list (cdr list)
421 n (1- n)))
422 list)
423
424 (cl-defmethod seq-take ((list list) n)
425 "Optimized implementation of `seq-take' for lists."
426 (let ((result '()))
427 (while (and list (> n 0))
428 (setq n (1- n))
429 (push (pop list) result))
430 (nreverse result)))
431
432 (cl-defmethod seq-drop-while (pred (list list))
433 "Optimized implementation of `seq-drop-while' for lists."
434 (while (and list (funcall pred (car list)))
435 (setq list (cdr list)))
436 list)
437
438 (cl-defmethod seq-empty-p ((list list))
439 "Optimized implementation of `seq-empty-p' for lists."
440 (null list))
441 \f
442
443 (defun seq--activate-font-lock-keywords ()
444 "Activate font-lock keywords for some symbols defined in seq."
445 (font-lock-add-keywords 'emacs-lisp-mode
446 '("\\<seq-doseq\\>" "\\<seq-let\\>")))
447
448 (unless (fboundp 'elisp--font-lock-flush-elisp-buffers)
449 ;; In Emacsā‰„25, (via elisp--font-lock-flush-elisp-buffers and a few others)
450 ;; we automatically highlight macros.
451 (add-hook 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
452
453 (provide 'seq)
454 ;;; seq.el ends here