[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 1396102 165/439: Add -when-let and -if-let macros
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 1396102 165/439: Add -when-let and -if-let macros |
Date: |
Tue, 04 Aug 2015 20:27:31 +0000 |
branch: externals/dash
commit 139610257a765532e6aa86110a21056812682f85
Author: Emanuel Evans <address@hidden>
Commit: Emanuel Evans <address@hidden>
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))
- [elpa] externals/dash 0351ca3 156/439: Add Wilfred Hughes to list of contributors., (continued)
- [elpa] externals/dash 0351ca3 156/439: Add Wilfred Hughes to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash 2451f99 159/439: Merge pull request #20 from tarsius/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6ffdc93 157/439: Release 1.1.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash a559a80 161/439: Added -last, Phillip Lord, 2015/08/04
- [elpa] externals/dash a40fc00 162/439: Merge pull request #23 from Fuco1/last, Phillip Lord, 2015/08/04
- [elpa] externals/dash d7174ed 160/439: Handle cons-cells properly in -flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash a1b04d8 163/439: Less implementation specific docs for -mapcat, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9371ce4 164/439: Don't use the word collection when meaning list, Phillip Lord, 2015/08/04
- [elpa] externals/dash c3dc883 166/439: Merge pull request #26 from shosti/when-if-let, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6420bf8 167/439: Add Emanuel Evans to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1396102 165/439: Add -when-let and -if-let macros,
Phillip Lord <=
- [elpa] externals/dash 30ac13b 168/439: Add -insert-at (closes #27), Phillip Lord, 2015/08/04
- [elpa] externals/dash d5c124d 170/439: Update @shosti's list of contributions., Phillip Lord, 2015/08/04
- [elpa] externals/dash 1754ae2 171/439: Typo, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79e3b92 172/439: Fix -when-let and -if-let docstrings, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0ff070b 169/439: Merge pull request #28 from shosti/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 73ac975 174/439: Merge pull request #29 from shosti/when-let-multi, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8db9e1 176/439: Avoid unnecessary copying for -insert-at, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6d328e4 175/439: Release 1.2.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f9fca3 177/439: Merge pull request #30 from shosti/insert-at-opt, Phillip Lord, 2015/08/04
- [elpa] externals/dash 336e5a5 181/439: Release 1.3.1, Phillip Lord, 2015/08/04