emacs-elpa-diffs
[Top][All Lists]
Advanced

[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)))
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]