2014-12-16 23:42:30 +00:00
|
|
|
|
;;; seq.el --- Sequence manipulation functions -*- lexical-binding: t -*-
|
|
|
|
|
|
2019-01-01 00:59:58 +00:00
|
|
|
|
;; Copyright (C) 2014-2019 Free Software Foundation, Inc.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-02-06 14:52:23 +00:00
|
|
|
|
;; Author: Nicolas Petton <nicolas@petton.fr>
|
2014-12-16 23:42:30 +00:00
|
|
|
|
;; Keywords: sequences
|
2018-12-18 08:42:50 +00:00
|
|
|
|
;; Version: 2.21
|
2015-03-09 11:46:29 +00:00
|
|
|
|
;; Package: seq
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2019-05-25 20:43:06 +00:00
|
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
|
|
|
|
|
2014-12-16 23:42:30 +00:00
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; Sequence-manipulation functions that complement basic functions
|
|
|
|
|
;; provided by subr.el.
|
|
|
|
|
;;
|
|
|
|
|
;; All functions are prefixed with "seq-".
|
|
|
|
|
;;
|
|
|
|
|
;; All provided functions work on lists, strings and vectors.
|
|
|
|
|
;;
|
2015-03-25 08:21:14 +00:00
|
|
|
|
;; Functions taking a predicate or iterating over a sequence using a
|
|
|
|
|
;; function as argument take the function as their first argument and
|
2014-12-16 23:42:30 +00:00
|
|
|
|
;; the sequence as their second argument. All other functions take
|
|
|
|
|
;; the sequence as their first argument.
|
|
|
|
|
;;
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; While seq.el version 1.8 is in GNU ELPA for convenience, seq.el
|
|
|
|
|
;; version 2.0 requires Emacs>=25.1.
|
|
|
|
|
;;
|
|
|
|
|
;; seq.el can be extended to support new type of sequences. Here are
|
|
|
|
|
;; the generic functions that must be implemented by new seq types:
|
|
|
|
|
;; - `seq-elt'
|
|
|
|
|
;; - `seq-length'
|
|
|
|
|
;; - `seq-do'
|
2015-11-11 17:18:32 +00:00
|
|
|
|
;; - `seqp'
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; - `seq-subseq'
|
2015-08-26 22:21:38 +00:00
|
|
|
|
;; - `seq-into-sequence'
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; - `seq-copy'
|
|
|
|
|
;; - `seq-into'
|
|
|
|
|
;;
|
Fix obsolete ‘test/automated’ references
* Makefile.in (mostlyclean, clean, maybeclean_dirs, distclean)
(bootstrap-clean, maintainer-clean):
Clean ‘test’, not ‘test/automated’. Test for existence of
subdirectory only for ‘test’, not for directories that should
always exist.
* admin/MAINTAINERS, etc/TODO, lisp/emacs-lisp/bytecomp.el:
* lisp/emacs-lisp/seq.el, lisp/emacs-lisp/thunk.el:
* lisp/man.el (Man-parse-man-k):
* lisp/url/url-domsuf.el, make-dist:
* test/file-organization.org:
Fix obsolete references to test/automated.
2017-03-27 18:22:54 +00:00
|
|
|
|
;; All functions are tested in test/lisp/emacs-lisp/seq-tests.el
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(eval-when-compile (require 'cl-generic))
|
|
|
|
|
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(defmacro seq-doseq (spec &rest body)
|
|
|
|
|
"Loop over a sequence.
|
2015-10-26 20:51:30 +00:00
|
|
|
|
Evaluate BODY with VAR bound to each element of SEQUENCE, in turn.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 21:16:27 +00:00
|
|
|
|
Similar to `dolist' but can be applied to lists, strings, and vectors.
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
\(fn (VAR SEQUENCE) BODY...)"
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(declare (indent 1) (debug ((symbolp form &optional form) body)))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
`(seq-do (lambda (,(car spec))
|
|
|
|
|
,@body)
|
|
|
|
|
,(cadr spec)))
|
|
|
|
|
|
2015-10-14 07:37:59 +00:00
|
|
|
|
(pcase-defmacro seq (&rest patterns)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Build a `pcase' pattern that matches elements of SEQUENCE.
|
2015-10-14 07:37:59 +00:00
|
|
|
|
|
2015-10-26 21:16:27 +00:00
|
|
|
|
The `pcase' pattern will match each element of PATTERNS against the
|
|
|
|
|
corresponding element of SEQUENCE.
|
2015-10-14 07:37:59 +00:00
|
|
|
|
|
2015-10-26 21:16:27 +00:00
|
|
|
|
Extra elements of the sequence are ignored if fewer PATTERNS are
|
|
|
|
|
given, and the match does not fail."
|
2015-11-11 17:18:32 +00:00
|
|
|
|
`(and (pred seqp)
|
2015-10-14 07:37:59 +00:00
|
|
|
|
,@(seq--make-pcase-bindings patterns)))
|
2015-05-10 18:19:38 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(defmacro seq-let (args sequence &rest body)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Bind the variables in ARGS to the elements of SEQUENCE, then evaluate BODY.
|
2015-05-10 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
ARGS can also include the `&rest' marker followed by a variable
|
2015-10-26 20:51:30 +00:00
|
|
|
|
name to be bound to the rest of SEQUENCE."
|
2016-10-24 11:15:05 +00:00
|
|
|
|
(declare (indent 2) (debug (sexp form body)))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
`(pcase-let ((,(seq--make-pcase-patterns args) ,sequence))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
,@body))
|
|
|
|
|
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
;;; Basic seq functions that have to be implemented by new sequence types
|
|
|
|
|
(cl-defgeneric seq-elt (sequence n)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Return Nth element of SEQUENCE."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(elt sequence n))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
;; Default gv setters for `seq-elt'.
|
2015-08-25 07:42:17 +00:00
|
|
|
|
;; It can be a good idea for new sequence implementations to provide a
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; "gv-setter" for `seq-elt'.
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defmethod (setf seq-elt) (store (sequence array) n)
|
|
|
|
|
(aset sequence n store))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defmethod (setf seq-elt) (store (sequence cons) n)
|
|
|
|
|
(setcar (nthcdr n sequence) store))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-length (sequence)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Return the number of elements of SEQUENCE."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(length sequence))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2018-12-18 08:42:50 +00:00
|
|
|
|
(defun seq-first (sequence)
|
|
|
|
|
"Return the first element of SEQUENCE."
|
|
|
|
|
(seq-elt sequence 0))
|
|
|
|
|
|
|
|
|
|
(defun seq-rest (sequence)
|
|
|
|
|
"Return a sequence of the elements of SEQUENCE except the first one."
|
|
|
|
|
(seq-drop sequence 1))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-do (function sequence)
|
|
|
|
|
"Apply FUNCTION to each element of SEQUENCE, presumably for side effects.
|
|
|
|
|
Return SEQUENCE."
|
|
|
|
|
(mapc function sequence))
|
2015-05-10 18:19:38 +00:00
|
|
|
|
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(defalias 'seq-each #'seq-do)
|
2015-05-10 18:19:38 +00:00
|
|
|
|
|
2016-06-18 07:32:18 +00:00
|
|
|
|
(defun seq-do-indexed (function sequence)
|
|
|
|
|
"Apply FUNCTION to each element of SEQUENCE and return nil.
|
|
|
|
|
Unlike `seq-map', FUNCTION takes two arguments: the element of
|
|
|
|
|
the sequence, and its index within the sequence."
|
|
|
|
|
(let ((index 0))
|
|
|
|
|
(seq-do (lambda (elt)
|
|
|
|
|
(funcall function elt index)
|
|
|
|
|
(setq index (1+ index)))
|
|
|
|
|
sequence)))
|
|
|
|
|
|
2017-04-09 09:06:44 +00:00
|
|
|
|
(cl-defgeneric seqp (object)
|
|
|
|
|
"Return non-nil if OBJECT is a sequence, nil otherwise."
|
|
|
|
|
(sequencep object))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-copy (sequence)
|
|
|
|
|
"Return a shallow copy of SEQUENCE."
|
|
|
|
|
(copy-sequence sequence))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-subseq (sequence start &optional end)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Return the sequence of elements of SEQUENCE from START to END.
|
2016-09-01 14:46:14 +00:00
|
|
|
|
END is exclusive.
|
2015-10-26 21:16:27 +00:00
|
|
|
|
|
|
|
|
|
If END is omitted, it defaults to the length of the sequence. If
|
|
|
|
|
START or END is negative, it counts from the end. Signal an
|
|
|
|
|
error if START or END are outside of the sequence (i.e too large
|
|
|
|
|
if positive or too small if negative)."
|
2019-10-27 17:25:00 +00:00
|
|
|
|
(cond
|
|
|
|
|
((or (stringp sequence) (vectorp sequence)) (substring sequence start end))
|
|
|
|
|
((listp sequence)
|
|
|
|
|
(let (len
|
|
|
|
|
(errtext (format "Bad bounding indices: %s, %s" start end)))
|
|
|
|
|
(and end (< end 0) (setq end (+ end (setq len (length sequence)))))
|
|
|
|
|
(if (< start 0) (setq start (+ start (or len (setq len (length sequence))))))
|
|
|
|
|
(unless (>= start 0)
|
|
|
|
|
(error "%s" errtext))
|
|
|
|
|
(when (> start 0)
|
|
|
|
|
(setq sequence (nthcdr (1- start) sequence))
|
|
|
|
|
(or sequence (error "%s" errtext))
|
|
|
|
|
(setq sequence (cdr sequence)))
|
|
|
|
|
(if end
|
|
|
|
|
(let ((res nil))
|
|
|
|
|
(while (and (>= (setq end (1- end)) start) sequence)
|
|
|
|
|
(push (pop sequence) res))
|
|
|
|
|
(or (= (1+ end) start) (error "%s" errtext))
|
|
|
|
|
(nreverse res))
|
|
|
|
|
(copy-sequence sequence))))
|
|
|
|
|
(t (error "Unsupported sequence: %s" sequence))))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-map (function sequence)
|
|
|
|
|
"Return the result of applying FUNCTION to each element of SEQUENCE."
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(let (result)
|
|
|
|
|
(seq-do (lambda (elt)
|
|
|
|
|
(push (funcall function elt) result))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence)
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(nreverse result)))
|
|
|
|
|
|
2016-02-14 09:25:10 +00:00
|
|
|
|
(defun seq-map-indexed (function sequence)
|
|
|
|
|
"Return the result of applying FUNCTION to each element of SEQUENCE.
|
|
|
|
|
Unlike `seq-map', FUNCTION takes two arguments: the element of
|
|
|
|
|
the sequence, and its index within the sequence."
|
|
|
|
|
(let ((index 0))
|
|
|
|
|
(seq-map (lambda (elt)
|
|
|
|
|
(prog1
|
|
|
|
|
(funcall function elt index)
|
|
|
|
|
(setq index (1+ index))))
|
|
|
|
|
sequence)))
|
|
|
|
|
|
|
|
|
|
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; faster implementation for sequences (sequencep)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defmethod seq-map (function (sequence sequence))
|
|
|
|
|
(mapcar function sequence))
|
2015-05-01 17:30:56 +00:00
|
|
|
|
|
2015-10-28 14:27:39 +00:00
|
|
|
|
(cl-defgeneric seq-mapn (function sequence &rest sequences)
|
|
|
|
|
"Like `seq-map' but FUNCTION is mapped over all SEQUENCES.
|
|
|
|
|
The arity of FUNCTION must match the number of SEQUENCES, and the
|
|
|
|
|
mapping stops on the shortest sequence.
|
|
|
|
|
Return a list of the results.
|
|
|
|
|
|
|
|
|
|
\(fn FUNCTION SEQUENCES...)"
|
|
|
|
|
(let ((result nil)
|
2016-12-15 09:24:57 +00:00
|
|
|
|
(sequences (seq-map (lambda (s)
|
2016-12-16 10:18:04 +00:00
|
|
|
|
(seq-into s 'list))
|
2015-10-28 14:27:39 +00:00
|
|
|
|
(cons sequence sequences))))
|
|
|
|
|
(while (not (memq nil sequences))
|
|
|
|
|
(push (apply function (seq-map #'car sequences)) result)
|
|
|
|
|
(setq sequences (seq-map #'cdr sequences)))
|
|
|
|
|
(nreverse result)))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-drop (sequence n)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Remove the first N elements of SEQUENCE and return the result.
|
2015-10-26 20:51:30 +00:00
|
|
|
|
The result is a sequence of the same type as SEQUENCE.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
If N is a negative integer or zero, SEQUENCE is returned."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(if (<= n 0)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence
|
|
|
|
|
(let ((length (seq-length sequence)))
|
|
|
|
|
(seq-subseq sequence (min n length) length))))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2019-10-29 22:31:11 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-take (sequence n)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Take the first N elements of SEQUENCE and return the result.
|
2015-10-26 20:51:30 +00:00
|
|
|
|
The result is a sequence of the same type as SEQUENCE.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
If N is a negative integer or zero, an empty sequence is
|
|
|
|
|
returned."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-subseq sequence 0 (min (max n 0) (seq-length sequence))))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-drop-while (pred sequence)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Remove the successive elements of SEQUENCE for which PRED returns non-nil.
|
|
|
|
|
PRED is a function of one argument. The result is a sequence of
|
|
|
|
|
the same type as SEQUENCE."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-drop sequence (seq--count-successive pred sequence)))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-take-while (pred sequence)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Take the successive elements of SEQUENCE for which PRED returns non-nil.
|
|
|
|
|
PRED is a function of one argument. The result is a sequence of
|
|
|
|
|
the same type as SEQUENCE."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-take sequence (seq--count-successive pred sequence)))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-empty-p (sequence)
|
|
|
|
|
"Return non-nil if the SEQUENCE is empty, nil otherwise."
|
|
|
|
|
(= 0 (seq-length sequence)))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-sort (pred sequence)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Sort SEQUENCE using PRED as comparison function.
|
2015-10-26 20:51:30 +00:00
|
|
|
|
The result is a sequence of the same type as SEQUENCE."
|
|
|
|
|
(let ((result (seq-sort pred (append sequence nil))))
|
|
|
|
|
(seq-into result (type-of sequence))))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
(cl-defmethod seq-sort (pred (list list))
|
|
|
|
|
(sort (seq-copy list) pred))
|
|
|
|
|
|
2019-10-26 22:16:10 +00:00
|
|
|
|
;;;###autoload
|
2016-03-29 07:19:32 +00:00
|
|
|
|
(defun seq-sort-by (function pred sequence)
|
|
|
|
|
"Sort SEQUENCE using PRED as a comparison function.
|
|
|
|
|
Elements of SEQUENCE are transformed by FUNCTION before being
|
|
|
|
|
sorted. FUNCTION must be a function of one argument."
|
|
|
|
|
(seq-sort (lambda (a b)
|
|
|
|
|
(funcall pred
|
|
|
|
|
(funcall function a)
|
|
|
|
|
(funcall function b)))
|
|
|
|
|
sequence))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-reverse (sequence)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Return a sequence with elements of SEQUENCE in reverse order."
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(let ((result '()))
|
|
|
|
|
(seq-map (lambda (elt)
|
|
|
|
|
(push elt result))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence)
|
|
|
|
|
(seq-into result (type-of sequence))))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
;; faster implementation for sequences (sequencep)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defmethod seq-reverse ((sequence sequence))
|
|
|
|
|
(reverse sequence))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-concatenate (type &rest sequences)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Concatenate SEQUENCES into a single sequence of type TYPE.
|
2015-08-14 20:33:10 +00:00
|
|
|
|
TYPE must be one of following symbols: vector, string or list.
|
|
|
|
|
|
|
|
|
|
\n(fn TYPE SEQUENCE...)"
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(apply #'cl-concatenate type (seq-map #'seq-into-sequence sequences)))
|
2015-08-26 22:21:38 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-into-sequence (sequence)
|
|
|
|
|
"Convert SEQUENCE into a sequence.
|
2015-08-26 22:21:38 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
The default implementation is to signal an error if SEQUENCE is not a
|
2015-08-26 22:21:38 +00:00
|
|
|
|
sequence, specific functions should be implemented for new types
|
2015-10-26 20:51:30 +00:00
|
|
|
|
of sequence."
|
|
|
|
|
(unless (sequencep sequence)
|
|
|
|
|
(error "Cannot convert %S into a sequence" sequence))
|
|
|
|
|
sequence)
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-into (sequence type)
|
2015-10-26 21:16:27 +00:00
|
|
|
|
"Concatenate the elements of SEQUENCE into a sequence of type TYPE.
|
|
|
|
|
TYPE can be one of the following symbols: vector, string or
|
|
|
|
|
list."
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(pcase type
|
2016-12-16 10:18:04 +00:00
|
|
|
|
(`vector (seq--into-vector sequence))
|
|
|
|
|
(`string (seq--into-string sequence))
|
|
|
|
|
(`list (seq--into-list sequence))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(_ (error "Not a sequence type name: %S" type))))
|
|
|
|
|
|
2019-10-29 22:31:11 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-filter (pred sequence)
|
|
|
|
|
"Return a list of all the elements for which (PRED element) is non-nil in SEQUENCE."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(let ((exclude (make-symbol "exclude")))
|
|
|
|
|
(delq exclude (seq-map (lambda (elt)
|
|
|
|
|
(if (funcall pred elt)
|
|
|
|
|
elt
|
|
|
|
|
exclude))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence))))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2019-10-26 22:16:10 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-remove (pred sequence)
|
|
|
|
|
"Return a list of all the elements for which (PRED element) is nil in SEQUENCE."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(seq-filter (lambda (elt) (not (funcall pred elt)))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2019-11-03 21:44:44 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-reduce (function sequence initial-value)
|
|
|
|
|
"Reduce the function FUNCTION across SEQUENCE, starting with INITIAL-VALUE.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
|
|
|
|
Return the result of calling FUNCTION with INITIAL-VALUE and the
|
2015-10-26 20:51:30 +00:00
|
|
|
|
first element of SEQUENCE, then calling FUNCTION with that result and
|
|
|
|
|
the second element of SEQUENCE, then with that result and the third
|
|
|
|
|
element of SEQUENCE, etc.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
If SEQUENCE is empty, return INITIAL-VALUE and FUNCTION is not called."
|
|
|
|
|
(if (seq-empty-p sequence)
|
2014-12-16 23:42:30 +00:00
|
|
|
|
initial-value
|
|
|
|
|
(let ((acc initial-value))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(setq acc (funcall function acc elt)))
|
|
|
|
|
acc)))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-every-p (pred sequence)
|
|
|
|
|
"Return non-nil if (PRED element) is non-nil for all elements of SEQUENCE."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(catch 'seq--break
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(or (funcall pred elt)
|
|
|
|
|
(throw 'seq--break nil)))
|
|
|
|
|
t))
|
|
|
|
|
|
2019-12-02 23:40:12 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-some (pred sequence)
|
2016-12-07 12:32:46 +00:00
|
|
|
|
"Return non-nil if PRED is satisfied for at least one element of SEQUENCE.
|
|
|
|
|
If so, return the first non-nil value returned by PRED."
|
2015-09-05 22:05:52 +00:00
|
|
|
|
(catch 'seq--break
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2015-09-05 22:26:17 +00:00
|
|
|
|
(let ((result (funcall pred elt)))
|
|
|
|
|
(when result
|
|
|
|
|
(throw 'seq--break result))))
|
2015-09-05 22:05:52 +00:00
|
|
|
|
nil))
|
|
|
|
|
|
2019-10-07 03:00:16 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-find (pred sequence &optional default)
|
|
|
|
|
"Return the first element for which (PRED element) is non-nil in SEQUENCE.
|
2015-09-13 14:18:24 +00:00
|
|
|
|
If no element is found, return DEFAULT.
|
2015-09-10 21:49:56 +00:00
|
|
|
|
|
2015-09-13 14:18:24 +00:00
|
|
|
|
Note that `seq-find' has an ambiguity if the found element is
|
|
|
|
|
identical to DEFAULT, as it cannot be known if an element was
|
|
|
|
|
found or not."
|
2015-09-10 21:49:56 +00:00
|
|
|
|
(catch 'seq--break
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2015-09-10 21:49:56 +00:00
|
|
|
|
(when (funcall pred elt)
|
|
|
|
|
(throw 'seq--break elt)))
|
2015-09-13 14:18:24 +00:00
|
|
|
|
default))
|
2015-09-10 21:49:56 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-count (pred sequence)
|
|
|
|
|
"Return the number of elements for which (PRED element) is non-nil in SEQUENCE."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(let ((count 0))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(when (funcall pred elt)
|
|
|
|
|
(setq count (+ 1 count))))
|
|
|
|
|
count))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-contains (sequence elt &optional testfn)
|
2019-03-20 20:44:01 +00:00
|
|
|
|
(declare (obsolete seq-contains-p "27.1"))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
"Return the first element in SEQUENCE that is equal to ELT.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
2015-09-05 22:05:52 +00:00
|
|
|
|
(seq-some (lambda (e)
|
2016-06-18 08:07:26 +00:00
|
|
|
|
(when (funcall (or testfn #'equal) elt e)
|
|
|
|
|
e))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
sequence))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
|
2019-03-20 20:44:01 +00:00
|
|
|
|
(cl-defgeneric seq-contains-p (sequence elt &optional testfn)
|
|
|
|
|
"Return non-nil if SEQUENCE contains an element equal to ELT.
|
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
|
|
|
|
(catch 'seq--break
|
|
|
|
|
(seq-doseq (e sequence)
|
|
|
|
|
(when (funcall (or testfn #'equal) e elt)
|
|
|
|
|
(throw 'seq--break t)))
|
|
|
|
|
nil))
|
|
|
|
|
|
2017-04-17 09:01:39 +00:00
|
|
|
|
(cl-defgeneric seq-set-equal-p (sequence1 sequence2 &optional testfn)
|
|
|
|
|
"Return non-nil if SEQUENCE1 and SEQUENCE2 contain the same elements, regardless of order.
|
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
2019-03-20 20:44:01 +00:00
|
|
|
|
(and (seq-every-p (lambda (item1) (seq-contains-p sequence2 item1 testfn)) sequence1)
|
|
|
|
|
(seq-every-p (lambda (item2) (seq-contains-p sequence1 item2 testfn)) sequence2)))
|
2017-04-17 09:01:39 +00:00
|
|
|
|
|
2019-10-20 11:10:59 +00:00
|
|
|
|
;;;###autoload
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-position (sequence elt &optional testfn)
|
|
|
|
|
"Return the index of the first element in SEQUENCE that is equal to ELT.
|
2015-10-19 22:11:30 +00:00
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
|
|
|
|
(let ((index 0))
|
|
|
|
|
(catch 'seq--break
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (e sequence)
|
2015-10-19 22:11:30 +00:00
|
|
|
|
(when (funcall (or testfn #'equal) e elt)
|
|
|
|
|
(throw 'seq--break index))
|
|
|
|
|
(setq index (1+ index)))
|
|
|
|
|
nil)))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-uniq (sequence &optional testfn)
|
|
|
|
|
"Return a list of the elements of SEQUENCE with duplicates removed.
|
2014-12-16 23:42:30 +00:00
|
|
|
|
TESTFN is used to compare elements, or `equal' if TESTFN is nil."
|
|
|
|
|
(let ((result '()))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-doseq (elt sequence)
|
2019-03-20 20:44:01 +00:00
|
|
|
|
(unless (seq-contains-p result elt testfn)
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(setq result (cons elt result))))
|
|
|
|
|
(nreverse result)))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-mapcat (function sequence &optional type)
|
|
|
|
|
"Concatenate the result of applying FUNCTION to each element of SEQUENCE.
|
2015-02-06 14:52:23 +00:00
|
|
|
|
The result is a sequence of type TYPE, or a list if TYPE is nil."
|
|
|
|
|
(apply #'seq-concatenate (or type 'list)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-map function sequence)))
|
2015-02-06 14:52:23 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-partition (sequence n)
|
|
|
|
|
"Return a list of the elements of SEQUENCE grouped into sub-sequences of length N.
|
2015-02-06 14:55:57 +00:00
|
|
|
|
The last sequence may contain less than N elements. If N is a
|
|
|
|
|
negative integer or 0, nil is returned."
|
|
|
|
|
(unless (< n 1)
|
|
|
|
|
(let ((result '()))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(while (not (seq-empty-p sequence))
|
|
|
|
|
(push (seq-take sequence n) result)
|
|
|
|
|
(setq sequence (seq-drop sequence n)))
|
2015-02-06 14:55:57 +00:00
|
|
|
|
(nreverse result))))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-intersection (sequence1 sequence2 &optional testfn)
|
|
|
|
|
"Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2.
|
2015-04-14 22:33:27 +00:00
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
|
|
|
|
(seq-reduce (lambda (acc elt)
|
2019-03-20 20:44:01 +00:00
|
|
|
|
(if (seq-contains-p sequence2 elt testfn)
|
2015-04-14 22:33:27 +00:00
|
|
|
|
(cons elt acc)
|
|
|
|
|
acc))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-reverse sequence1)
|
2015-04-14 22:33:27 +00:00
|
|
|
|
'()))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-difference (sequence1 sequence2 &optional testfn)
|
|
|
|
|
"Return a list of the elements that appear in SEQUENCE1 but not in SEQUENCE2.
|
2015-04-14 22:33:27 +00:00
|
|
|
|
Equality is defined by TESTFN if non-nil or by `equal' if nil."
|
|
|
|
|
(seq-reduce (lambda (acc elt)
|
2019-03-21 20:07:55 +00:00
|
|
|
|
(if (seq-contains-p sequence2 elt testfn)
|
|
|
|
|
acc
|
|
|
|
|
(cons elt acc)))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-reverse sequence1)
|
2015-04-14 22:33:27 +00:00
|
|
|
|
'()))
|
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-group-by (function sequence)
|
|
|
|
|
"Apply FUNCTION to each element of SEQUENCE.
|
|
|
|
|
Separate the elements of SEQUENCE into an alist using the results as
|
2015-02-06 14:55:57 +00:00
|
|
|
|
keys. Keys are compared using `equal'."
|
2015-02-09 12:14:52 +00:00
|
|
|
|
(seq-reduce
|
|
|
|
|
(lambda (acc elt)
|
|
|
|
|
(let* ((key (funcall function elt))
|
|
|
|
|
(cell (assoc key acc)))
|
|
|
|
|
(if cell
|
|
|
|
|
(setcdr cell (push elt (cdr cell)))
|
|
|
|
|
(push (list key elt) acc))
|
|
|
|
|
acc))
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(seq-reverse sequence)
|
2015-02-09 12:14:52 +00:00
|
|
|
|
nil))
|
2015-02-06 14:55:57 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-min (sequence)
|
|
|
|
|
"Return the smallest element of SEQUENCE.
|
|
|
|
|
SEQUENCE must be a sequence of numbers or markers."
|
|
|
|
|
(apply #'min (seq-into sequence 'list)))
|
2015-06-30 16:29:32 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(cl-defgeneric seq-max (sequence)
|
|
|
|
|
"Return the largest element of SEQUENCE.
|
|
|
|
|
SEQUENCE must be a sequence of numbers or markers."
|
|
|
|
|
(apply #'max (seq-into sequence 'list)))
|
2015-06-30 16:29:32 +00:00
|
|
|
|
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(defun seq--count-successive (pred sequence)
|
|
|
|
|
"Return the number of successive elements for which (PRED element) is non-nil in SEQUENCE."
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(let ((n 0)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(len (seq-length sequence)))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(while (and (< n len)
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(funcall pred (seq-elt sequence n)))
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(setq n (+ 1 n)))
|
|
|
|
|
n))
|
|
|
|
|
|
2015-05-12 19:57:18 +00:00
|
|
|
|
(defun seq--make-pcase-bindings (args)
|
|
|
|
|
"Return a list of bindings of the variables in ARGS to the elements of a sequence."
|
|
|
|
|
(let ((bindings '())
|
|
|
|
|
(index 0)
|
2015-05-10 18:19:38 +00:00
|
|
|
|
(rest-marker nil))
|
|
|
|
|
(seq-doseq (name args)
|
|
|
|
|
(unless rest-marker
|
|
|
|
|
(pcase name
|
|
|
|
|
(`&rest
|
2015-05-12 19:57:18 +00:00
|
|
|
|
(progn (push `(app (pcase--flip seq-drop ,index)
|
2015-05-10 18:19:38 +00:00
|
|
|
|
,(seq--elt-safe args (1+ index)))
|
|
|
|
|
bindings)
|
|
|
|
|
(setq rest-marker t)))
|
2015-06-16 21:49:17 +00:00
|
|
|
|
(_
|
2015-05-12 19:57:18 +00:00
|
|
|
|
(push `(app (pcase--flip seq--elt-safe ,index) ,name) bindings))))
|
2015-05-10 18:19:38 +00:00
|
|
|
|
(setq index (1+ index)))
|
|
|
|
|
bindings))
|
2015-05-01 17:30:56 +00:00
|
|
|
|
|
2015-05-12 19:57:18 +00:00
|
|
|
|
(defun seq--make-pcase-patterns (args)
|
|
|
|
|
"Return a list of `(seq ...)' pcase patterns from the argument list ARGS."
|
|
|
|
|
(cons 'seq
|
|
|
|
|
(seq-map (lambda (elt)
|
2015-11-11 17:18:32 +00:00
|
|
|
|
(if (seqp elt)
|
2015-05-12 19:57:18 +00:00
|
|
|
|
(seq--make-pcase-patterns elt)
|
|
|
|
|
elt))
|
|
|
|
|
args)))
|
2015-05-10 18:19:38 +00:00
|
|
|
|
|
2015-08-14 20:33:10 +00:00
|
|
|
|
;; TODO: make public?
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(defun seq--elt-safe (sequence n)
|
|
|
|
|
"Return element of SEQUENCE at the index N.
|
2015-05-01 17:30:56 +00:00
|
|
|
|
If no element is found, return nil."
|
2015-10-26 20:51:30 +00:00
|
|
|
|
(ignore-errors (seq-elt sequence n)))
|
2016-10-21 05:53:08 +00:00
|
|
|
|
|
|
|
|
|
(cl-defgeneric seq-random-elt (sequence)
|
|
|
|
|
"Return a random element from SEQUENCE.
|
2016-10-25 15:06:03 +00:00
|
|
|
|
Signal an error if SEQUENCE is empty."
|
2016-10-21 05:53:08 +00:00
|
|
|
|
(if (seq-empty-p sequence)
|
|
|
|
|
(error "Sequence cannot be empty")
|
|
|
|
|
(seq-elt sequence (random (seq-length sequence)))))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;; Optimized implementations for lists
|
|
|
|
|
|
|
|
|
|
(cl-defmethod seq-drop ((list list) n)
|
|
|
|
|
"Optimized implementation of `seq-drop' for lists."
|
2016-06-12 10:37:16 +00:00
|
|
|
|
(nthcdr n list))
|
2015-08-14 20:33:10 +00:00
|
|
|
|
|
|
|
|
|
(cl-defmethod seq-take ((list list) n)
|
|
|
|
|
"Optimized implementation of `seq-take' for lists."
|
|
|
|
|
(let ((result '()))
|
|
|
|
|
(while (and list (> n 0))
|
|
|
|
|
(setq n (1- n))
|
|
|
|
|
(push (pop list) result))
|
|
|
|
|
(nreverse result)))
|
|
|
|
|
|
|
|
|
|
(cl-defmethod seq-drop-while (pred (list list))
|
2015-09-09 13:09:31 +00:00
|
|
|
|
"Optimized implementation of `seq-drop-while' for lists."
|
2015-08-14 20:33:10 +00:00
|
|
|
|
(while (and list (funcall pred (car list)))
|
|
|
|
|
(setq list (cdr list)))
|
|
|
|
|
list)
|
|
|
|
|
|
|
|
|
|
(cl-defmethod seq-empty-p ((list list))
|
|
|
|
|
"Optimized implementation of `seq-empty-p' for lists."
|
|
|
|
|
(null list))
|
|
|
|
|
|
2015-04-14 22:33:27 +00:00
|
|
|
|
|
2016-12-16 10:18:04 +00:00
|
|
|
|
(defun seq--into-list (sequence)
|
|
|
|
|
"Concatenate the elements of SEQUENCE into a list."
|
|
|
|
|
(if (listp sequence)
|
|
|
|
|
sequence
|
|
|
|
|
(append sequence nil)))
|
|
|
|
|
|
|
|
|
|
(defun seq--into-vector (sequence)
|
|
|
|
|
"Concatenate the elements of SEQUENCE into a vector."
|
|
|
|
|
(if (vectorp sequence)
|
|
|
|
|
sequence
|
|
|
|
|
(vconcat sequence)))
|
|
|
|
|
|
|
|
|
|
(defun seq--into-string (sequence)
|
|
|
|
|
"Concatenate the elements of SEQUENCE into a string."
|
|
|
|
|
(if (stringp sequence)
|
|
|
|
|
sequence
|
|
|
|
|
(concat sequence)))
|
|
|
|
|
|
2015-05-10 18:19:38 +00:00
|
|
|
|
(defun seq--activate-font-lock-keywords ()
|
|
|
|
|
"Activate font-lock keywords for some symbols defined in seq."
|
|
|
|
|
(font-lock-add-keywords 'emacs-lisp-mode
|
|
|
|
|
'("\\<seq-doseq\\>" "\\<seq-let\\>")))
|
|
|
|
|
|
2015-04-24 20:11:35 +00:00
|
|
|
|
(unless (fboundp 'elisp--font-lock-flush-elisp-buffers)
|
|
|
|
|
;; In Emacs≥25, (via elisp--font-lock-flush-elisp-buffers and a few others)
|
|
|
|
|
;; we automatically highlight macros.
|
2015-05-13 22:39:49 +00:00
|
|
|
|
(add-hook 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
|
2015-04-14 22:33:27 +00:00
|
|
|
|
|
2014-12-16 23:42:30 +00:00
|
|
|
|
(provide 'seq)
|
|
|
|
|
;;; seq.el ends here
|