Given a datastructure on the stack we can navigate through it, modify
it, and rebuild it using the “zipper” technique.
-.. code:: ipython2
+.. code:: python
from notebook_preamble import J, V, define
`trees <https://en.wikipedia.org/wiki/Tree_%28data_structure%29>`__ out
of sequences.
-.. code:: ipython2
+.. code:: python
J('[1 [2 [3 4 25 6] 7] 8]')
these a lot it would make sense to write Python versions for efficiency,
but see below.
-.. code:: ipython2
+.. code:: python
define('z-down == [] swap uncons swap')
define('z-up == swons swap shunt')
define('z-right == [swons] cons dip uncons swap')
define('z-left == swons [uncons swap] dip swap')
-.. code:: ipython2
+.. code:: python
V('[1 [2 [3 4 25 6] 7] 8] z-down')
[] [[2 [3 4 25 6] 7] 8] 1 .
-.. code:: ipython2
+.. code:: python
V('[] [[2 [3 4 25 6] 7] 8] 1 z-right')
[1] [8] [2 [3 4 25 6] 7] .
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2 [3 4 25 6] 7] z-down')
[1] [8] [] [[3 4 25 6] 7] 2
-.. code:: ipython2
+.. code:: python
J('[1] [8] [] [[3 4 25 6] 7] 2 z-right')
[1] [8] [2] [7] [3 4 25 6]
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2] [7] [3 4 25 6] z-down')
[1] [8] [2] [7] [] [4 25 6] 3
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2] [7] [] [4 25 6] 3 z-right')
[1] [8] [2] [7] [3] [25 6] 4
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2] [7] [3] [25 6] 4 z-right')
[1] [8] [2] [7] [4 3] [6] 25
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2] [7] [4 3] [6] 25 sqr')
[1] [8] [2] [7] [4 3] [6] 625
-.. code:: ipython2
+.. code:: python
V('[1] [8] [2] [7] [4 3] [6] 625 z-up')
[1] [8] [2] [7] [3 4 625 6] .
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2] [7] [3 4 625 6] z-up')
[1] [8] [2 [3 4 625 6] 7]
-.. code:: ipython2
+.. code:: python
J('[1] [8] [2 [3 4 625 6] 7] z-up')
In Joy we have the ``dip`` and ``infra`` combinators which can “target”
or “address” any particular item in a Joy tree structure.
-.. code:: ipython2
+.. code:: python
V('[1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] infra')
The ``Z`` function isn’t hard to make.
-.. code:: ipython2
+.. code:: python
define('Z == [[] cons cons] step i')
Here it is in action in a simplified scenario.
-.. code:: ipython2
+.. code:: python
V('1 [2 3 4] Z')
And here it is doing the main thing.
-.. code:: ipython2
+.. code:: python
J('[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z')