diff options
| author | Nicolas Petton | 2015-04-15 00:33:27 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2015-04-15 01:55:03 +0200 |
| commit | 17d667b3876920652152baa4eab24134940a0f30 (patch) | |
| tree | e7c8dcebf8bfc976e7212c84dc54b53ae3ced477 /lisp | |
| parent | 4191e54fc63be623b3a25081ab9fe03d28615fea (diff) | |
| download | emacs-17d667b3876920652152baa4eab24134940a0f30.tar.gz emacs-17d667b3876920652152baa4eab24134940a0f30.zip | |
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')
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 29 |
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 @@ | |||
| 4 | 4 | ||
| 5 | ;; Author: Nicolas Petton <nicolas@petton.fr> | 5 | ;; Author: Nicolas Petton <nicolas@petton.fr> |
| 6 | ;; Keywords: sequences | 6 | ;; Keywords: sequences |
| 7 | ;; Version: 1.3 | 7 | ;; Version: 1.4 |
| 8 | ;; Package: seq | 8 | ;; Package: seq |
| 9 | 9 | ||
| 10 | ;; Maintainer: emacs-devel@gnu.org | 10 | ;; Maintainer: emacs-devel@gnu.org |
| @@ -240,6 +240,26 @@ negative integer or 0, nil is returned." | |||
| 240 | (setq seq (seq-drop seq n))) | 240 | (setq seq (seq-drop seq n))) |
| 241 | (nreverse result)))) | 241 | (nreverse result)))) |
| 242 | 242 | ||
| 243 | (defun seq-intersection (seq1 seq2 &optional testfn) | ||
| 244 | "Return a list of the elements that appear in both SEQ1 and SEQ2. | ||
| 245 | Equality is defined by TESTFN if non-nil or by `equal' if nil." | ||
| 246 | (seq-reduce (lambda (acc elt) | ||
| 247 | (if (seq-contains-p seq2 elt testfn) | ||
| 248 | (cons elt acc) | ||
| 249 | acc)) | ||
| 250 | (seq-reverse seq1) | ||
| 251 | '())) | ||
| 252 | |||
| 253 | (defun seq-difference (seq1 seq2 &optional testfn) | ||
| 254 | "Return a list of th elements that appear in SEQ1 but not in SEQ2. | ||
| 255 | Equality is defined by TESTFN if non-nil or by `equal' if nil." | ||
| 256 | (seq-reduce (lambda (acc elt) | ||
| 257 | (if (not (seq-contains-p seq2 elt testfn)) | ||
| 258 | (cons elt acc) | ||
| 259 | acc)) | ||
| 260 | (seq-reverse seq1) | ||
| 261 | '())) | ||
| 262 | |||
| 243 | (defun seq-group-by (function seq) | 263 | (defun seq-group-by (function seq) |
| 244 | "Apply FUNCTION to each element of SEQ. | 264 | "Apply FUNCTION to each element of SEQ. |
| 245 | Separate the elements of SEQ into an alist using the results as | 265 | 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'." | |||
| 318 | (setq n (+ 1 n))) | 338 | (setq n (+ 1 n))) |
| 319 | n)) | 339 | n)) |
| 320 | 340 | ||
| 341 | (defun seq--activate-font-lock-keywords () | ||
| 342 | "Activate font-lock keywords for some symbols defined in seq." | ||
| 343 | (font-lock-add-keywords 'emacs-lisp-mode | ||
| 344 | '("\\<seq-doseq\\>"))) | ||
| 345 | |||
| 321 | (defalias 'seq-copy #'copy-sequence) | 346 | (defalias 'seq-copy #'copy-sequence) |
| 322 | (defalias 'seq-elt #'elt) | 347 | (defalias 'seq-elt #'elt) |
| 323 | (defalias 'seq-length #'length) | 348 | (defalias 'seq-length #'length) |
| @@ -325,5 +350,7 @@ This is an optimization for lists in `seq-take-while'." | |||
| 325 | (defalias 'seq-each #'seq-do) | 350 | (defalias 'seq-each #'seq-do) |
| 326 | (defalias 'seq-map #'mapcar) | 351 | (defalias 'seq-map #'mapcar) |
| 327 | 352 | ||
| 353 | (add-to-list 'emacs-lisp-mode-hook #'seq--activate-font-lock-keywords) | ||
| 354 | |||
| 328 | (provide 'seq) | 355 | (provide 'seq) |
| 329 | ;;; seq.el ends here | 356 | ;;; seq.el ends here |