branch: externals/dash
commit 8f9fc4113d90b8f48192b56ea6d60db1bce2dc87
Author: Matus Goljer <[email protected]>
Commit: Matus Goljer <[email protected]>
Add support for multiple input arguments to -lambda
---
README.md | 2 +-
dash.el | 8 ++++++--
dev/examples.el | 2 ++
3 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index a065eec..5b425ac 100644
--- a/README.md
+++ b/README.md
@@ -1822,7 +1822,7 @@ See `-let` for the description of destructuring mechanism.
```cl
(-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) ;; => '(3 7 11)
(-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) ;; => '(3 7 11)
-(-map (-lambda ((&plist :a a :b b)) (+ a b)) '((:a 1 :b 2) (:a 3 :b 4) (:a 5
:b 6))) ;; => '(3 7 11)
+(funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) ;; =>
'(2 3 5 6)
```
diff --git a/dash.el b/dash.el
index 9b32668..7efe2b7 100644
--- a/dash.el
+++ b/dash.el
@@ -1426,8 +1426,12 @@ See `-let' for the description of destructuring
mechanism."
(symbolp (car match-form)))
`(lambda ,match-form ,@body))
(t
- `(lambda (x)
- (-let* ((,@match-form x)) ,@body)))))
+ (let* ((inputs (--map-indexed (list it (make-symbol (format "input%d"
it-index))) match-form)))
+ ;; TODO: because inputs to the lambda are evaluated only once,
+ ;; -let* need not to create the extra bindings to ensure that.
+ ;; We should find a way to optimize that. Not critical however.
+ `(lambda ,(--map (cadr it) inputs)
+ (-let* ,inputs ,@body))))))
(defun -distinct (list)
"Return a new list with all duplicates removed.
diff --git a/dev/examples.el b/dev/examples.el
index 2bd0ee1..d78bccc 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -757,8 +757,10 @@ new list."
(defexamples -lambda
(-map (-lambda ((x y)) (+ x y)) '((1 2) (3 4) (5 6))) => '(3 7 11)
(-map (-lambda ([x y]) (+ x y)) '([1 2] [3 4] [5 6])) => '(3 7 11)
+ (funcall (-lambda ((_ . a) (_ . b)) (-concat a b)) '(1 2 3) '(4 5 6)) =>
'(2 3 5 6)
(-map (-lambda ((&plist :a a :b b)) (+ a b)) '((:a 1 :b 2) (:a 3 :b 4) (:a
5 :b 6))) => '(3 7 11)
(-map (-lambda (x) (let ((k (car x)) (v (cadr x))) (+ k v))) '((1 2) (3 4)
(5 6))) => '(3 7 11)
+ (funcall (-lambda ((a) (b)) (+ a b)) '(1 2 3) '(4 5 6)) => 5
(condition-case nil (progn (-lambda a t) (error "previous form should
error")) (error t)) => t))
(def-example-group "Side-effects"