summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Petton <nicolas@petton.fr>2015-05-01 19:30:56 +0200
committerNicolas Petton <nicolas@petton.fr>2015-05-01 19:33:40 +0200
commitc856843f6b83d4578e7fab3d9821802f2a542540 (patch)
tree4e4e2f43fb72bc7a83ff9c1b283d8048fa7a2579
parentb0481de73be9fb7c4839340de2b494f19041130a (diff)
downloademacs-seq-let.tar.gz
New macro seq-let, providing destructuring support to seq.elseq-let
* lisp/emacs-lisp/seq.el (seq-let): New macro. `seq-let' is similar to `cl-destructuring-bind' but works on all sequence types supported by `seq.el'. Bump version number to 1.6. * test/automated/seq-tests.el: Add tests for seq-let. * doc/lispref/sequences.texi: Add documentation for seq-let.
-rw-r--r--doc/lispref/sequences.texi27
-rw-r--r--lisp/emacs-lisp/seq.el39
-rw-r--r--test/automated/seq-tests.el17
3 files changed, 81 insertions, 2 deletions
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi
index b48fae4741f..e58f0390a45 100644
--- a/doc/lispref/sequences.texi
+++ b/doc/lispref/sequences.texi
@@ -797,6 +797,33 @@ vector or string (@pxref{Iteration} for more information about the
@code{dolist} macro). This is primarily useful for side-effects.
@end defmac
+@defmac seq-let arguments sequense body@dots{}
+@cindex sequence destructuring
+ This macro binds the variables in defined in the sequence
+@var{arguments} to the elements of the sequence @var{sequence}.
+@var{arguments} can itself include sequences allowing for nested
+destructuring.
+
+@example
+@group
+(seq-let [first second] [1 2 3 4]
+ (list first second))
+@result{} (1 2)
+@end group
+@group
+(seq-let (_ a _ b) '(1 2 3 4)
+ (list a b))
+@result{} (2 4)
+@end group
+@group
+(seq-let [a [b [c]]] [1 [2 [3]]]
+ (list a b c))
+@result{} (1 2 3)
+@end group
+@end example
+@end defmac
+
+
@node Arrays
@section Arrays
@cindex array
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 2f3f519e986..d22bbc57817 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.5
+;; Version: 1.6
;; Package: seq
;; Maintainer: emacs-devel@gnu.org
@@ -40,6 +40,10 @@
;;
;; All functions are tested in test/automated/seq-tests.el
+;;; TODO:
+
+;; - Add support for &rest in the argument list of seq-let
+
;;; Code:
(defmacro seq-doseq (spec &rest body)
@@ -65,6 +69,14 @@ Evaluate BODY with VAR bound to each element of SEQ, in turn.
(pop ,index))))
,@body)))))
+(defmacro seq-let (args seq &rest body)
+ "Bind the variables in ARGS to the elements of SEQ then evaluate BODY."
+ (declare (indent 2) (debug t))
+ (let ((seq-var (make-symbol "seq")))
+ `(let* ((,seq-var ,seq)
+ ,@(seq--make-bindings args seq-var))
+ ,@body)))
+
(defun seq-drop (seq n)
"Return a subsequence of SEQ without its first N elements.
The result is a sequence of the same type as SEQ.
@@ -336,7 +348,30 @@ This is an optimization for lists in `seq-take-while'."
(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\\>")))
+ '("\\<seq-doseq\\>" "\\<seq-let\\>")))
+
+(defun seq--make-bindings (args seq &optional initial-bindings)
+ "Return an alist of the bindings 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))
+ (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))
+ (setq index (1+ index)))
+ initial-bindings))
+
+(defun seq--elt-safe (seq n)
+ "Return element of SEQ at the index N.
+If no element is found, return nil."
+ (when (or (listp seq)
+ (and (sequencep seq)
+ (> (seq-length seq) n)))
+ (seq-elt seq n)))
(defalias 'seq-copy #'copy-sequence)
(defalias 'seq-elt #'elt)
diff --git a/test/automated/seq-tests.el b/test/automated/seq-tests.el
index 7f6e06cc4b6..e1e8ae002d0 100644
--- a/test/automated/seq-tests.el
+++ b/test/automated/seq-tests.el
@@ -276,5 +276,22 @@ Evaluate BODY for each created sequence.
(v2 [2 4 6]))
(should (seq-empty-p (seq-difference v1 v2)))))
+(ert-deftest test-seq-let ()
+ (with-test-sequences (seq '(1 2 3 4))
+ (seq-let (a b c d e) seq
+ (should (= a 1))
+ (should (= b 2))
+ (should (= c 3))
+ (should (= d 4))
+ (should (null e))))
+ (let ((seq '(1 (2 (3 (4))))))
+ (seq-let (_ (_ (_ (a)))) seq
+ (should (= a 4))))
+ (let (seq)
+ (seq-let (a b c) seq
+ (should (null a))
+ (should (null b))
+ (should (null c)))))
+
(provide 'seq-tests)
;;; seq-tests.el ends here