[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash d37947a 332/439: Add -tree-seq
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash d37947a 332/439: Add -tree-seq |
Date: |
Tue, 04 Aug 2015 20:29:51 +0000 |
branch: externals/dash
commit d37947abfa853af1f4f81072e0013598e57b5be6
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -tree-seq
---
README.md | 23 +++++++++++++++++++++--
dash.el | 21 +++++++++++++++++++++
dev/examples.el | 7 ++++++-
3 files changed, 48 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
index 8eb8e26..a6ce378 100644
--- a/README.md
+++ b/README.md
@@ -50,7 +50,7 @@ new list.
* [-splice](#-splice-pred-fun-list) `(pred fun list)`
* [-splice-list](#-splice-list-pred-new-list-list) `(pred new-list list)`
* [-mapcat](#-mapcat-fn-list) `(fn list)`
-* [-copy](#-copy-list) `(list)`
+* [-copy](#-copy-arg) `(arg)`
### Sublist selection
@@ -191,6 +191,7 @@ Other list functions not fit to be classified elsewhere.
Functions pretending lists are trees.
+* [-tree-seq](#-tree-seq-branch-children-tree) `(branch children tree)`
* [-tree-map](#-tree-map-fn-tree) `(fn tree)`
* [-tree-reduce](#-tree-reduce-fn-tree) `(fn tree)`
* [-tree-reduce-from](#-tree-reduce-from-fn-init-value-tree) `(fn init-value
tree)`
@@ -370,7 +371,7 @@ Thus function `fn` should return a list.
(--mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3)
```
-#### -copy `(list)`
+#### -copy `(arg)`
Create a shallow copy of `list`.
@@ -1462,6 +1463,24 @@ not, return a list with `args` as elements.
Functions pretending lists are trees.
+#### -tree-seq `(branch children tree)`
+
+Return a sequence of the nodes in `tree`, in depth-first search order.
+
+`branch` is a predicate of one argument that returns non-nil if the
+passed argument is a branch, that is, a node that can have children.
+
+`children` is a function of one argument that returns the children
+of the passed branch node.
+
+Non-branch nodes are simply copied.
+
+```cl
+(-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7)))) ;; => '((1 (2 3) 4 (5 (6
7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7)
+(-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7)))) ;; => '((1 (2 3) 4 (5 (6
7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1)
+(--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]]) ;; => '([1 [2
3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7)
+```
+
#### -tree-map `(fn tree)`
Apply `fn` to each element of `tree` while preserving the tree structure.
diff --git a/dash.el b/dash.el
index e83324d..8e72182 100644
--- a/dash.el
+++ b/dash.el
@@ -1508,6 +1508,25 @@ 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-seq (branch children tree)
+ "Return a sequence of the nodes in TREE, in depth-first search order.
+
+BRANCH is a predicate of one argument that returns non-nil if the
+passed argument is a branch, that is, a node that can have children.
+
+CHILDREN is a function of one argument that returns the children
+of the passed branch node.
+
+Non-branch nodes are simply copied."
+ (cons tree
+ (when (funcall branch tree)
+ (-mapcat (lambda (x) (-tree-seq branch children x))
+ (funcall children tree)))))
+
+(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
@@ -1709,6 +1728,8 @@ structure such as plist or alist."
"--tree-reduce-from"
"-tree-reduce"
"--tree-reduce"
+ "-tree-seq"
+ "--tree-seq"
"-clone"
"-rpartial"
"-juxt"
diff --git a/dev/examples.el b/dev/examples.el
index d42b05c..2eebe59 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -574,6 +574,11 @@ new list."
(def-example-group "Tree operations"
"Functions pretending lists are trees."
+ (defexamples -tree-seq
+ (-tree-seq 'listp 'identity '(1 (2 3) 4 (5 (6 7)))) => '((1 (2 3) 4 (5 (6
7))) 1 (2 3) 2 3 4 (5 (6 7)) 5 (6 7) 6 7)
+ (-tree-seq 'listp 'reverse '(1 (2 3) 4 (5 (6 7)))) => '((1 (2 3) 4 (5 (6
7))) (5 (6 7)) (6 7) 7 6 5 4 (2 3) 3 2 1)
+ (--tree-seq (vectorp it) (append it nil) [1 [2 3] 4 [5 [6 7]]]) => '([1 [2
3] 4 [5 [6 7]]] 1 [2 3] 2 3 4 [5 [6 7]] 5 [6 7] 6 7))
+
(defexamples -tree-map
(-tree-map '1+ '(1 (2 3) (4 (5 6) 7))) => '(2 (3 4) (5 (6 7) 8))
(-tree-map '(lambda (x) (cons x (expt 2 x))) '(1 (2 3) 4)) => '((1 . 2)
((2 . 4) (3 . 8)) (4 . 16))
@@ -624,7 +629,7 @@ new list."
"}"
'((elips-mode (foo (bar . booze)) (baz . qux)) (c-mode (foo .
bla) (bum . bam)))))
=> "{elips-mode : {foo : {bar -> booze}, baz -> qux}, c-mode : {foo ->
bla, bum -> bam}}")
-
+
(defexamples -clone
(let* ((a '(1 2 3)) (b (-clone a))) (nreverse a) b) => '(1 2 3)))
- [elpa] externals/dash 7e4adb5 319/439: Add missing keywords to highlight list, (continued)
- [elpa] externals/dash 7e4adb5 319/439: Add missing keywords to highlight list, Phillip Lord, 2015/08/04
- [elpa] externals/dash 174747e 323/439: Add -splice and -splice-list, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5637bd6 322/439: Add alias from -find to -first, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ebcce6 325/439: Add some niceties to font-locking of examples, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8873a0 327/439: Update readme-template with changes from last release, Phillip Lord, 2015/08/04
- [elpa] externals/dash f18a520 326/439: Release 2.7.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f257fb9 328/439: Add -butlast, Phillip Lord, 2015/08/04
- [elpa] externals/dash d9879e9 329/439: Release 2.8.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 25c114c 330/439: Implementing a shallow copy counterpart to -clone., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6fc652d 331/439: Merge pull request #95 from Wilfred/shallow-copy-function, Phillip Lord, 2015/08/04
- [elpa] externals/dash d37947a 332/439: Add -tree-seq,
Phillip Lord <=
- [elpa] externals/dash 166bccd 333/439: Add -tree-map-nodes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3adad97 334/439: Add -non-nil, Phillip Lord, 2015/08/04
- [elpa] externals/dash ab99be6 335/439: Add -fix, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2626840 336/439: Add -fixfn, Phillip Lord, 2015/08/04
- [elpa] externals/dash c6b92ae 324/439: Reorder the examples into more meaningful blocks, Phillip Lord, 2015/08/04
- [elpa] externals/dash b1e585d 337/439: Update copyright notice, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0831d3 339/439: Add -lambda, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f9fc41 340/439: Add support for multiple input arguments to -lambda, Phillip Lord, 2015/08/04
- [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