[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 0fc5d73 351/439: [-let] Make the cons matcher temp
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 0fc5d73 351/439: [-let] Make the cons matcher temp-bind as late as possible |
Date: |
Tue, 04 Aug 2015 20:30:14 +0000 |
branch: externals/dash
commit 0fc5d7394ab222bcc765741f4a92a5d69efce192
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
[-let] Make the cons matcher temp-bind as late as possible
---
dash.el | 28 +++++++++++++++++++++++-----
dev/examples.el | 22 +++++++++++++++++++++-
2 files changed, 44 insertions(+), 6 deletions(-)
diff --git a/dash.el b/dash.el
index c456b88..6fa8454 100644
--- a/dash.el
+++ b/dash.el
@@ -1174,11 +1174,6 @@ otherwise do ELSE."
`(let ((it ,val))
(if it ,then ,@else)))
-(defun dash--match-cons (match-form source)
- "Setup a cons matching environment and call the real matcher."
- (let ((s (make-symbol "--dash-source--")))
- (cons (list s source) (dash--match-cons-1 match-form s))))
-
(defun dash--match-cons-skip-cdr (skip-cdr source)
"Helper function generating idiomatic shifting code."
(cond
@@ -1209,6 +1204,29 @@ otherwise do ELSE."
(t
`(nthcdr ,skip-cdr ,source))))
+(defun dash--match-cons (match-form source)
+ "Setup a cons matching environment and call the real matcher."
+ (let ((s (make-symbol "--dash-source--"))
+ (n 0)
+ (m match-form))
+ (while (and (consp m)
+ (symbolp (car m))
+ (eq (aref (symbol-name (car m)) 0) ?_))
+ (setq n (1+ n)) (!cdr m))
+ (cond
+ ;; handle improper lists
+ ((and (consp m)
+ (not (cdr m)))
+ (dash--match (car m) (dash--match-cons-get-car n source)))
+ ;; handle other special types
+ ((> n 0)
+ (dash--match m (dash--match-cons-get-cdr n source)))
+ ;; this is the only entry-point for dash--match-cons-1, that's
+ ;; why we can't simply use the above branch, it would produce
+ ;; infinite recursion
+ (t
+ (cons (list s source) (dash--match-cons-1 match-form s))))))
+
(defun dash--match-cons-1 (match-form source &optional props)
"Match MATCH-FORM against SOURCE.
diff --git a/dev/examples.el b/dev/examples.el
index ba09d1f..ee03c33 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -768,7 +768,27 @@ new list."
(-let [(_ a _ _) (list 1 2 3 4 5)] a) => 2
(-let [(_ . b) (cons 1 2)] b) => 2
(-let [([a b c d] . e) (cons (vector 1 2 3 4) 5)] (list a b c d e)) => '(1
2 3 4 5)
- (-let [([a b c d] _ . e) (cons (vector 1 2 3 4) (cons 5 6))] (list a b c d
e)) => '(1 2 3 4 6))
+ (-let [([a b c d] _ . e) (cons (vector 1 2 3 4) (cons 5 6))] (list a b c d
e)) => '(1 2 3 4 6)
+ ;; late-binding optimization
+ (-let [(((a))) (list (list (list 1 2) 3) 4)] a) => 1
+ (-let [(((&plist :foo a :bar b))) (list (list (list :bar 1 :foo 2) 3) 4)]
(list a b)) => '(2 1)
+ (-let [(((a b) c) d) (list (list (list 1 2) 3) 4)] (list a b c d)) => '(1
2 3 4)
+ (-let [(((a b) c) . d) (list (list (list 1 2) 3) 4)] (list a b c d)) =>
'(1 2 3 (4))
+ (-let [(((a b) c)) (list (list (list 1 2) 3) 4)] (list a b c)) => '(1 2 3)
+ (-let [(a b c d) (list 1 2 3 4)] (list a b c d)) => '(1 2 3 4)
+ (-let [(a) (list 1 2 3 4)] (list a)) => '(1)
+ (-let [(_ a) (list 1 2 3 4)] (list a)) => '(2)
+ (-let [(_ _ a) (list 1 2 3 4)] (list a)) => '(3)
+ (-let [(_ _ . a) (list 1 2 3 4)] a) => '(3 4)
+ (-let [(_ _ [a b]) (list 1 2 (vector 3 4))] (list a b)) => '(3 4)
+ (-let [(a _ _ b) (list 1 2 3 4 5 6 7 8)] (list a b)) => '(1 4)
+ (-let [(_ _ a _ _ b) (list 1 2 3 4 5 6 7 8)] (list a b)) => '(3 6)
+ (-let [(_ _ a _ _ . b) (list 1 2 3 4 5 6 7 8)] (cons a b)) => '(3 6 7 8)
+ (-let [(_ a _ b) (list 1 2 3 4)] (list a b)) => '(2 4)
+ (-let [(a b c (d e)) (list 1 2 3 (list 4 5))] (list a b c d e)) => '(1 2 3
4 5)
+ (-let [(_ _ (_ _ (_ _ a))) (list 1 2 (list 3 4 (list 5 6 7)))] a) => 7
+ (-let [(_ (_ (_ a))) (list 1 (list 2 (list 3 4)))] a) => 4
+ (-let [(_ _ . (&plist :foo a :bar b)) (list 1 2 :bar 2 :foo 1)] (list a
b)) => '(1 2))
(defexamples -let*
(-let* (((a . b) (cons 1 2))
- [elpa] externals/dash 05fa92f 342/439: [-let] Transpose nreverse/flatten, (continued)
- [elpa] externals/dash 05fa92f 342/439: [-let] Transpose nreverse/flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7f2b3c7 343/439: [-lambda] test all match-forms before converting to regular lambda, Phillip Lord, 2015/08/04
- [elpa] externals/dash 947ffda 341/439: Add support for &rest match for non-list sequences (like . for improper lists), Phillip Lord, 2015/08/04
- [elpa] externals/dash a4be872 338/439: Add `-let` and `-let*`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9065e1b 344/439: [-let] Final cdr shift optimization, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6f81492 346/439: [-lambda] Better error-handling, Phillip Lord, 2015/08/04
- [elpa] externals/dash 65a3736 349/439: [-let] Fix improper list non-symbol last argument handler, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4d67b25 345/439: [-let] Do not reinvent `pop', Phillip Lord, 2015/08/04
- [elpa] externals/dash f939201 347/439: [-let] Optimize shifting/binding of unused _ places, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51a0c9f 350/439: [-let] Fix expansion of _ symbols in vector matcher, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0fc5d73 351/439: [-let] Make the cons matcher temp-bind as late as possible,
Phillip Lord <=
- [elpa] externals/dash f7664c6 352/439: [-let] Add &keys support for cons matcher, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6f0bb7d 348/439: [-let] Fix dynamic scoping issue, Phillip Lord, 2015/08/04
- [elpa] externals/dash db7f651 353/439: [-let] Simplify the conditions in dash--match-cons-1, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ec1a02 354/439: [-let] Abstract the _ test into a function, Phillip Lord, 2015/08/04
- [elpa] externals/dash c1d555b 355/439: [-let] Optimize single-binding of vectors and kv, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0310e0e 359/439: Update docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 40849a7 357/439: [-let] Remove stale comment, Phillip Lord, 2015/08/04
- [elpa] externals/dash edb1e31 356/439: [-let] Add more tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4b63be1 362/439: Declare --mapcat macro before using it. #102, Phillip Lord, 2015/08/04
- [elpa] externals/dash 31f321a 358/439: Release 2.9.0, Phillip Lord, 2015/08/04