[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash f0831d3 339/426: Add -lambda
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash f0831d3 339/426: Add -lambda |
Date: |
Tue, 04 Aug 2015 19:38:43 +0000 |
branch: externals/dash
commit f0831d3ab9f5c6dcca6d52f8688d94d52af983fd
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -lambda
---
README.md | 23 +++++++++++++++++++++++
dash.el | 26 ++++++++++++++++++++++++++
dev/examples.el | 9 ++++++++-
3 files changed, 57 insertions(+), 1 deletions(-)
diff --git a/README.md b/README.md
index d47a2fe..a065eec 100644
--- a/README.md
+++ b/README.md
@@ -219,6 +219,7 @@ Convenient versions of `let` and `let*` constructs combined
with flow control.
* [-if-let*](#-if-let-vars-vals-then-rest-else) `(vars-vals then &rest else)`
* [-let](#-let-varlist-rest-body) `(varlist &rest body)`
* [-let*](#-let-varlist-rest-body) `(varlist &rest body)`
+* [-lambda](#-lambda-match-form-rest-body) `(match-form &rest body)`
### Side-effects
@@ -1802,6 +1803,28 @@ See `-let` for the list of all possible patterns.
(-let* (((&alist "foo" foo "bar" bar) (list (cons "foo" 1) (cons "bar" (list
'a 'b 'c)))) ((a b c) bar)) (list foo a b c bar)) ;; => '(1 a b c (a b c))
```
+#### -lambda `(match-form &rest body)`
+
+Return a lambda which destructures its input as `match-form` and executes
`body`.
+
+Note that you have to enclose the `match-form` in a pair of parens,
+such that:
+
+ (-lambda (x) body)
+ (-lambda (x y ...) body)
+
+has the usual semantics of `lambda`. Furthermore, these get
+translated into normal lambda, so there is no performance
+penalty.
+
+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)
+```
+
## Side-effects
diff --git a/dash.el b/dash.el
index 6fd4fdb..9b32668 100644
--- a/dash.el
+++ b/dash.el
@@ -1404,6 +1404,31 @@ Key/value stores:
`(let ,inputs
(-let* ,new-varlist ,@body)))))
+(defmacro -lambda (match-form &rest body)
+ "Return a lambda which destructures its input as MATCH-FORM and executes
BODY.
+
+Note that you have to enclose the MATCH-FORM in a pair of parens,
+such that:
+
+ (-lambda (x) body)
+ (-lambda (x y ...) body)
+
+has the usual semantics of `lambda'. Furthermore, these get
+translated into normal lambda, so there is no performance
+penalty.
+
+See `-let' for the description of destructuring mechanism."
+ (cond
+ ((not (consp match-form))
+ (error "match-form must be a list"))
+ ;; no destructuring, so just return regular lambda to make things faster
+ ((and (consp match-form)
+ (symbolp (car match-form)))
+ `(lambda ,match-form ,@body))
+ (t
+ `(lambda (x)
+ (-let* ((,@match-form x)) ,@body)))))
+
(defun -distinct (list)
"Return a new list with all duplicates removed.
The test for equality is done with `equal',
@@ -1951,6 +1976,7 @@ structure such as plist or alist."
"--if-let"
"-let*"
"-let"
+ "-lambda"
"-distinct"
"-uniq"
"-union"
diff --git a/dev/examples.el b/dev/examples.el
index fa9fdd5..2bd0ee1 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -752,7 +752,14 @@ new list."
(-let* (((a . b) a)
((c . d) b)) ;; b here comes from above binding
(list a b c d))) => '(1 (2 3) 2 (3))
- (-let* ((a "foo") (b a)) (list a b)) => '("foo" "foo")))
+ (-let* ((a "foo") (b a)) (list a b)) => '("foo" "foo"))
+
+ (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)
+ (-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)
+ (condition-case nil (progn (-lambda a t) (error "previous form should
error")) (error t)) => t))
(def-example-group "Side-effects"
"Functions iterating over lists for side-effect only."
- [elpa] externals/dash d9879e9 329/426: Release 2.8.0, (continued)
- [elpa] externals/dash d9879e9 329/426: Release 2.8.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8873a0 327/426: Update readme-template with changes from last release, Phillip Lord, 2015/08/04
- [elpa] externals/dash 25c114c 330/426: Implementing a shallow copy counterpart to -clone., Phillip Lord, 2015/08/04
- [elpa] externals/dash f257fb9 328/426: Add -butlast, Phillip Lord, 2015/08/04
- [elpa] externals/dash 174747e 323/426: Add -splice and -splice-list, Phillip Lord, 2015/08/04
- [elpa] externals/dash ab99be6 335/426: Add -fix, Phillip Lord, 2015/08/04
- [elpa] externals/dash b1e585d 337/426: Update copyright notice, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3adad97 334/426: Add -non-nil, Phillip Lord, 2015/08/04
- [elpa] externals/dash 166bccd 333/426: Add -tree-map-nodes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 05fa92f 342/426: [-let] Transpose nreverse/flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0831d3 339/426: Add -lambda,
Phillip Lord <=
- [elpa] externals/dash 9ebcce6 325/426: Add some niceties to font-locking of examples, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6fc652d 331/426: Merge pull request #95 from Wilfred/shallow-copy-function, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5637bd6 322/426: Add alias from -find to -first, Phillip Lord, 2015/08/04
- [elpa] externals/dash a4be872 338/426: Add `-let` and `-let*`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 947ffda 341/426: Add support for &rest match for non-list sequences (like . for improper lists), Phillip Lord, 2015/08/04
- [elpa] externals/dash 9065e1b 344/426: [-let] Final cdr shift optimization, Phillip Lord, 2015/08/04
- [elpa] externals/dash d37947a 332/426: Add -tree-seq, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51a0c9f 350/426: [-let] Fix expansion of _ symbols in vector matcher, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f9fc41 340/426: Add support for multiple input arguments to -lambda, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7f2b3c7 343/426: [-lambda] test all match-forms before converting to regular lambda, Phillip Lord, 2015/08/04