1 ;;; seq-25.el --- seq.el implementation for Emacs 25.x -*- lexical-binding: t -*-
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 ;; Author: Nicolas Petton <nicolas@petton.fr>
8 ;; Maintainer: emacs-devel@gnu.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Sequence-manipulation functions that complement basic functions
28 ;; provided by subr.el.
30 ;; All functions are prefixed with "seq-".
32 ;; All provided functions work on lists, strings and vectors.
34 ;; Functions taking a predicate or iterating over a sequence using a
35 ;; function as argument take the function as their first argument and
36 ;; the sequence as their second argument. All other functions take
37 ;; the sequence as their first argument.
39 ;; seq.el can be extended to support new type of sequences. Here are
40 ;; the generic functions that must be implemented by new seq types:
46 ;; - `seq-into-sequence'
52 ;; When loading seq.el in Emacs 24.x, this file gets byte-compiled, even if
53 ;; never used. This takes care of byte-compilation warnings is emitted, by
54 ;; emitting nil in the macro expansion in Emacs 24.x.
55 (defmacro seq--when-emacs-25-p (&rest body)
56 "Execute BODY if in Emacs>=25.x."
57 (declare (indent (lambda (&rest x) 0)) (debug t))
58 (when (version<= "25" emacs-version)
64 (require 'cl-lib) ;; for cl-subseq
66 (defmacro seq-doseq (spec &rest body)
67 "Loop over a sequence.
68 Evaluate BODY with VAR bound to each element of SEQUENCE, in turn.
70 Similar to `dolist' but can be applied to lists, strings, and vectors.
72 \(fn (VAR SEQUENCE) BODY...)"
73 (declare (indent 1) (debug ((symbolp form &optional form) body)))
74 `(seq-do (lambda (,(car spec))
78 (pcase-defmacro seq (&rest patterns)
79 "Build a `pcase' pattern that matches elements of SEQUENCE.
81 The `pcase' pattern will match each element of PATTERNS against the
82 corresponding element of SEQUENCE.
84 Extra elements of the sequence are ignored if fewer PATTERNS are
85 given, and the match does not fail."
87 ,@(seq--make-pcase-bindings patterns)))
89 (defmacro seq-let (args sequence &rest body)
90 "Bind the variables in ARGS to the elements of SEQUENCE, then evaluate BODY.
92 ARGS can also include the `&rest' marker followed by a variable
93 name to be bound to the rest of SEQUENCE."
94 (declare (indent 2) (debug (sexp form body)))
95 `(pcase-let ((,(seq--make-pcase-patterns args) ,sequence))
99 ;;; Basic seq functions that have to be implemented by new sequence types
100 (cl-defgeneric seq-elt (sequence n)
101 "Return Nth element of SEQUENCE."
104 ;; Default gv setters for `seq-elt'.
105 ;; It can be a good idea for new sequence implementations to provide a
106 ;; "gv-setter" for `seq-elt'.
107 (cl-defmethod (setf seq-elt) (store (sequence array) n)
108 (aset sequence n store))
110 (cl-defmethod (setf seq-elt) (store (sequence cons) n)
111 (setcar (nthcdr n sequence) store))
113 (cl-defgeneric seq-length (sequence)
114 "Return the number of elements of SEQUENCE."
117 (cl-defgeneric seq-do (function sequence)
118 "Apply FUNCTION to each element of SEQUENCE, presumably for side effects.
120 (mapc function sequence))
122 (defalias 'seq-each #'seq-do)
124 (cl-defgeneric seqp (sequence)
125 "Return non-nil if SEQUENCE is a sequence, nil otherwise."
126 (sequencep sequence))
128 (cl-defgeneric seq-copy (sequence)
129 "Return a shallow copy of SEQUENCE."
130 (copy-sequence sequence))
132 (cl-defgeneric seq-subseq (sequence start &optional end)
133 "Return the sequence of elements of SEQUENCE from START to END.
136 If END is omitted, it defaults to the length of the sequence. If
137 START or END is negative, it counts from the end. Signal an
138 error if START or END are outside of the sequence (i.e too large
139 if positive or too small if negative)."
140 (cl-subseq sequence start end))
143 (cl-defgeneric seq-map (function sequence)
144 "Return the result of applying FUNCTION to each element of SEQUENCE."
146 (seq-do (lambda (elt)
147 (push (funcall function elt) result))
151 (defun seq-map-indexed (function sequence)
152 "Return the result of applying FUNCTION to each element of SEQUENCE.
153 Unlike `seq-map', FUNCTION takes two arguments: the element of
154 the sequence, and its index within the sequence."
156 (seq-map (lambda (elt)
158 (funcall function elt index)
159 (setq index (1+ index))))
162 ;; faster implementation for sequences (sequencep)
163 (cl-defmethod seq-map (function (sequence sequence))
164 (mapcar function sequence))
166 (cl-defgeneric seq-mapn (function sequence &rest sequences)
167 "Like `seq-map' but FUNCTION is mapped over all SEQUENCES.
168 The arity of FUNCTION must match the number of SEQUENCES, and the
169 mapping stops on the shortest sequence.
170 Return a list of the results.
172 \(fn FUNCTION SEQUENCES...)"
174 (sequences (seq-map (lambda (s)
176 (cons sequence sequences))))
177 (while (not (memq nil sequences))
178 (push (apply function (seq-map #'car sequences)) result)
179 (setq sequences (seq-map #'cdr sequences)))
182 (cl-defgeneric seq-drop (sequence n)
183 "Remove the first N elements of SEQUENCE and return the result.
184 The result is a sequence of the same type as SEQUENCE.
186 If N is a negative integer or zero, SEQUENCE is returned."
189 (let ((length (seq-length sequence)))
190 (seq-subseq sequence (min n length) length))))
192 (cl-defgeneric seq-take (sequence n)
193 "Take the first N elements of SEQUENCE and return the result.
194 The result is a sequence of the same type as SEQUENCE.
196 If N is a negative integer or zero, an empty sequence is
198 (seq-subseq sequence 0 (min (max n 0) (seq-length sequence))))
200 (cl-defgeneric seq-drop-while (pred sequence)
201 "Remove the successive elements of SEQUENCE for which PRED returns non-nil.
202 PRED is a function of one argument. The result is a sequence of
203 the same type as SEQUENCE."
204 (seq-drop sequence (seq--count-successive pred sequence)))
206 (cl-defgeneric seq-take-while (pred sequence)
207 "Take the successive elements of SEQUENCE for which PRED returns non-nil.
208 PRED is a function of one argument. The result is a sequence of
209 the same type as SEQUENCE."
210 (seq-take sequence (seq--count-successive pred sequence)))
212 (cl-defgeneric seq-empty-p (sequence)
213 "Return non-nil if the SEQUENCE is empty, nil otherwise."
214 (= 0 (seq-length sequence)))
216 (cl-defgeneric seq-sort (pred sequence)
217 "Sort SEQUENCE using PRED as comparison function.
218 The result is a sequence of the same type as SEQUENCE."
219 (let ((result (seq-sort pred (append sequence nil))))
220 (seq-into result (type-of sequence))))
222 (defun seq-sort-by (function pred sequence)
223 "Sort SEQUENCE using PRED as a comparison function.
224 Elements of SEQUENCE are transformed by FUNCTION before being
225 sorted. FUNCTION must be a function of one argument."
226 (seq-sort (lambda (a b)
229 (funcall function b)))
232 (cl-defmethod seq-sort (pred (list list))
233 (sort (seq-copy list) pred))
235 (cl-defgeneric seq-reverse (sequence)
236 "Return a sequence with elements of SEQUENCE in reverse order."
238 (seq-map (lambda (elt)
241 (seq-into result (type-of sequence))))
243 ;; faster implementation for sequences (sequencep)
244 (cl-defmethod seq-reverse ((sequence sequence))
247 (cl-defgeneric seq-concatenate (type &rest sequences)
248 "Concatenate SEQUENCES into a single sequence of type TYPE.
249 TYPE must be one of following symbols: vector, string or list.
251 \n(fn TYPE SEQUENCE...)"
252 (apply #'cl-concatenate type (seq-map #'seq-into-sequence sequences)))
254 (cl-defgeneric seq-into-sequence (sequence)
255 "Convert SEQUENCE into a sequence.
257 The default implementation is to signal an error if SEQUENCE is not a
258 sequence, specific functions should be implemented for new types
260 (unless (sequencep sequence)
261 (error "Cannot convert %S into a sequence" sequence))
264 (cl-defgeneric seq-into (sequence type)
265 "Concatenate the elements of SEQUENCE into a sequence of type TYPE.
266 TYPE can be one of the following symbols: vector, string or
269 (`vector (seq--into-vector sequence))
270 (`string (seq--into-string sequence))
271 (`list (seq--into-list sequence))
272 (_ (error "Not a sequence type name: %S" type))))
274 (cl-defgeneric seq-filter (pred sequence)
275 "Return a list of all the elements for which (PRED element) is non-nil in SEQUENCE."
276 (let ((exclude (make-symbol "exclude")))
277 (delq exclude (seq-map (lambda (elt)
278 (if (funcall pred elt)
283 (cl-defgeneric seq-remove (pred sequence)
284 "Return a list of all the elements for which (PRED element) is nil in SEQUENCE."
285 (seq-filter (lambda (elt) (not (funcall pred elt)))
288 (cl-defgeneric seq-reduce (function sequence initial-value)
289 "Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE.
291 Return the result of calling FUNCTION with INITIAL-VALUE and the
292 first element of SEQUENCE, then calling FUNCTION with that result and
293 the second element of SEQUENCE, then with that result and the third
294 element of SEQUENCE, etc.
296 If SEQUENCE is empty, return INITIAL-VALUE and FUNCTION is not called."
297 (if (seq-empty-p sequence)
299 (let ((acc initial-value))
300 (seq-doseq (elt sequence)
301 (setq acc (funcall function acc elt)))
304 (cl-defgeneric seq-every-p (pred sequence)
305 "Return non-nil if (PRED element) is non-nil for all elements of SEQUENCE."
307 (seq-doseq (elt sequence)
308 (or (funcall pred elt)
309 (throw 'seq--break nil)))
312 (cl-defgeneric seq-some (pred sequence)
313 "Return the first value for which if (PRED element) is non-nil for in SEQUENCE."
315 (seq-doseq (elt sequence)
316 (let ((result (funcall pred elt)))
318 (throw 'seq--break result))))
321 (cl-defgeneric seq-find (pred sequence &optional default)
322 "Return the first element for which (PRED element) is non-nil in SEQUENCE.
323 If no element is found, return DEFAULT.
325 Note that `seq-find' has an ambiguity if the found element is
326 identical to DEFAULT, as it cannot be known if an element was
329 (seq-doseq (elt sequence)
330 (when (funcall pred elt)
331 (throw 'seq--break elt)))
334 (cl-defgeneric seq-count (pred sequence)
335 "Return the number of elements for which (PRED element) is non-nil in SEQUENCE."
337 (seq-doseq (elt sequence)
338 (when (funcall pred elt)
339 (setq count (+ 1 count))))
342 (cl-defgeneric seq-contains (sequence elt &optional testfn)
343 "Return the first element in SEQUENCE that is equal to ELT.
344 Equality is defined by TESTFN if non-nil or by `equal' if nil."
345 (seq-some (lambda (e)
346 (funcall (or testfn #'equal) elt e))
349 (cl-defgeneric seq-set-equal-p (sequence1 sequence2 &optional testfn)
350 "Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements, regardless of order.
351 Equality is defined by TESTFN if non-nil or by `equal' if nil."
352 (and (seq-every-p (lambda (item1) (seq-contains sequence2 item1 testfn)) sequence1)
353 (seq-every-p (lambda (item2) (seq-contains sequence1 item2 testfn)) sequence2)))
355 (cl-defgeneric seq-position (sequence elt &optional testfn)
356 "Return the index of the first element in SEQUENCE that is equal to ELT.
357 Equality is defined by TESTFN if non-nil or by `equal' if nil."
360 (seq-doseq (e sequence)
361 (when (funcall (or testfn #'equal) e elt)
362 (throw 'seq--break index))
363 (setq index (1+ index)))
366 (cl-defgeneric seq-uniq (sequence &optional testfn)
367 "Return a list of the elements of SEQUENCE with duplicates removed.
368 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
370 (seq-doseq (elt sequence)
371 (unless (seq-contains result elt testfn)
372 (setq result (cons elt result))))
375 (cl-defgeneric seq-mapcat (function sequence &optional type)
376 "Concatenate the result of applying FUNCTION to each element of SEQUENCE.
377 The result is a sequence of type TYPE, or a list if TYPE is nil."
378 (apply #'seq-concatenate (or type 'list)
379 (seq-map function sequence)))
381 (cl-defgeneric seq-partition (sequence n)
382 "Return a list of the elements of SEQUENCE grouped into sub-sequences of length N.
383 The last sequence may contain less than N elements. If N is a
384 negative integer or 0, nil is returned."
387 (while (not (seq-empty-p sequence))
388 (push (seq-take sequence n) result)
389 (setq sequence (seq-drop sequence n)))
392 (cl-defgeneric seq-intersection (sequence1 sequence2 &optional testfn)
393 "Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2.
394 Equality is defined by TESTFN if non-nil or by `equal' if nil."
395 (seq-reduce (lambda (acc elt)
396 (if (seq-contains sequence2 elt testfn)
399 (seq-reverse sequence1)
402 (cl-defgeneric seq-difference (sequence1 sequence2 &optional testfn)
403 "Return a list of the elements that appear in SEQUENCE1 but not in SEQUENCE2.
404 Equality is defined by TESTFN if non-nil or by `equal' if nil."
405 (seq-reduce (lambda (acc elt)
406 (if (not (seq-contains sequence2 elt testfn))
409 (seq-reverse sequence1)
412 (cl-defgeneric seq-group-by (function sequence)
413 "Apply FUNCTION to each element of SEQUENCE.
414 Separate the elements of SEQUENCE into an alist using the results as
415 keys. Keys are compared using `equal'."
418 (let* ((key (funcall function elt))
419 (cell (assoc key acc)))
421 (setcdr cell (push elt (cdr cell)))
422 (push (list key elt) acc))
424 (seq-reverse sequence)
427 (cl-defgeneric seq-min (sequence)
428 "Return the smallest element of SEQUENCE.
429 SEQUENCE must be a sequence of numbers or markers."
430 (apply #'min (seq-into sequence 'list)))
432 (cl-defgeneric seq-max (sequence)
433 "Return the largest element of SEQUENCE.
434 SEQUENCE must be a sequence of numbers or markers."
435 (apply #'max (seq-into sequence 'list)))
437 (defun seq--count-successive (pred sequence)
438 "Return the number of successive elements for which (PRED element) is non-nil in SEQUENCE."
440 (len (seq-length sequence)))
441 (while (and (< n len)
442 (funcall pred (seq-elt sequence n)))
446 ;;; Optimized implementations for lists
448 (cl-defmethod seq-drop ((list list) n)
449 "Optimized implementation of `seq-drop' for lists."
452 (cl-defmethod seq-take ((list list) n)
453 "Optimized implementation of `seq-take' for lists."
455 (while (and list (> n 0))
457 (push (pop list) result))
460 (cl-defmethod seq-drop-while (pred (list list))
461 "Optimized implementation of `seq-drop-while' for lists."
462 (while (and list (funcall pred (car list)))
463 (setq list (cdr list)))
466 (cl-defmethod seq-empty-p ((list list))
467 "Optimized implementation of `seq-empty-p' for lists."
471 (defun seq--into-list (sequence)
472 "Concatenate the elements of SEQUENCE into a list."
475 (append sequence nil)))
477 (defun seq--into-vector (sequence)
478 "Concatenate the elements of SEQUENCE into a vector."
479 (if (vectorp sequence)
483 (defun seq--into-string (sequence)
484 "Concatenate the elements of SEQUENCE into a string."
485 (if (stringp sequence)
489 (defun seq--make-pcase-bindings (args)
490 "Return a list of bindings of the variables in ARGS to the elements of a sequence."
494 (seq-doseq (name args)
498 (progn (push `(app (pcase--flip seq-drop ,index)
499 ,(seq--elt-safe args (1+ index)))
501 (setq rest-marker t)))
503 (push `(app (pcase--flip seq--elt-safe ,index) ,name) bindings))))
504 (setq index (1+ index)))
507 (defun seq--make-pcase-patterns (args)
508 "Return a list of `(seq ...)' pcase patterns from the argument list ARGS."
510 (seq-map (lambda (elt)
512 (seq--make-pcase-patterns elt)
516 ;; TODO: make public?
517 (defun seq--elt-safe (sequence n)
518 "Return element of SEQUENCE at the index N.
519 If no element is found, return nil."
520 (ignore-errors (seq-elt sequence n))))
522 (cl-defgeneric seq-random-elt (sequence)
523 "Return a random element from SEQUENCE.
524 Signal an error if SEQUENCE is empty."
525 (if (seq-empty-p sequence)
526 (error "Sequence cannot be empty")
527 (seq-elt sequence (random (seq-length sequence)))))
530 ;;; seq-25.el ends here