[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Emacs-diffs] seq-let 6cd7415 2/3: Add support for &rest in `seq-let'
From: |
Nicolas Petton |
Subject: |
[Emacs-diffs] seq-let 6cd7415 2/3: Add support for &rest in `seq-let' |
Date: |
Tue, 05 May 2015 20:05:29 +0000 |
branch: seq-let
commit 6cd74155985f6335e26ffa419c38d0d2f91e3b4e
Author: Nicolas Petton <address@hidden>
Commit: Nicolas Petton <address@hidden>
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'.
---
lisp/emacs-lisp/seq.el | 35 ++++++++++++++++++++++-------------
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 ea7a613..3938945 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -42,7 +42,8 @@
;;; TODO:
-;; - Add support for &rest in the argument list of seq-let
+;; - Add a pcase macro named using `pcase-defmacro' that `seq-let'
+;; - could wrap.
;;; Code:
@@ -350,20 +351,28 @@ This is an optimization for lists in `seq-take-while'."
(font-lock-add-keywords 'emacs-lisp-mode
'("\\<seq-doseq\\>" "\\<seq-let\\>")))
-(defun seq--make-bindings (args seq &optional initial-bindings)
- "Return an alist of bindings of the variables in ARGS to the elements of SEQ.
-if INITIAL-BINDINGS is non-nil, append new bindings to it, and
-return INITIAL-BINDINGS."
- (let ((index 0))
+(defun seq--make-bindings (args seq &optional bindings)
+ "Return a list of bindings of the variables in ARGS to the elements of SEQ.
+if BINDINGS is non-nil, append new bindings to it, and
+return BINDINGS."
+ (let ((index 0)
+ (rest-bound nil))
(seq-doseq (name args)
- (if (sequencep name)
- (setq initial-bindings (seq--make-bindings
- (seq--elt-safe args index)
- `(seq--elt-safe ,seq ,index)
- initial-bindings))
- (push `(,name (seq--elt-safe ,seq ,index)) initial-bindings))
+ (unless rest-bound
+ (pcase name
+ ((pred seq-p)
+ (setq bindings (seq--make-bindings (seq--elt-safe args index)
+ `(seq--elt-safe ,seq ,index)
+ bindings)))
+ (`&rest
+ (progn (push `(,(seq--elt-safe args (1+ index))
+ (seq-drop ,seq ,index))
+ bindings)
+ (setq rest-bound t)))
+ (t
+ (push `(,name (seq--elt-safe ,seq ,index)) bindings))))
(setq index (1+ index)))
- initial-bindings))
+ bindings))
(defun seq--elt-safe (seq n)
"Return element of SEQ at the index N.
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el
index e1e8ae0..ab46eb8 100644
--- a/test/automated/seq-tests.el
+++ b/test/automated/seq-tests.el
@@ -283,7 +283,11 @@ Evaluate BODY for each created sequence.
(should (= b 2))
(should (= c 3))
(should (= d 4))
- (should (null e))))
+ (should (null e)))
+ (seq-let (a b &rest others) seq
+ (should (= a 1))
+ (should (= b 2))
+ (should (same-contents-p others (seq-drop seq 2)))))
(let ((seq '(1 (2 (3 (4))))))
(seq-let (_ (_ (_ (a)))) seq
(should (= a 4))))