diff options
| author | Nicolas Petton | 2015-05-05 21:45:36 +0200 |
|---|---|---|
| committer | Nicolas Petton | 2015-05-05 21:45:36 +0200 |
| commit | 6cd74155985f6335e26ffa419c38d0d2f91e3b4e (patch) | |
| tree | 7e85d09789d5c8f57c6234eac99afa14d3fb3379 | |
| parent | b096be2aa8f2eeaef12cb4e19994f40ab85a342c (diff) | |
| download | emacs-6cd74155985f6335e26ffa419c38d0d2f91e3b4e.tar.gz emacs-6cd74155985f6335e26ffa419c38d0d2f91e3b4e.zip | |
Add support for &rest in `seq-let'
* lisp/emacs-lisp/seq.el (seq--make-bindings): Add support for `&rest'
in the argument list.
* test/automated/seq-tests.el: Add a test for parsing and binding
`&rest' in `seq-let'.
| -rw-r--r-- | lisp/emacs-lisp/seq.el | 35 | ||||
| -rw-r--r-- | test/automated/seq-tests.el | 6 |
2 files changed, 27 insertions, 14 deletions
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el index ea7a61382e1..39389454adf 100644 --- a/lisp/emacs-lisp/seq.el +++ b/lisp/emacs-lisp/seq.el | |||
| @@ -42,7 +42,8 @@ | |||
| 42 | 42 | ||
| 43 | ;;; TODO: | 43 | ;;; TODO: |
| 44 | 44 | ||
| 45 | ;; - Add support for &rest in the argument list of seq-let | 45 | ;; - Add a pcase macro named using `pcase-defmacro' that `seq-let' |
| 46 | ;; - could wrap. | ||
| 46 | 47 | ||
| 47 | ;;; Code: | 48 | ;;; Code: |
| 48 | 49 | ||
| @@ -350,20 +351,28 @@ This is an optimization for lists in `seq-take-while'." | |||
| 350 | (font-lock-add-keywords 'emacs-lisp-mode | 351 | (font-lock-add-keywords 'emacs-lisp-mode |
| 351 | '("\\<seq-doseq\\>" "\\<seq-let\\>"))) | 352 | '("\\<seq-doseq\\>" "\\<seq-let\\>"))) |
| 352 | 353 | ||
| 353 | (defun seq--make-bindings (args seq &optional initial-bindings) | 354 | (defun seq--make-bindings (args seq &optional bindings) |
| 354 | "Return an alist of bindings of the variables in ARGS to the elements of SEQ. | 355 | "Return a list of bindings of the variables in ARGS to the elements of SEQ. |
| 355 | if INITIAL-BINDINGS is non-nil, append new bindings to it, and | 356 | if BINDINGS is non-nil, append new bindings to it, and |
| 356 | return INITIAL-BINDINGS." | 357 | return BINDINGS." |
| 357 | (let ((index 0)) | 358 | (let ((index 0) |
| 359 | (rest-bound nil)) | ||
| 358 | (seq-doseq (name args) | 360 | (seq-doseq (name args) |
| 359 | (if (sequencep name) | 361 | (unless rest-bound |
| 360 | (setq initial-bindings (seq--make-bindings | 362 | (pcase name |
| 361 | (seq--elt-safe args index) | 363 | ((pred seq-p) |
| 362 | `(seq--elt-safe ,seq ,index) | 364 | (setq bindings (seq--make-bindings (seq--elt-safe args index) |
| 363 | initial-bindings)) | 365 | `(seq--elt-safe ,seq ,index) |
| 364 | (push `(,name (seq--elt-safe ,seq ,index)) initial-bindings)) | 366 | bindings))) |
| 367 | (`&rest | ||
| 368 | (progn (push `(,(seq--elt-safe args (1+ index)) | ||
| 369 | (seq-drop ,seq ,index)) | ||
| 370 | bindings) | ||
| 371 | (setq rest-bound t))) | ||
| 372 | (t | ||
| 373 | (push `(,name (seq--elt-safe ,seq ,index)) bindings)))) | ||
| 365 | (setq index (1+ index))) | 374 | (setq index (1+ index))) |
| 366 | initial-bindings)) | 375 | bindings)) |
| 367 | 376 | ||
| 368 | (defun seq--elt-safe (seq n) | 377 | (defun seq--elt-safe (seq n) |
| 369 | "Return element of SEQ at the index N. | 378 | "Return element of SEQ at the index N. |
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el index e1e8ae002d0..ab46eb85f76 100644 --- a/test/automated/seq-tests.el +++ b/test/automated/seq-tests.el | |||
| @@ -283,7 +283,11 @@ Evaluate BODY for each created sequence. | |||
| 283 | (should (= b 2)) | 283 | (should (= b 2)) |
| 284 | (should (= c 3)) | 284 | (should (= c 3)) |
| 285 | (should (= d 4)) | 285 | (should (= d 4)) |
| 286 | (should (null e)))) | 286 | (should (null e))) |
| 287 | (seq-let (a b &rest others) seq | ||
| 288 | (should (= a 1)) | ||
| 289 | (should (= b 2)) | ||
| 290 | (should (same-contents-p others (seq-drop seq 2))))) | ||
| 287 | (let ((seq '(1 (2 (3 (4)))))) | 291 | (let ((seq '(1 (2 (3 (4)))))) |
| 288 | (seq-let (_ (_ (_ (a)))) seq | 292 | (seq-let (_ (_ (_ (a)))) seq |
| 289 | (should (= a 4)))) | 293 | (should (= a 4)))) |