a contingent existence predicated on the disciplined use of these
functions on otherwise undistinguished Joy datastructures.)
-.. code:: python
+.. code:: ipython2
from notebook_preamble import D, J, V, define, DefinitionWrapper
Tree-new == swap [[] []] cons cons
-.. code:: python
+.. code:: ipython2
define('Tree-new == swap [[] []] cons cons')
-.. code:: python
+.. code:: ipython2
J('"v" "k" Tree-new')
P < == pop roll> pop first <
P == pop roll> pop first
-.. code:: python
+.. code:: ipython2
define('P == pop roll> pop first')
-.. code:: python
+.. code:: ipython2
J('["old_key" 23 [] []] 17 "new_key" ["..."] P')
T == cons cons [dipdd] cons infra
-.. code:: python
+.. code:: ipython2
define('T == cons cons [dipdd] cons infra')
-.. code:: python
+.. code:: ipython2
J('["old_k" "old_value" "left" "right"] "new_value" "new_key" ["Tree-add"] T')
[key_n value_n left right] value key [Tree-add] E
[key_n value_n left right] value key [Tree-add] [P <] [Te] [Ee] ifte
-.. code:: python
+.. code:: ipython2
define('E == [P <] [Te] [Ee] ifte')
Te == cons cons [dipd] cons infra
-.. code:: python
+.. code:: ipython2
define('Te == cons cons [dipd] cons infra')
-.. code:: python
+.. code:: ipython2
J('["old_k" "old_value" "left" "right"] "new_value" "new_key" ["Tree-add"] Te')
key new_value [ left right] cons cons
[key new_value left right]
-.. code:: python
+.. code:: ipython2
define('Ee == pop swap roll< rest rest cons cons')
-.. code:: python
+.. code:: ipython2
J('["k" "old_value" "left" "right"] "new_value" "k" ["Tree-add"] Ee')
Tree-add == [popop not] [[pop] dipd Tree-new] [] [R] genrec
-.. code:: python
+.. code:: ipython2
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec')
Examples
~~~~~~~~
-.. code:: python
+.. code:: ipython2
J('[] 23 "b" Tree-add') # Initial
['b' 23 [] []]
-.. code:: python
+.. code:: ipython2
J('["b" 23 [] []] 88 "c" Tree-add') # Greater than
['b' 23 [] ['c' 88 [] []]]
-.. code:: python
+.. code:: ipython2
J('["b" 23 [] []] 88 "a" Tree-add') # Less than
['b' 23 ['a' 88 [] []] []]
-.. code:: python
+.. code:: ipython2
J('["b" 23 [] []] 88 "b" Tree-add') # Equal to
['b' 88 [] []]
-.. code:: python
+.. code:: ipython2
J('[] 23 "b" Tree-add 88 "a" Tree-add 44 "c" Tree-add') # Series.
['b' 23 ['a' 88 [] []] ['c' 44 [] []]]
-.. code:: python
+.. code:: ipython2
J('[] [[23 "b"] [88 "a"] [44 "c"]] [i Tree-add] step')
------------------------- a < b
L
-.. code:: python
+.. code:: ipython2
J("1 0 ['G'] ['E'] ['L'] cmp")
'G'
-.. code:: python
+.. code:: ipython2
J("1 1 ['G'] ['E'] ['L'] cmp")
'E'
-.. code:: python
+.. code:: ipython2
J("0 1 ['G'] ['E'] ['L'] cmp")
P == over [popop popop first] nullary
-.. code:: python
+.. code:: ipython2
define('P == over [popop popop first] nullary')
Tree-add == [popop not] [[pop] dipd Tree-new] [] [P [T] [Ee] [Te] cmp] genrec
-.. code:: python
+.. code:: ipython2
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [P [T] [Ee] [Te] cmp] genrec')
-.. code:: python
+.. code:: ipython2
J('[] 23 "b" Tree-add 88 "a" Tree-add 44 "c" Tree-add') # Still works.
Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec
-.. code:: python
+.. code:: ipython2
define('Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')
Examples
~~~~~~~~
-.. code:: python
+.. code:: ipython2
J('[] [foo] Tree-iter') # It doesn't matter what F is as it won't be used.
-.. code:: python
+.. code:: ipython2
J("['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [first] Tree-iter")
'b' 'a' 'c'
-.. code:: python
+.. code:: ipython2
J("['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [second] Tree-iter")
`:math:`O(\log_2 N)` <https://en.wikipedia.org/wiki/Binary_search_tree#cite_note-2>`__
time.
-.. code:: python
+.. code:: ipython2
J('[] [3 9 5 2 8 6 7 8 4] [0 swap Tree-add] step')
[3 0 [2 0 [] []] [9 0 [5 0 [4 0 [] []] [8 0 [6 0 [] [7 0 [] []]] []]] []]]
-.. code:: python
+.. code:: ipython2
define('to_set == [] swap [0 swap Tree-add] step')
-.. code:: python
+.. code:: ipython2
J('[3 9 5 2 8 6 7 8 4] to_set')
And with that we can write a little program ``unique`` to remove
duplicate items from a list.
-.. code:: python
+.. code:: ipython2
define('unique == [to_set [first] Tree-iter] cons run')
-.. code:: python
+.. code:: ipython2
J('[3 9 3 5 2 9 8 8 8 6 2 7 8 4 3] unique') # Filter duplicate items.
Now we can sort sequences.
-.. code:: python
+.. code:: ipython2
#define('Tree-iter-order == [not] [pop] [dup third] [[cons dip] dupdip [[first] dupdip] dip [rest rest rest first] dip i] genrec')
-.. code:: python
+.. code:: ipython2
J('[3 9 5 2 8 6 7 8 4] to_set Tree-iter-order')
Tree-get == [pop not] swap [] [P [T>] [E] [T<] cmp] genrec
-.. code:: python
+.. code:: ipython2
# I don't want to deal with name conflicts with the above so I'm inlining everything here.
# The original Joy system has "hide" which is a meta-command which allows you to use named
] genrec
''')
-.. code:: python
+.. code:: ipython2
J('["gary" 23 [] []] "mike" [popd " not in tree" +] Tree-get')
'mike not in tree'
-.. code:: python
+.. code:: ipython2
J('["gary" 23 [] []] "gary" [popop "err"] Tree-get')
23
-.. code:: python
+.. code:: ipython2
J('''
2
-.. code:: python
+.. code:: ipython2
J('''
By the standards of the code I’ve written so far, this is a *huge* Joy
program.
-.. code:: python
+.. code:: ipython2
DefinitionWrapper.add_definitions('''
first_two == uncons uncons pop
Tree-Delete == [pop not] [pop] [R0] [R1] genrec
''', D)
-.. code:: python
+.. code:: ipython2
J("['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'c' Tree-Delete ")
['a' 23 [] ['b' 88 [] []]]
-.. code:: python
+.. code:: ipython2
J("['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'b' Tree-Delete ")
['a' 23 [] ['c' 44 [] []]]
-.. code:: python
+.. code:: ipython2
J("['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'a' Tree-Delete ")
['b' 88 [] ['c' 44 [] []]]
-.. code:: python
+.. code:: ipython2
J("['a' 23 [] ['b' 88 [] ['c' 44 [] []]]] 'der' Tree-Delete ")
['a' 23 [] ['b' 88 [] ['c' 44 [] []]]]
-.. code:: python
+.. code:: ipython2
J('[] [4 2 3 1 6 7 5 ] [0 swap Tree-add] step')
[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]]
-.. code:: python
+.. code:: ipython2
J("[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]] 3 Tree-Delete ")
[4 0 [2 0 [1 0 [] []] []] [6 0 [5 0 [] []] [7 0 [] []]]]
-.. code:: python
+.. code:: ipython2
J("[4 0 [2 0 [1 0 [] []] [3 0 [] []]] [6 0 [5 0 [] []] [7 0 [] []]]] 4 Tree-Delete ")