summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/seq.el
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2015-04-15 00:33:27 +0200
committerNicolas Petton <nicolas@petton.fr>2015-04-15 01:55:03 +0200
commit17d667b3876920652152baa4eab24134940a0f30 (patch)
treee7c8dcebf8bfc976e7212c84dc54b53ae3ced477 /lisp/emacs-lisp/seq.el
parent4191e54fc63be623b3a25081ab9fe03d28615fea (diff)
downloademacs-17d667b3876920652152baa4eab24134940a0f30.tar.gz
Add seq-intersection and seq-difference to the seq library
* lisp/emacs-lisp/seq.el (seq-intersection, seq-difference): New functions. * test/automated/seq-tests.el: Add tests for seq-intersection and seq-difference. * doc/lispref/sequences.texi: Add documentation for seq-intersection and seq-difference.
Diffstat (limited to 'lisp/emacs-lisp/seq.el')
-rw-r--r--lisp/emacs-lisp/seq.el29
1 files changed, 28 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index c5f5906e7e5..6f7f3c46e2a 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: 1.3
+;; Version: 1.4
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
@@ -240,6 +240,26 @@ negative integer or 0, nil is returned."
(setq seq (seq-drop seq n)))
(nreverse result))))
+(defun seq-intersection (seq1 seq2 &optional testfn)
+ "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)
+ (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.
+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))
+ (cons elt acc)
+ acc))
+ (seq-reverse seq1)
+ '()))
+
(defun seq-group-by (function seq)
"Apply FUNCTION to each element of SEQ.
Separate the elements of SEQ into an alist using the results as
@@ -318,6 +338,11 @@ This is an optimization for lists in `seq-take-while'."
(setq n (+ 1 n)))
n))
+(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\\>")))
+
(defalias 'seq-copy #'copy-sequence)
(defalias 'seq-elt #'elt)
(defalias 'seq-length #'length)
@@ -325,5 +350,7 @@ This is an optimization for lists in `seq-take-while'."
(defalias 'seq-each #'seq-do)
(defalias 'seq-map #'mapcar)
+(add-to-list 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords)
+
(provide 'seq)
;;; seq.el ends here