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

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

[elpa] externals/dash c09c0f6 372/426: Merge branch 'more-destructuring'


From: Phillip Lord
Subject: [elpa] externals/dash c09c0f6 372/426: Merge branch 'more-destructuring' (#103) of https://github.com/fbergroth/dash.el
Date: Tue, 04 Aug 2015 19:39:00 +0000

branch: externals/dash
commit c09c0f667b69e6edbbf6b4b5447de26d05df9b85
Merge: d0c6fc0 8707aaf
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>

    Merge branch 'more-destructuring' (#103) of 
https://github.com/fbergroth/dash.el
---
 README.md       |   23 +++++++---
 dash.el         |  123 ++++++++++++++++++++++++++-----------------------------
 dev/examples.el |    5 ++-
 3 files changed, 78 insertions(+), 73 deletions(-)

diff --git a/README.md b/README.md
index 041dfe3..ad8dbd5 100644
--- a/README.md
+++ b/README.md
@@ -1312,7 +1312,7 @@ second elements of each list, and so on. The lengths of 
the returned
 groupings are equal to the length of the shortest input list.
 
 If two lists are provided as arguments, return the groupings as a list
-of cons cells. Otherwise, return the groupings as a list of lists. 
+of cons cells. Otherwise, return the groupings as a list of lists.
 
 ```el
 (-zip '(1 2 3) '(4 5 6)) ;; => '((1 . 4) (2 . 5) (3 . 6))
@@ -1663,17 +1663,21 @@ Convenient versions of `let` and `let*` constructs 
combined with flow control.
 If `val` evaluates to non-nil, bind it to `var` and execute body.
 `var-val` should be a (`var` `val`) pair.
 
-```el
+Note: binding is done according to `-let`.
+
+```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
+(-when-let ((&plist :foo foo) (list :foo "foo")) foo) ;; => "foo"
+(-when-let ((&plist :foo foo) (list :bar "bar")) foo) ;; => nil
 ```
 
 #### -when-let* `(vars-vals &rest body)`
 
 If all `vals` evaluate to true, bind them to their corresponding
 `vars` and execute body. `vars-vals` should be a list of (`var` `val`)
-pairs (corresponding to bindings of `let*`).
+pairs.
+
+Note: binding is done according to `-let`.
 
 ```el
 (-when-let* ((x 5) (y 3) (z (+ y 4))) (+ x y z)) ;; => 15
@@ -1685,7 +1689,9 @@ pairs (corresponding to bindings of `let*`).
 If `val` evaluates to non-nil, bind it to `var` and do `then`,
 otherwise do `else`. `var-val` should be a (`var` `val`) pair.
 
-```el
+Note: binding is done according to `-let`.
+
+```cl
 (-if-let (match-index (string-match "d" "abc")) (+ match-index 3) 7) ;; => 7
 (--if-let (even? 4) it nil) ;; => t
 ```
@@ -1694,11 +1700,14 @@ otherwise do `else`. `var-val` should be a (`var` 
`val`) pair.
 
 If all `vals` evaluate to true, bind them to their corresponding
 `vars` and do `then`, otherwise do `else`. `vars-vals` should be a list
-of (`var` `val`) pairs (corresponding to the bindings of `let*`).
+of (`var` `val`) pairs.
+
+Note: binding is done according to `-let`.
 
 ```el
 (-if-let* ((x 5) (y 3) (z 7)) (+ x y z) "foo") ;; => 15
 (-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo") ;; => "foo"
+(-if-let* (((_ _ x) '(nil nil 7))) x) ;; => 7
 ```
 
 #### -let `(varlist &rest body)`
diff --git a/dash.el b/dash.el
index d398e8b..a499245 100644
--- a/dash.el
+++ b/dash.el
@@ -1114,71 +1114,6 @@ sorts it in descending order."
       (-sort comp)
       (-map 'cdr))))
 
-(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."
-  (declare (debug ((symbolp form) body))
-           (indent 1))
-  (let ((var (car var-val))
-        (val (cadr var-val)))
-    `(let ((,var ,val))
-       (when ,var
-         ,@body))))
-
-(defmacro -when-let* (vars-vals &rest body)
-  "If all VALS evaluate to true, bind them to their corresponding
-VARS and execute body. VARS-VALS should be a list of (VAR VAL)
-pairs (corresponding to bindings of `let*')."
-  (declare (debug ((&rest (symbolp form)) body))
-           (indent 1))
-  (if (= (length vars-vals) 1)
-      `(-when-let ,(car vars-vals)
-         ,@body)
-    `(-when-let ,(car vars-vals)
-       (-when-let* ,(cdr vars-vals)
-         ,@body))))
-
-(defmacro --when-let (val &rest body)
-  "If VAL evaluates to non-nil, bind it to `it' and execute
-body."
-  (declare (debug (form body))
-           (indent 1))
-  `(let ((it ,val))
-     (when it
-       ,@body)))
-
-(defmacro -if-let (var-val then &rest 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."
-  (declare (debug ((symbolp form) form body))
-           (indent 2))
-  (let ((var (car var-val))
-        (val (cadr var-val)))
-    `(let ((,var ,val))
-       (if ,var ,then ,@else))))
-
-(defmacro -if-let* (vars-vals then &rest else)
-  "If all VALS evaluate to true, bind them to their corresponding
-VARS and do THEN, otherwise do ELSE. VARS-VALS should be a list
-of (VAR VAL) pairs (corresponding to the bindings of `let*')."
-  (declare (debug ((&rest (symbolp form)) form body))
-           (indent 2))
-  (let ((first-pair (car vars-vals))
-        (rest (cdr vars-vals)))
-    (if (= (length vars-vals) 1)
-        `(-if-let ,first-pair ,then ,@else)
-      `(-if-let ,first-pair
-         (-if-let* ,rest ,then ,@else)
-         ,@else))))
-
-(defmacro --if-let (val then &rest else)
-  "If VAL evaluates to non-nil, bind it to `it' and do THEN,
-otherwise do ELSE."
-  (declare (debug (form form body))
-           (indent 2))
-  `(let ((it ,val))
-     (if it ,then ,@else)))
-
 (defun dash--match-ignore-place-p (symbol)
   "Return non-nil if SYMBOL is a symbol and starts with _."
   (and (symbolp symbol)
@@ -1556,6 +1491,64 @@ See `-let' for the description of destructuring 
mechanism."
       `(lambda ,(--map (cadr it) inputs)
          (-let* ,inputs ,@body))))))
 
+(defmacro -if-let* (vars-vals then &rest else)
+  "If all VALS evaluate to true, bind them to their corresponding
+VARS and do THEN, otherwise do ELSE. VARS-VALS should be a list
+of (VAR VAL) pairs.
+
+Note: binding is done according to `-let'."
+  (declare (debug ((&rest (symbolp form)) form body))
+           (indent 2))
+  (->> vars-vals
+    (-mapcat (-lambda ((pat src)) (dash--match pat src)))
+    (-reduce-r-from
+     (-lambda ((var val) memo)
+       `(let ((,var ,val))
+          (if ,var ,memo ,@else)))
+     then)))
+
+(defmacro -if-let (var-val then &rest 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.
+
+Note: binding is done according to `-let'."
+  (declare (debug ((symbolp form) form body))
+           (indent 2))
+  `(-if-let* (,var-val) ,then ,@else))
+
+(defmacro --if-let (val then &rest else)
+  "If VAL evaluates to non-nil, bind it to `it' and do THEN,
+otherwise do ELSE."
+  (declare (debug (form form body))
+           (indent 2))
+  `(-if-let (it ,val) ,then ,@else))
+
+(defmacro -when-let* (vars-vals &rest body)
+  "If all VALS evaluate to true, bind them to their corresponding
+VARS and execute body. VARS-VALS should be a list of (VAR VAL)
+pairs.
+
+Note: binding is done according to `-let'."
+  (declare (debug ((&rest (symbolp form)) body))
+           (indent 1))
+  `(-if-let* ,vars-vals (progn ,@body)))
+
+(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.
+
+Note: binding is done according to `-let'."
+  (declare (debug ((symbolp form) body))
+           (indent 1))
+  `(-if-let ,var-val (progn ,@body)))
+
+(defmacro --when-let (val &rest body)
+  "If VAL evaluates to non-nil, bind it to `it' and execute
+body."
+  (declare (debug (form body))
+           (indent 1))
+  `(--if-let ,val (progn ,@body)))
+
 (defun -distinct (list)
   "Return a new list with all duplicates removed.
 The test for equality is done with `equal',
diff --git a/dev/examples.el b/dev/examples.el
index 75e1918..7e25d16 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -678,6 +678,8 @@ new list."
 
   (defexamples -when-let
     (-when-let (match-index (string-match "d" "abcd")) (+ match-index 2)) => 5
+    (-when-let ((&plist :foo foo) (list :foo "foo")) foo) => "foo"
+    (-when-let ((&plist :foo foo) (list :bar "bar")) foo) => nil
     (--when-let (member :b '(:a :b :c)) (cons :d it)) => '(:d :b :c)
     (--when-let (even? 3) (cat it :a)) => nil)
 
@@ -691,7 +693,8 @@ new list."
 
   (defexamples -if-let*
     (-if-let* ((x 5) (y 3) (z 7)) (+ x y z) "foo") => 15
-    (-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo") => "foo")
+    (-if-let* ((x 5) (y nil) (z 7)) (+ x y z) "foo") => "foo"
+    (-if-let* (((_ _ x) '(nil nil 7))) x) => 7)
 
   (defexamples -let
     (-let (([a (b c) d] [1 (2 3) 4])) (list a b c d)) => '(1 2 3 4)



reply via email to

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