projects
/
nmutil.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
| inline |
side by side
(parent:
f74008f
)
add treereduce function
author
Luke Kenneth Casson Leighton
<lkcl@lkcl.net>
Tue, 26 May 2020 12:36:09 +0000
(13:36 +0100)
committer
Luke Kenneth Casson Leighton
<lkcl@lkcl.net>
Tue, 26 May 2020 12:36:09 +0000
(13:36 +0100)
src/nmutil/util.py
patch
|
blob
|
history
diff --git
a/src/nmutil/util.py
b/src/nmutil/util.py
index e0b09af164c27b6dd64c32e8cc6402ce0a647db4..28a8e13621fef0efb454f3624a8b4adf2942f275 100644
(file)
--- a/
src/nmutil/util.py
+++ b/
src/nmutil/util.py
@@
-8,3
+8,15
@@
def flatten(v):
yield from flatten(i)
else:
yield v
+
+# tree reduction function. operates recursively.
+def treereduce(tree, op, attr="data_o"):
+ #print ("treereduce", tree)
+ if not isinstance(tree, list):
+ return tree
+ if len(tree) == 1:
+ return getattr(tree[0], attr)
+ if len(tree) == 2:
+ return op(getattr(tree[0], attr), getattr(tree[1], attr))
+ s = len(tree) // 2 # splitpoint
+ return treereduce(op(tree[:s], op, attr), treereduce(tree[s:], op, attr))