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