summaryrefslogtreecommitdiff
path: root/lisp/emacs-lisp/subr-x.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina <fgallina@gnu.org>2014-06-30 01:11:43 -0300
committerFabián Ezequiel Gallina <fgallina@gnu.org>2014-06-30 01:11:43 -0300
commitc08f8be29f4f6d107da5cc38d614519df7a6ab11 (patch)
tree1aa7c297a73c23b8c0dccc1242601c1eb02033dc /lisp/emacs-lisp/subr-x.el
parentf8e16324a038417f0180b76c77c60313c880e74c (diff)
downloademacs-c08f8be29f4f6d107da5cc38d614519df7a6ab11.tar.gz
New if-let, when-let, thread-first and thread-last macros.
* lisp/emacs-lisp/subr-x.el (internal--listify, internal--check-binding) (internal--build-binding-value-form, internal--build-binding) (internal--build-bindings): New functions. (internal--thread-argument, thread-first, thread-last) (if-let, when-let): New macros. * test/automated/subr-x-tests.el (subr-x-test-if-let-single-binding-expansion) (subr-x-test-if-let-single-symbol-expansion) (subr-x-test-if-let-nil-related-expansion) (subr-x-test-if-let-malformed-binding, subr-x-test-if-let-true) (subr-x-test-if-let-false, subr-x-test-if-let-bound-references) (subr-x-test-if-let-and-lazyness-is-preserved) (subr-x-test-when-let-body-expansion) (subr-x-test-when-let-single-binding-expansion) (subr-x-test-when-let-single-symbol-expansion) (subr-x-test-when-let-nil-related-expansion) (subr-x-test-when-let-malformed-binding) (subr-x-test-when-let-true, subr-x-test-when-let-false) (subr-x-test-when-let-bound-references) (subr-x-test-when-let-and-lazyness-is-preserved) (subr-x-test-thread-first-no-forms) (subr-x-test-thread-first-function-names-are-threaded) (subr-x-test-thread-first-expansion) (subr-x-test-thread-last-no-forms) (subr-x-test-thread-last-function-names-are-threaded) (subr-x-test-thread-last-expansion): New tests.
Diffstat (limited to 'lisp/emacs-lisp/subr-x.el')
-rw-r--r--lisp/emacs-lisp/subr-x.el107
1 files changed, 107 insertions, 0 deletions
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 505a556b65f..60cd7b8995b 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -32,6 +32,113 @@
;;; Code:
+(require 'pcase)
+
+
+(defmacro internal--thread-argument (first? &rest forms)
+ "Internal implementation for `thread-first' and `thread-last'.
+When Argument FIRST? is non-nil argument is threaded first, else
+last. FORMS are the expressions to be threaded."
+ (pcase forms
+ (`(,x (,f . ,args) . ,rest)
+ `(internal--thread-argument
+ ,first? ,(if first? `(,f ,x ,@args) `(,f ,@args ,x)) ,@rest))
+ (`(,x ,f . ,rest) `(internal--thread-argument ,first? (,f ,x) ,@rest))
+ (_ (car forms))))
+
+(defmacro thread-first (&rest forms)
+ "Thread FORMS elements as the first argument of their succesor.
+Example:
+ (thread-first
+ 5
+ (+ 20)
+ (/ 25)
+ -
+ (+ 40))
+Is equivalent to:
+ (+ (- (/ (+ 5 20) 25)) 40)
+Note how the single `-' got converted into a list before
+threading."
+ (declare (indent 1)
+ (debug (form &rest [&or symbolp (sexp &rest form)])))
+ `(internal--thread-argument t ,@forms))
+
+(defmacro thread-last (&rest forms)
+ "Thread FORMS elements as the last argument of their succesor.
+Example:
+ (thread-last
+ 5
+ (+ 20)
+ (/ 25)
+ -
+ (+ 40))
+Is equivalent to:
+ (+ 40 (- (/ 25 (+ 20 5))))
+Note how the single `-' got converted into a list before
+threading."
+ (declare (indent 1) (debug thread-first))
+ `(internal--thread-argument nil ,@forms))
+
+(defsubst internal--listify (elt)
+ "Wrap ELT in a list if it is not one."
+ (if (not (listp elt))
+ (list elt)
+ elt))
+
+(defsubst internal--check-binding (binding)
+ "Check BINDING is properly formed."
+ (when (> (length binding) 2)
+ (signal
+ 'error
+ (cons "`let' bindings can have only one value-form" binding)))
+ binding)
+
+(defsubst internal--build-binding-value-form (binding prev-var)
+ "Build the conditional value form for BINDING using PREV-VAR."
+ `(,(car binding) (and ,prev-var ,(cadr binding))))
+
+(defun internal--build-binding (binding prev-var)
+ "Check and build a single BINDING with PREV-VAR."
+ (thread-first
+ binding
+ internal--listify
+ internal--check-binding
+ (internal--build-binding-value-form prev-var)))
+
+(defun internal--build-bindings (bindings)
+ "Check and build conditional value forms for BINDINGS."
+ (let ((prev-var t))
+ (mapcar (lambda (binding)
+ (let ((binding (internal--build-binding binding prev-var)))
+ (setq prev-var (car binding))
+ binding))
+ bindings)))
+
+(defmacro if-let (bindings then &rest else)
+ "Process BINDINGS and if all values are non-nil eval THEN, else ELSE.
+Argument BINDINGS is a list of tuples whose car is a symbol to be
+bound and (optionally) used in THEN, and its cadr is a sexp to be
+evaled to set symbol's value. In the special case you only want
+to bind a single value, BINDINGS can just be a plain tuple."
+ (declare (indent 2) (debug ((&rest (symbolp form)) form body)))
+ (when (and (<= (length bindings) 2)
+ (not (listp (car bindings))))
+ ;; Adjust the single binding case
+ (setq bindings (list bindings)))
+ `(let* ,(internal--build-bindings bindings)
+ (if ,(car (internal--listify (car (last bindings))))
+ ,then
+ ,@else)))
+
+(defmacro when-let (bindings &rest body)
+ "Process BINDINGS and if all values are non-nil eval BODY.
+Argument BINDINGS is a list of tuples whose car is a symbol to be
+bound and (optionally) used in BODY, and its cadr is a sexp to be
+evaled to set symbol's value. In the special case you only want
+to bind a single value, BINDINGS can just be a plain tuple."
+ (declare (indent 1) (debug if-let))
+ (list 'if-let bindings (macroexp-progn body)))
+
(defsubst hash-table-keys (hash-table)
"Return a list of keys in HASH-TABLE."
(let ((keys '()))