;; Author: Nicolas Petton <nicolas@petton.fr>
;; Keywords: sequences
-;; Version: 1.7
+;; Version: 1.9
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
(setq acc (funcall function acc elt)))
acc)))
-(defun seq-some-p (pred seq)
- "Return any element for which (PRED element) is non-nil in SEQ, nil otherwise."
+(defun seq-some (pred seq)
+ "Return non-nil if (PRED element) is non-nil for any element in SEQ, nil otherwise.
+If so, return the non-nil value returned by PRED."
+ (catch 'seq--break
+ (seq-doseq (elt seq)
+ (let ((result (funcall pred elt)))
+ (when result
+ (throw 'seq--break result))))
+ nil))
+
+(defun seq-find (pred seq &optional default)
+ "Return the first element for which (PRED element) is non-nil in SEQ.
+If no element is found, return DEFAULT.
+
+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."
(catch 'seq--break
(seq-doseq (elt seq)
(when (funcall pred elt)
(throw 'seq--break elt)))
- nil))
+ default))
(defun seq-every-p (pred seq)
"Return non-nil if (PRED element) is non-nil for all elements of the sequence SEQ."
(let ((result (seq-sort pred (append seq nil))))
(seq-into result (type-of seq)))))
-(defun seq-contains-p (seq elt &optional testfn)
+(defun seq-contains (seq elt &optional testfn)
"Return the first element in SEQ that equals to ELT.
Equality is defined by TESTFN if non-nil or by `equal' if nil."
- (seq-some-p (lambda (e)
+ (seq-some (lambda (e)
(funcall (or testfn #'equal) elt e))
seq))
TESTFN is used to compare elements, or `equal' if TESTFN is nil."
(let ((result '()))
(seq-doseq (elt seq)
- (unless (seq-contains-p result elt testfn)
+ (unless (seq-contains result elt testfn)
(setq result (cons elt result))))
(nreverse result)))
"Return a list of the elements that appear in both SEQ1 and SEQ2.
Equality is defined by TESTFN if non-nil or by `equal' if nil."
(seq-reduce (lambda (acc elt)
- (if (seq-contains-p seq2 elt testfn)
+ (if (seq-contains seq2 elt testfn)
(cons elt acc)
acc))
(seq-reverse seq1)
'()))
(defun seq-difference (seq1 seq2 &optional testfn)
- "Return a list of th elements that appear in SEQ1 but not in SEQ2.
+ "Return a list of the elements that appear in SEQ1 but not in SEQ2.
Equality is defined by TESTFN if non-nil or by `equal' if nil."
(seq-reduce (lambda (acc elt)
- (if (not (seq-contains-p seq2 elt testfn))
+ (if (not (seq-contains seq2 elt testfn))
(cons elt acc)
acc))
(seq-reverse seq1)
(`list (append seq nil))
(t (error "Not a sequence type name: %S" type))))
+(defun seq-min (seq)
+ "Return the smallest element of SEQ.
+SEQ must be a sequence of numbers or markers."
+ (apply #'min (seq-into seq 'list)))
+
+(defun seq-max (seq)
+ "Return the largest element of SEQ.
+SEQ must be a sequence of numbers or markers."
+ (apply #'max (seq-into seq 'list)))
+
(defun seq--drop-list (list n)
"Return a list from LIST without its first N elements.
This is an optimization for lists in `seq-drop'."
(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.
- (add-to-list 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
+ (add-hook 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords))
(provide 'seq)
;;; seq.el ends here