Use PAT rather than UPAT in pcase macros
[emacs.git] / lisp / emacs-lisp / seq.el
bloba17b0a8f1b92e22852af2df1f5b3567de2dcc2bd
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-some-p (pred seq)
256 "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise."
257 (catch 'seq--break
258 (seq-doseq (elt seq)
259 (when (funcall pred elt)
260 (throw 'seq--break elt)))
261 nil))
263 (cl-defgeneric seq-every-p (pred seq)
264 "Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ."
265 (catch 'seq--break
266 (seq-doseq (elt seq)
267 (or (funcall pred elt)
268 (throw 'seq--break nil)))
271 (cl-defgeneric seq-count (pred seq)
272 "Return the number of elements for which (PRED element) is non-nil in SEQ."
273 (let ((count 0))
274 (seq-doseq (elt seq)
275 (when (funcall pred elt)
276 (setq count (+ 1 count))))
277 count))
279 (cl-defgeneric seq-contains-p (seq elt &optional testfn)
280 "Return the first element in SEQ that equals to ELT.
281 Equality is defined by TESTFN if non-nil or by `equal' if nil."
282 (seq-some-p (lambda (e)
283 (funcall (or testfn #'equal) elt e))
284 seq))
286 (cl-defgeneric seq-uniq (seq &optional testfn)
287 "Return a list of the elements of SEQ with duplicates removed.
288 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
289 (let ((result '()))
290 (seq-doseq (elt seq)
291 (unless (seq-contains-p result elt testfn)
292 (setq result (cons elt result))))
293 (nreverse result)))
295 (cl-defgeneric seq-mapcat (function seq &optional type)
296 "Concatenate the result of applying FUNCTION to each element of SEQ.
297 The result is a sequence of type TYPE, or a list if TYPE is nil."
298 (apply #'seq-concatenate (or type 'list)
299 (seq-map function seq)))
301 (cl-defgeneric seq-partition (seq n)
302 "Return a list of the elements of SEQ grouped into sub-sequences of length N.
303 The last sequence may contain less than N elements. If N is a
304 negative integer or 0, nil is returned."
305 (unless (< n 1)
306 (let ((result '()))
307 (while (not (seq-empty-p seq))
308 (push (seq-take seq n) result)
309 (setq seq (seq-drop seq n)))
310 (nreverse result))))
312 (cl-defgeneric seq-intersection (seq1 seq2 &optional testfn)
313 "Return a list of the elements that appear in both SEQ1 and SEQ2.
314 Equality is defined by TESTFN if non-nil or by `equal' if nil."
315 (seq-reduce (lambda (acc elt)
316 (if (seq-contains-p seq2 elt testfn)
317 (cons elt acc)
318 acc))
319 (seq-reverse seq1)
320 '()))
322 (cl-defgeneric seq-difference (seq1 seq2 &optional testfn)
323 "Return a list of the elements that appear in SEQ1 but not in SEQ2.
324 Equality is defined by TESTFN if non-nil or by `equal' if nil."
325 (seq-reduce (lambda (acc elt)
326 (if (not (seq-contains-p seq2 elt testfn))
327 (cons elt acc)
328 acc))
329 (seq-reverse seq1)
330 '()))
332 (cl-defgeneric seq-group-by (function seq)
333 "Apply FUNCTION to each element of SEQ.
334 Separate the elements of SEQ into an alist using the results as
335 keys. Keys are compared using `equal'."
336 (seq-reduce
337 (lambda (acc elt)
338 (let* ((key (funcall function elt))
339 (cell (assoc key acc)))
340 (if cell
341 (setcdr cell (push elt (cdr cell)))
342 (push (list key elt) acc))
343 acc))
344 (seq-reverse seq)
345 nil))
347 (cl-defgeneric seq-min (seq)
348 "Return the smallest element of SEQ.
349 SEQ must be a sequence of numbers or markers."
350 (apply #'min (seq-into seq 'list)))
352 (cl-defgeneric seq-max (seq)
353 "Return the largest element of SEQ.
354 SEQ must be a sequence of numbers or markers."
355 (apply #'max (seq-into seq 'list)))
357 (defun seq--count-successive (pred seq)
358 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
359 (let ((n 0)
360 (len (seq-length seq)))
361 (while (and (< n len)
362 (funcall pred (seq-elt seq n)))
363 (setq n (+ 1 n)))
366 (defun seq--make-pcase-bindings (args)
367 "Return a list of bindings of the variables in ARGS to the elements of a sequence."
368 (let ((bindings '())
369 (index 0)
370 (rest-marker nil))
371 (seq-doseq (name args)
372 (unless rest-marker
373 (pcase name
374 (`&rest
375 (progn (push `(app (pcase--flip seq-drop ,index)
376 ,(seq--elt-safe args (1+ index)))
377 bindings)
378 (setq rest-marker t)))
380 (push `(app (pcase--flip seq--elt-safe ,index) ,name) bindings))))
381 (setq index (1+ index)))
382 bindings))
384 (defun seq--make-pcase-patterns (args)
385 "Return a list of `(seq ...)' pcase patterns from the argument list ARGS."
386 (cons 'seq
387 (seq-map (lambda (elt)
388 (if (seq-p elt)
389 (seq--make-pcase-patterns elt)
390 elt))
391 args)))
393 ;; TODO: make public?
394 (defun seq--elt-safe (seq n)
395 "Return element of SEQ at the index N.
396 If no element is found, return nil."
397 (ignore-errors (seq-elt seq n)))
400 ;;; Optimized implementations for lists
402 (cl-defmethod seq-drop ((list list) n)
403 "Optimized implementation of `seq-drop' for lists."
404 (while (and list (> n 0))
405 (setq list (cdr list)
406 n (1- n)))
407 list)
409 (cl-defmethod seq-take ((list list) n)
410 "Optimized implementation of `seq-take' for lists."
411 (let ((result '()))
412 (while (and list (> n 0))
413 (setq n (1- n))
414 (push (pop list) result))
415 (nreverse result)))
417 (cl-defmethod seq-drop-while (pred (list list))
418 "Optimized implementation of `seq-drop-while' for lists"
419 (while (and list (funcall pred (car list)))
420 (setq list (cdr list)))
421 list)
423 (cl-defmethod seq-drop-while (pred (list list))
424 "Optimized implementation of `seq-drop-while' for lists"
425 (while (and list (funcall pred (car list)))
426 (setq list (cdr list)))
427 list)
429 (cl-defmethod seq-empty-p ((list list))
430 "Optimized implementation of `seq-empty-p' for lists."
431 (null list))
434 (defun seq--activate-font-lock-keywords ()
435 "Activate font-lock keywords for some symbols defined in seq."
436 (font-lock-add-keywords 'emacs-lisp-mode
437 '("\\<seq-doseq\\>" "\\<seq-let\\>")))
439 (unless (fboundp 'elisp--font-lock-flush-elisp-buffers)
440 ;; In Emacsā‰„25, (via elisp--font-lock-flush-elisp-buffers and a few others)
441 ;; we automatically highlight macros.
442 (add-hook 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
444 (provide 'seq)
445 ;;; seq.el ends here