Better docstring and parameter name for seq-find
[emacs.git] / lisp / emacs-lisp / seq.el
blobe0f17c0335d1bfef97423c0803f0bff2085fbb0b
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: 2.0
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 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.
41 ;; While seq.el version 1.8 is in GNU ELPA for convenience, seq.el
42 ;; version 2.0 requires Emacs>=25.1.
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'
55 ;; All functions are tested in test/automated/seq-tests.el
57 ;;; Code:
59 (eval-when-compile (require 'cl-generic))
60 (require 'cl-extra) ;; for cl-subseq
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.
66 Evaluate BODY with VAR bound to each element of SEQ, in turn.
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)))
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)))
82 (defmacro seq-let (args seq &rest body)
83 "Bind the variables in ARGS to the elements of SEQ then evaluate BODY.
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))
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))
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))
103 (cl-defmethod (setf seq-elt) (store (seq cons) n)
104 (setcar (nthcdr n seq) store))
106 (cl-defgeneric seq-length (seq)
107 "Return the length of the sequence SEQ."
108 (length seq))
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))
115 (defalias 'seq-each #'seq-do)
117 (cl-defgeneric seq-p (seq)
118 "Return non-nil if SEQ is a sequence, nil otherwise."
119 (sequencep seq))
121 (cl-defgeneric seq-copy (seq)
122 "Return a shallow copy of SEQ."
123 (copy-sequence seq))
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))
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)))
142 ;; faster implementation for sequences (sequencep)
143 (cl-defmethod seq-map (function (seq sequence))
144 (mapcar function seq))
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.
150 If N is a negative integer or zero, SEQ is returned."
151 (if (<= n 0)
153 (let ((length (seq-length seq)))
154 (seq-subseq seq (min n length) length))))
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.
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))))
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)))
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)))
174 (cl-defgeneric seq-empty-p (seq)
175 "Return non-nil if the sequence SEQ is empty, nil otherwise."
176 (= 0 (seq-length seq)))
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))))
184 (cl-defmethod seq-sort (pred (list list))
185 (sort (seq-copy list) pred))
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))))
195 ;; faster implementation for sequences (sequencep)
196 (cl-defmethod seq-reverse ((seq sequence))
197 (reverse seq))
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.
203 \n(fn TYPE SEQUENCE...)"
204 (apply #'cl-concatenate type (seq-map #'seq-into-sequence seqs)))
206 (cl-defgeneric seq-into-sequence (seq)
207 "Convert SEQ into a sequence.
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)
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))))
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)
231 exclude))
232 seq))))
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))
239 (cl-defgeneric seq-reduce (function seq initial-value)
240 "Reduce the function FUNCTION across SEQ, starting with INITIAL-VALUE.
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.
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)))
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)))
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))
273 (cl-defgeneric seq-find (pred seq &optional default)
274 "Return the first element for which (PRED element) is non-nil in SEQ.
275 If no element is found, return DEFAULT.
277 Note that `seq-find' has an ambiguity if the found element is
278 identical to DEFAULT, as it cannot be known if an element was
279 found or not."
280 (catch 'seq--break
281 (seq-doseq (elt seq)
282 (when (funcall pred elt)
283 (throw 'seq--break elt)))
284 default))
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))
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))
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)))
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)))
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))))
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 '()))
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 '()))
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))
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)))
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)))
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)))
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)))
395 (push `(app (pcase--flip seq--elt-safe ,index) ,name) bindings))))
396 (setq index (1+ index)))
397 bindings))
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)))
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)))
415 ;;; Optimized implementations for lists
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)
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)))
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)
438 (cl-defmethod seq-empty-p ((list list))
439 "Optimized implementation of `seq-empty-p' for lists."
440 (null list))
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\\>")))
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))
453 (provide 'seq)
454 ;;; seq.el ends here