diff options
Diffstat (limited to 'lisp/emacs-lisp/seq.el')
-rw-r--r-- | lisp/emacs-lisp/seq.el | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 6c15463ad52..451ff196316 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el @@ -4,7 +4,7 @@ ;; Author: Nicolas Petton <nicolas@petton.fr> ;; Keywords: sequences -;; Version: 2.22 +;; Version: 2.23 ;; Package: seq ;; Maintainer: emacs-devel@gnu.org @@ -93,6 +93,14 @@ name to be bound to the rest of SEQUENCE." (declare (indent 2) (debug (sexp form body))) `(pcase-let ((,(seq--make-pcase-patterns args) ,sequence)) ,@body)) + +(defmacro seq-setq (args sequence) + "Assign to the variables in ARGS the elements of SEQUENCE. + +ARGS can also include the `&rest' marker followed by a variable +name to be bound to the rest of SEQUENCE." + (declare (debug (sexp form))) + `(pcase-setq ,(seq--make-pcase-patterns args) ,sequence)) ;;; Basic seq functions that have to be implemented by new sequence types @@ -394,14 +402,15 @@ found or not." (setq count (+ 1 count)))) count)) -(cl-defgeneric seq-contains (sequence elt &optional testfn) - "Return the first element in SEQUENCE that is equal to ELT. +(with-suppressed-warnings ((obsolete seq-contains)) + (cl-defgeneric seq-contains (sequence elt &optional testfn) + "Return the first element in SEQUENCE that is equal to ELT. Equality is defined by TESTFN if non-nil or by `equal' if nil." - (declare (obsolete seq-contains-p "27.1")) - (seq-some (lambda (e) - (when (funcall (or testfn #'equal) elt e) - e)) - sequence)) + (declare (obsolete seq-contains-p "27.1")) + (seq-some (lambda (e) + (when (funcall (or testfn #'equal) elt e) + e)) + sequence))) (cl-defgeneric seq-contains-p (sequence elt &optional testfn) "Return non-nil if SEQUENCE contains an element equal to ELT. @@ -459,6 +468,18 @@ negative integer or 0, nil is returned." (nreverse result)))) ;;;###autoload +(cl-defgeneric seq-union (sequence1 sequence2 &optional testfn) + "Return a list of all elements that appear in either SEQUENCE1 or SEQUENCE2. +Equality is defined by TESTFN if non-nil or by `equal' if nil." + (let* ((accum (lambda (acc elt) + (if (seq-contains-p acc elt testfn) + acc + (cons elt acc)))) + (result (seq-reduce accum sequence2 + (seq-reduce accum sequence1 '())))) + (nreverse result))) + +;;;###autoload (cl-defgeneric seq-intersection (sequence1 sequence2 &optional testfn) "Return a list of the elements that appear in both SEQUENCE1 and SEQUENCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." @@ -469,7 +490,6 @@ Equality is defined by TESTFN if non-nil or by `equal' if nil." (seq-reverse sequence1) '())) -;;;###autoload (cl-defgeneric seq-difference (sequence1 sequence2 &optional testfn) "Return a list of the elements that appear in SEQUENCE1 but not in SEQUENCE2. Equality is defined by TESTFN if non-nil or by `equal' if nil." @@ -508,7 +528,7 @@ SEQUENCE must be a sequence of numbers or markers." (apply #'max (seq-into sequence 'list))) (defun seq--count-successive (pred sequence) - "Return the number of successive elements for which (PRED element) is non-nil in SEQUENCE." + "Count successive elements for which (PRED element) is non-nil in SEQUENCE." (let ((n 0) (len (seq-length sequence))) (while (and (< n len) @@ -517,7 +537,7 @@ SEQUENCE must be a sequence of numbers or markers." n)) (defun seq--make-pcase-bindings (args) - "Return a list of bindings of the variables in ARGS to the elements of a sequence." + "Return list of bindings of the variables in ARGS to the elements of a sequence." (let ((bindings '()) (index 0) (rest-marker nil)) @@ -549,6 +569,7 @@ SEQUENCE must be a sequence of numbers or markers." If no element is found, return nil." (ignore-errors (seq-elt sequence n))) +;;;###autoload (cl-defgeneric seq-random-elt (sequence) "Return a random element from SEQUENCE. Signal an error if SEQUENCE is empty." |