Define ``treestep``
-------------------
-.. code:: ipython2
+.. code:: python
from notebook_preamble import D, J, V, define, DefinitionWrapper
-.. code:: ipython2
+.. code:: python
DefinitionWrapper.add_definitions('''
sumtree == [pop 0] [] [sum +] treestep
-.. code:: ipython2
+.. code:: python
define('sumtree == [pop 0] [] [sum +] treestep')
------------------------------------
0
-.. code:: ipython2
+.. code:: python
J('[] sumtree') # Empty tree.
n m +
n+m
-.. code:: ipython2
+.. code:: python
J('[23] sumtree') # No child trees.
23
-.. code:: ipython2
+.. code:: python
J('[23 []] sumtree') # Child tree, empty.
23
-.. code:: ipython2
+.. code:: python
J('[23 [2 [4]] [3]] sumtree') # Non-empty child trees.
32
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] sumtree') # Etc...
49
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] [pop 0] [] [cons sum] treestep') # Alternate "spelling".
49
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 23] [cons] treestep') # Replace each node.
[23 [23 [23] [23]] [23] [23 []]]
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 1] [cons] treestep')
[1 [1 [1] [1]] [1] [1 []]]
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] [] [pop 1] [cons] treestep sumtree')
6
-.. code:: ipython2
+.. code:: python
J('[23 [2 [8] [9]] [3] [4 []]] [pop 0] [pop 1] [sum +] treestep') # Combine replace and sum into one function.
6
-.. code:: ipython2
+.. code:: python
J('[4 [3 [] [7]]] [pop 0] [pop 1] [sum +] treestep') # Combine replace and sum into one function.
This doesn’t quite work:
-.. code:: ipython2
+.. code:: python
J('[[3 0] [[2 0] [][]] [[9 0] [[5 0] [[4 0] [][]] [[8 0] [[6 0] [] [[7 0] [][]]][]]][]]] ["B"] [first] [i] treestep')
[] [first] [flatten cons] treestep
-.. code:: ipython2
+.. code:: python
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [first] [flatten cons] treestep')
[] [i roll< swons concat] [first] treestep
-.. code:: ipython2
+.. code:: python
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [uncons pop] [i roll< swons concat] treestep')
[key value] N [left right] [K] C
-.. code:: ipython2
+.. code:: python
J('[["key" "value"] ["left"] ["right"] ] ["B"] ["N"] ["C"] treegrind')
Iteration through the nodes
-.. code:: ipython2
+.. code:: python
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [pop] ["N"] [step] treegrind')
Sum the nodes’ keys.
-.. code:: ipython2
+.. code:: python
J('0 [[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [pop] [first +] [step] treegrind')
Rebuild the tree using ``map`` (imitating ``treestep``.)
-.. code:: ipython2
+.. code:: python
J('[[3 0] [[2 0] [] []] [[9 0] [[5 0] [[4 0] [] []] [[8 0] [[6 0] [] [[7 0] [] []]] []]] []]] [] [[100 +] infra] [map cons] treegrind')
To me, that seems simpler than the ``genrec`` version.
-.. code:: ipython2
+.. code:: python
DefinitionWrapper.add_definitions('''
''', D)
-.. code:: ipython2
+.. code:: python
J('''\
15
-.. code:: ipython2
+.. code:: python
J('''\