[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 166bccd 333/426: Add -tree-map-nodes
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 166bccd 333/426: Add -tree-map-nodes |
Date: |
Tue, 04 Aug 2015 19:38:40 +0000 |
branch: externals/dash
commit 166bccdd6171f1f732c85a3e18434c8ee6e0ccb3
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -tree-map-nodes
---
README.md | 15 +++++++++++++++
dash.el | 18 ++++++++++++++++++
dev/examples.el | 8 ++++++++
3 files changed, 41 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index a6ce378..2e58b9d 100644
--- a/README.md
+++ b/README.md
@@ -193,6 +193,7 @@ Functions pretending lists are trees.
* [-tree-seq](#-tree-seq-branch-children-tree) `(branch children tree)`
* [-tree-map](#-tree-map-fn-tree) `(fn tree)`
+* [-tree-map-nodes](#-tree-map-nodes-pred-fun-tree) `(pred fun tree)`
* [-tree-reduce](#-tree-reduce-fn-tree) `(fn tree)`
* [-tree-reduce-from](#-tree-reduce-from-fn-init-value-tree) `(fn init-value
tree)`
* [-tree-mapreduce](#-tree-mapreduce-fn-folder-tree) `(fn folder tree)`
@@ -1491,6 +1492,20 @@ Apply `fn` to each element of `tree` while preserving
the tree structure.
(--tree-map (length it) '("<body>" ("<p>" "text" "</p>") "</body>")) ;; => '(6
(3 4 4) 7)
```
+#### -tree-map-nodes `(pred fun tree)`
+
+Call `fun` on each node of `tree` that satisfies `pred`.
+
+If `pred` returns nil, continue descending down this node. If `pred`
+returns non-nil, apply `fun` to this node and do not descend
+further.
+
+```cl
+(-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4 (5 [6
7] 8))) ;; => '(1 5 4 (5 13 8))
+(-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6 :bar)
:baz 8))) ;; => '(1 ":foo" 4 ((5 6 ":bar") ":baz" 8))
+(--tree-map-nodes (eq (car-safe it) 'add-mode) (-concat it (list :mode
'emacs-lisp-mode)) '(with-mode emacs-lisp-mode (foo bar) (add-mode a b) (baz
(add-mode c d)))) ;; => '(with-mode emacs-lisp-mode (foo bar) (add-mode a b
:mode emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode)))
+```
+
#### -tree-reduce `(fn tree)`
Use `fn` to reduce elements of list `tree`.
diff --git a/dash.el b/dash.el
index 8e72182..43e0349 100644
--- a/dash.el
+++ b/dash.el
@@ -1508,6 +1508,21 @@ See `-reduce-r' for how exactly are lists of zero or one
element handled."
(declare (debug (form form)))
`(-tree-reduce (lambda (it acc) ,form) ,tree))
+(defun -tree-map-nodes (pred fun tree)
+ "Call FUN on each node of TREE that satisfies PRED.
+
+If PRED returns nil, continue descending down this node. If PRED
+returns non-nil, apply FUN to this node and do not descend
+further."
+ (if (funcall pred tree)
+ (funcall fun tree)
+ (if (listp tree)
+ (-map (lambda (x) (-tree-map-nodes pred fun x)) tree)
+ tree)))
+
+(defmacro --tree-map-nodes (pred form tree)
+ "Anaphoric form of `-tree-map-nodes'."
+ `(-tree-map-nodes (lambda (it) ,pred) (lambda (it) ,form) ,tree))
(defun -tree-seq (branch children tree)
"Return a sequence of the nodes in TREE, in depth-first search order.
@@ -1527,6 +1542,7 @@ Non-branch nodes are simply copied."
(defmacro --tree-seq (branch children tree)
"Anaphoric form of `-tree-seq'."
`(-tree-seq (lambda (it) ,branch) (lambda (it) ,children) ,tree))
+
(defun -clone (list)
"Create a deep copy of LIST.
The new list has the same elements and structure but all cons are
@@ -1730,6 +1746,8 @@ structure such as plist or alist."
"--tree-reduce"
"-tree-seq"
"--tree-seq"
+ "-tree-map-nodes"
+ "--tree-map-nodes"
"-clone"
"-rpartial"
"-juxt"
diff --git a/dev/examples.el b/dev/examples.el
index 2eebe59..fba6639 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -586,6 +586,14 @@ new list."
(--tree-map 1 '(1 2 (3 4) (5 6))) => '(1 1 (1 1) (1 1))
(--tree-map (cdr it) '((1 . 2) (3 . 4) (5 . 6))) => '(2 4 6))
+ (defexamples -tree-map-nodes
+ (-tree-map-nodes 'vectorp (lambda (x) (-sum (append x nil))) '(1 [2 3] 4
(5 [6 7] 8))) => '(1 5 4 (5 13 8))
+ (-tree-map-nodes 'keywordp (lambda (x) (symbol-name x)) '(1 :foo 4 ((5 6
:bar) :baz 8))) => '(1 ":foo" 4 ((5 6 ":bar") ":baz" 8))
+ (--tree-map-nodes
+ (eq (car-safe it) 'add-mode)
+ (-concat it (list :mode 'emacs-lisp-mode))
+ '(with-mode emacs-lisp-mode (foo bar) (add-mode a b) (baz (add-mode c
d)))) => '(with-mode emacs-lisp-mode (foo bar) (add-mode a b :mode
emacs-lisp-mode) (baz (add-mode c d :mode emacs-lisp-mode))))
+
(defexamples -tree-reduce
(-tree-reduce '+ '(1 (2 3) (4 5))) => 15
(-tree-reduce 'concat '("strings" (" on" " various") ((" levels")))) =>
"strings on various levels"
- [elpa] externals/dash 2ecc073 316/426: Reorder the partition examples to follow more logical order, add tests, (continued)
- [elpa] externals/dash 2ecc073 316/426: Reorder the partition examples to follow more logical order, add tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash eea928a 320/426: Add -replace, Phillip Lord, 2015/08/04
- [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 <=
- [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, 2015/08/04
- [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