mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-01-03 02:31:03 -08:00
Merge branch 'seq-let'
This commit is contained in:
commit
4ac426a1b9
3 changed files with 104 additions and 3 deletions
|
|
@ -413,7 +413,7 @@ but their relative order is also preserved:
|
||||||
(9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
|
(9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
|
||||||
@end group
|
@end group
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@xref{Sorting}, for more functions that perform sorting.
|
@xref{Sorting}, for more functions that perform sorting.
|
||||||
See @code{documentation} in @ref{Accessing Documentation}, for a
|
See @code{documentation} in @ref{Accessing Documentation}, for a
|
||||||
useful example of @code{sort}.
|
useful example of @code{sort}.
|
||||||
|
|
@ -797,6 +797,42 @@ vector or string (@pxref{Iteration} for more information about the
|
||||||
@code{dolist} macro). This is primarily useful for side-effects.
|
@code{dolist} macro). This is primarily useful for side-effects.
|
||||||
@end defmac
|
@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.
|
||||||
|
|
||||||
|
The @var{arguments} sequence can also include the `&rest' marker
|
||||||
|
followed by a variable name to be bound to the rest of
|
||||||
|
@code{sequence}.
|
||||||
|
|
||||||
|
@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
|
||||||
|
@group
|
||||||
|
(seq-let [a b &rest others] [1 2 3 4]
|
||||||
|
others)
|
||||||
|
@end group
|
||||||
|
@result{} [3 4]
|
||||||
|
@end example
|
||||||
|
@end defmac
|
||||||
|
|
||||||
|
|
||||||
@node Arrays
|
@node Arrays
|
||||||
@section Arrays
|
@section Arrays
|
||||||
@cindex array
|
@cindex array
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
;; Author: Nicolas Petton <nicolas@petton.fr>
|
;; Author: Nicolas Petton <nicolas@petton.fr>
|
||||||
;; Keywords: sequences
|
;; Keywords: sequences
|
||||||
;; Version: 1.5
|
;; Version: 1.6
|
||||||
;; Package: seq
|
;; Package: seq
|
||||||
|
|
||||||
;; Maintainer: emacs-devel@gnu.org
|
;; Maintainer: emacs-devel@gnu.org
|
||||||
|
|
@ -40,6 +40,11 @@
|
||||||
;;
|
;;
|
||||||
;; All functions are tested in test/automated/seq-tests.el
|
;; All functions are tested in test/automated/seq-tests.el
|
||||||
|
|
||||||
|
;;; TODO:
|
||||||
|
|
||||||
|
;; - Add a pcase macro named using `pcase-defmacro' that `seq-let'
|
||||||
|
;; - could wrap.
|
||||||
|
|
||||||
;;; Code:
|
;;; Code:
|
||||||
|
|
||||||
(defmacro seq-doseq (spec &rest body)
|
(defmacro seq-doseq (spec &rest body)
|
||||||
|
|
@ -65,6 +70,14 @@ Evaluate BODY with VAR bound to each element of SEQ, in turn.
|
||||||
(pop ,index))))
|
(pop ,index))))
|
||||||
,@body)))))
|
,@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)
|
(defun seq-drop (seq n)
|
||||||
"Return a subsequence of SEQ without its first N elements.
|
"Return a subsequence of SEQ without its first N elements.
|
||||||
The result is a sequence of the same type as SEQ.
|
The result is a sequence of the same type as SEQ.
|
||||||
|
|
@ -336,7 +349,38 @@ This is an optimization for lists in `seq-take-while'."
|
||||||
(defun seq--activate-font-lock-keywords ()
|
(defun seq--activate-font-lock-keywords ()
|
||||||
"Activate font-lock keywords for some symbols defined in seq."
|
"Activate font-lock keywords for some symbols defined in seq."
|
||||||
(font-lock-add-keywords 'emacs-lisp-mode
|
(font-lock-add-keywords 'emacs-lisp-mode
|
||||||
'("\\<seq-doseq\\>")))
|
'("\\<seq-doseq\\>" "\\<seq-let\\>")))
|
||||||
|
|
||||||
|
(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)
|
||||||
|
(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)))
|
||||||
|
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-copy #'copy-sequence)
|
||||||
(defalias 'seq-elt #'elt)
|
(defalias 'seq-elt #'elt)
|
||||||
|
|
|
||||||
|
|
@ -276,5 +276,26 @@ Evaluate BODY for each created sequence.
|
||||||
(v2 [2 4 6]))
|
(v2 [2 4 6]))
|
||||||
(should (seq-empty-p (seq-difference v1 v2)))))
|
(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)))
|
||||||
|
(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))))
|
||||||
|
(let (seq)
|
||||||
|
(seq-let (a b c) seq
|
||||||
|
(should (null a))
|
||||||
|
(should (null b))
|
||||||
|
(should (null c)))))
|
||||||
|
|
||||||
(provide 'seq-tests)
|
(provide 'seq-tests)
|
||||||
;;; seq-tests.el ends here
|
;;; seq-tests.el ends here
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue