emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash c3dc883 166/439: Merge pull request #26 from shost


From: Phillip Lord
Subject: [elpa] externals/dash c3dc883 166/439: Merge pull request #26 from shosti/when-if-let
Date: Tue, 04 Aug 2015 20:27:31 +0000

branch: externals/dash
commit c3dc883c9652abc074181b575659fcc87f4f5a51
Merge: 9371ce4 1396102
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>

    Merge pull request #26 from shosti/when-if-let
    
    Add -when-let and -if-let macros
---
 README.md       |   23 +++++++++++++++++++++++
 dash.el         |   39 +++++++++++++++++++++++++++++++++++++++
 dev/examples.el |    9 +++++++++
 3 files changed, 71 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md
index 027ddfe..b11ae3a 100644
--- a/README.md
+++ b/README.md
@@ -63,6 +63,8 @@ Or you can just dump `dash.el` in your load path somewhere.
 * [->](#--x-optional-form-rest-more) `(x &optional form &rest more)`
 * [->>](#--x-form-rest-more) `(x form &rest more)`
 * [-->](#---x-form-rest-more) `(x form &rest more)`
+* [-when-let](#-when-let-var-val-rest-body) `(var-val &rest body)`
+* [-if-let](#-if-let-var-val-then-optional-else) `(var-val then &optional 
else)`
 * [!cons](#-cons-car-cdr) `(car cdr)`
 * [!cdr](#-cdr-list) `(list)`
 
@@ -674,6 +676,27 @@ in in second form, etc.
 (--> "def" (concat "abc" it "ghi") upcase) ;; => "ABCDEFGHI"
 ```
 
+### -when-let `(var-val &rest body)`
+
+If `val` evaluates to non-nil, bind it to `var` and execute body.
+`var-val` should be a (var val) pair.
+
+```cl
+(-when-let (match-index (string-match "d" "abcd")) (+ match-index 2)) ;; => 5
+(--when-let (member :b '(:a :b :c)) (cons :d it)) ;; => '(:d :b :c)
+(--when-let (even? 3) (cat it :a)) ;; => nil
+```
+
+### -if-let `(var-val then &optional else)`
+
+If `val` evaluates to non-nil, bind it to `var` and do `then`,
+otherwise do `else`.  `var-val` should be a (`var` `val`) pair.
+
+```cl
+(-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7) ;; => 7
+(--if-let (even? 4) it nil) ;; => t
+```
+
 ### !cons `(car cdr)`
 
 Destructive: Sets `cdr` to the cons of `car` and `cdr`.
diff --git a/dash.el b/dash.el
index b04fb27..81748ac 100644
--- a/dash.el
+++ b/dash.el
@@ -682,6 +682,41 @@ in in second form, etc."
 (put '->> 'lisp-indent-function 1)
 (put '--> 'lisp-indent-function 1)
 
+(defmacro -when-let (var-val &rest body)
+  "If VAL evaluates to non-nil, bind it to VAR and execute body.
+VAR-VAL should be a (var val) pair."
+  (let ((var (car var-val))
+        (val (cadr var-val)))
+    `(let ((,var ,val))
+       (when ,var
+         ,@body))))
+
+(defmacro --when-let (val &rest body)
+  "If VAL evaluates to non-nil, bind it to `it' and execute
+body."
+  `(let ((it ,val))
+     (when it
+       ,@body)))
+
+(defmacro -if-let (var-val then &optional else)
+  "If VAL evaluates to non-nil, bind it to VAR and do THEN,
+otherwise do ELSE.  VAR-VAL should be a (VAR VAL) pair."
+  (let ((var (car var-val))
+        (val (cadr var-val)))
+    `(let ((,var ,val))
+       (if ,var ,then ,else))))
+
+(defmacro --if-let (val then &optional else)
+  "If VAL evaluates to non-nil, bind it to `it' and do THEN,
+otherwise do ELSE."
+  `(let ((it ,val))
+     (if it ,then ,else)))
+
+(put '-when-let 'lisp-indent-function 1)
+(put '--when-let 'lisp-indent-function 1)
+(put '-if-let 'lisp-indent-function 1)
+(put '--if-let 'lisp-indent-function 1)
+
 (defun -distinct (list)
   "Return a new list with all duplicates removed.
 The test for equality is done with `equal',
@@ -826,6 +861,10 @@ Returns nil if N is less than 1."
                            "->"
                            "->>"
                            "-->"
+                           "-when-let"
+                           "--when-let"
+                           "-if-let"
+                           "--if-let"
                            "-distinct"
                            "-intersection"
                            "-difference"
diff --git a/dev/examples.el b/dev/examples.el
index b6d9045..4482700 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -273,6 +273,15 @@
   (--> "def" (concat "abc" it "ghi") (upcase it)) => "ABCDEFGHI"
   (--> "def" (concat "abc" it "ghi") upcase) => "ABCDEFGHI")
 
+(defexamples -when-let
+  (-when-let (match-index (string-match "d" "abcd")) (+ match-index 2)) => 5
+  (--when-let (member :b '(:a :b :c)) (cons :d it)) => '(:d :b :c)
+  (--when-let (even? 3) (cat it :a)) => nil)
+
+(defexamples -if-let
+  (-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7) => 7
+  (--if-let (even? 4) it nil) => t)
+
 (defexamples !cons
   (let (l) (!cons 5 l) l) => '(5)
   (let ((l '(3))) (!cons 5 l) l) => '(5 3))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]