diff options
| author | Nicolas Petton | 2016-02-14 10:25:10 +0100 |
|---|---|---|
| committer | Nicolas Petton | 2016-02-14 10:25:10 +0100 |
| commit | 30fe90fa3c8f814a30a5136089b995b0a26f5cd0 (patch) | |
| tree | 6854e323308d2e2790119ffb47d9d97ec8a2a056 | |
| parent | d9bf0c1c6a6ce90aa2edbb911fb58b26975d423b (diff) | |
| download | emacs-30fe90fa3c8f814a30a5136089b995b0a26f5cd0.tar.gz emacs-30fe90fa3c8f814a30a5136089b995b0a26f5cd0.zip | |
New function seq-map-indexed
* lisp/emacs-lisp/seq.el (seq-map-indexed): New function.
* test/lisp/emacs-lisp/seq-tests.el: Add tests for seq-map-indexed.
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 12 | ||||
| -rw-r--r-- | test/lisp/emacs-lisp/seq-tests.el | 10 |
2 files changed, 22 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index 300fe5cd1fd..8b7b594f5e1 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el | |||
| @@ -144,6 +144,18 @@ if positive or too small if negative)." | |||
| 144 | sequence) | 144 | sequence) |
| 145 | (nreverse result))) | 145 | (nreverse result))) |
| 146 | 146 | ||
| 147 | (defun seq-map-indexed (function sequence) | ||
| 148 | "Return the result of applying FUNCTION to each element of SEQUENCE. | ||
| 149 | Unlike `seq-map', FUNCTION takes two arguments: the element of | ||
| 150 | the sequence, and its index within the sequence." | ||
| 151 | (let ((index 0)) | ||
| 152 | (seq-map (lambda (elt) | ||
| 153 | (prog1 | ||
| 154 | (funcall function elt index) | ||
| 155 | (setq index (1+ index)))) | ||
| 156 | sequence))) | ||
| 157 | |||
| 158 | |||
| 147 | ;; faster implementation for sequences (sequencep) | 159 | ;; faster implementation for sequences (sequencep) |
| 148 | (cl-defmethod seq-map (function (sequence sequence)) | 160 | (cl-defmethod seq-map (function (sequence sequence)) |
| 149 | (mapcar function sequence)) | 161 | (mapcar function sequence)) |
diff --git a/test/lisp/emacs-lisp/seq-tests.el b/test/lisp/emacs-lisp/seq-tests.el index a8ca48b1328..c9219b51d00 100644 --- a/test/lisp/emacs-lisp/seq-tests.el +++ b/test/lisp/emacs-lisp/seq-tests.el | |||
| @@ -97,6 +97,16 @@ Evaluate BODY for each created sequence. | |||
| 97 | (with-test-sequences (seq '()) | 97 | (with-test-sequences (seq '()) |
| 98 | (should (seq-empty-p (seq-take-while #'test-sequences-oddp seq))))) | 98 | (should (seq-empty-p (seq-take-while #'test-sequences-oddp seq))))) |
| 99 | 99 | ||
| 100 | (ert-deftest test-seq-map-indexed () | ||
| 101 | (should (equal (seq-map-indexed (lambda (elt i) | ||
| 102 | (list elt i)) | ||
| 103 | nil) | ||
| 104 | nil)) | ||
| 105 | (should (equal (seq-map-indexed (lambda (elt i) | ||
| 106 | (list elt i)) | ||
| 107 | '(a b c d)) | ||
| 108 | '((a 0) (b 1) (c 2) (d 3))))) | ||
| 109 | |||
| 100 | (ert-deftest test-seq-filter () | 110 | (ert-deftest test-seq-filter () |
| 101 | (with-test-sequences (seq '(6 7 8 9 10)) | 111 | (with-test-sequences (seq '(6 7 8 9 10)) |
| 102 | (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10))) | 112 | (should (equal (seq-filter #'test-sequences-evenp seq) '(6 8 10))) |