]> code.delx.au - gnu-emacs-elpa/blob - packages/seq/seq.el
Add 'packages/hydra/' from commit 'cfac8bd9e73ea1e219250867b22881354fc2d56e'
[gnu-emacs-elpa] / packages / seq / seq.el
1 ;;; seq.el --- Sequence manipulation functions -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2014 Free Software Foundation, Inc.
4
5 ;; Author: Nicolas Petton <petton.nicolas@gmail.com>
6 ;; Keywords: sequences
7 ;; Version: 1.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 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, of 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 a sequence of 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) returns 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 (cond ((stringp seq) (concat result))
176 ((vectorp seq) (vconcat result))
177 (t (error "Unsupported sequence: %s" seq))))))
178
179 (defun seq-contains-p (seq elt &optional testfn)
180 "Return the first element in SEQ that equals to ELT.
181 Equality is defined by TESTFN if non-nil or by `equal' if nil."
182 (seq-some-p (lambda (e)
183 (funcall (or testfn #'equal) elt e))
184 seq))
185
186 (defun seq-uniq (seq &optional testfn)
187 "Return a list of the elements of SEQ with duplicates removed.
188 TESTFN is used to compare elements, or `equal' if TESTFN is nil."
189 (let ((result '()))
190 (seq-doseq (elt seq)
191 (unless (seq-contains-p result elt testfn)
192 (setq result (cons elt result))))
193 (nreverse result)))
194
195 (defun seq-subseq (seq start &optional end)
196 "Return the subsequence of SEQ from START to END.
197 If END is omitted, it defaults to the length of the sequence.
198 If START or END is negative, it counts from the end."
199 (cond ((or (stringp seq) (vectorp seq)) (substring seq start end))
200 ((listp seq)
201 (let (len)
202 (and end (< end 0) (setq end (+ end (setq len (seq-length seq)))))
203 (if (< start 0) (setq start (+ start (or len (setq len (seq-length seq))))))
204 (if (> start 0) (setq seq (nthcdr start seq)))
205 (if end
206 (let ((res nil))
207 (while (>= (setq end (1- end)) start)
208 (push (pop seq) res))
209 (nreverse res))
210 (seq-copy seq))))
211 (t (error "Unsupported sequence: %s" seq))))
212
213 (defun seq-concatenate (type &rest seqs)
214 "Concatenate, into a sequence of type TYPE, the sequences SEQS.
215 TYPE must be one of following symbols: vector, string or list.
216
217 \n(fn TYPE SEQUENCE...)"
218 (pcase type
219 (`vector (apply #'vconcat seqs))
220 (`string (apply #'concat seqs))
221 (`list (apply #'append (append seqs '(nil))))
222 (t (error "Not a sequence type name: %s" type))))
223
224 (defun seq--drop-list (list n)
225 "Optimized version of `seq-drop' for lists."
226 (while (and list (> n 0))
227 (setq list (cdr list)
228 n (1- n)))
229 list)
230
231 (defun seq--take-list (list n)
232 "Optimized version of `seq-take' for lists."
233 (let ((result '()))
234 (while (and list (> n 0))
235 (setq n (1- n))
236 (push (pop list) result))
237 (nreverse result)))
238
239 (defun seq--drop-while-list (pred list)
240 "Optimized version of `seq-drop-while' for lists."
241 (while (and list (funcall pred (car list)))
242 (setq list (cdr list)))
243 list)
244
245 (defun seq--take-while-list (pred list)
246 "Optimized version of `seq-take-while' for lists."
247 (let ((result '()))
248 (while (and list (funcall pred (car list)))
249 (push (pop list) result))
250 (nreverse result)))
251
252 (defun seq--count-successive (pred seq)
253 "Return the number of successive elements for which (PRED element) is non-nil in SEQ."
254 (let ((n 0)
255 (len (seq-length seq)))
256 (while (and (< n len)
257 (funcall pred (seq-elt seq n)))
258 (setq n (+ 1 n)))
259 n))
260
261 (defalias 'seq-copy #'copy-sequence)
262 (defalias 'seq-elt #'elt)
263 (defalias 'seq-reverse #'reverse)
264 (defalias 'seq-length #'length)
265 (defalias 'seq-do #'mapc)
266 (defalias 'seq-each #'seq-do)
267 (defalias 'seq-map #'mapcar)
268
269 (provide 'seq)
270 ;;; seq.el ends here