55 KiB
55 KiB
In [1]:
from notebook_preamble import D, J, V, define, DefinitionWrapperIn [2]:
define('Tree-new == swap [[] []] cons cons')In [3]:
J('"v" "k" Tree-new')['k' 'v' [] []]
In [4]:
define('P == pop roll> pop first')In [5]:
J('["old_key" 23 [] []] 17 "new_key" ["..."] P')'new_key' 'old_key'
In [6]:
define('T == cons cons [dipdd] cons infra')In [7]:
J('["old_k" "old_value" "left" "right"] "new_value" "new_key" ["Tree-add"] T')['old_k' 'old_value' 'left' 'Tree-add' 'new_key' 'new_value' 'right']
In [8]:
define('E == [P <] [Te] [Ee] ifte')In [9]:
define('Te == cons cons [dipd] cons infra')In [10]:
J('["old_k" "old_value" "left" "right"] "new_value" "new_key" ["Tree-add"] Te')['old_k' 'old_value' 'Tree-add' 'new_key' 'new_value' 'left' 'right']
In [11]:
define('Ee == pop swap roll< rest rest cons cons')In [12]:
J('["k" "old_value" "left" "right"] "new_value" "k" ["Tree-add"] Ee')['k' 'new_value' 'left' 'right']
In [13]:
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [[P >] [T] [E] ifte] genrec')In [14]:
J('[] 23 "b" Tree-add') # Initial['b' 23 [] []]
In [15]:
J('["b" 23 [] []] 88 "c" Tree-add') # Greater than['b' 23 [] ['c' 88 [] []]]
In [16]:
J('["b" 23 [] []] 88 "a" Tree-add') # Less than['b' 23 ['a' 88 [] []] []]
In [17]:
J('["b" 23 [] []] 88 "b" Tree-add') # Equal to['b' 88 [] []]
In [18]:
J('[] 23 "b" Tree-add 88 "a" Tree-add 44 "c" Tree-add') # Series.['b' 23 ['a' 88 [] []] ['c' 44 [] []]]
In [19]:
J('[] [[23 "b"] [88 "a"] [44 "c"]] [i Tree-add] step')['b' 23 ['a' 88 [] []] ['c' 44 [] []]]
In [20]:
J("1 0 ['G'] ['E'] ['L'] cmp")'G'
In [21]:
J("1 1 ['G'] ['E'] ['L'] cmp")'E'
In [22]:
J("0 1 ['G'] ['E'] ['L'] cmp")'L'
In [23]:
define('P == over [popop popop first] nullary')In [24]:
define('Tree-add == [popop not] [[pop] dipd Tree-new] [] [P [T] [Ee] [Te] cmp] genrec')In [25]:
J('[] 23 "b" Tree-add 88 "a" Tree-add 44 "c" Tree-add') # Still works.['b' 23 ['a' 88 [] []] ['c' 44 [] []]]
In [26]:
define('Tree-iter == [not] [pop] roll< [dupdip rest rest] cons [step] genrec')In [27]:
J('[] [foo] Tree-iter') # It doesn't matter what F is as it won't be used.In [28]:
J("['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [first] Tree-iter")'b' 'a' 'c'
In [29]:
J("['b' 23 ['a' 88 [] []] ['c' 44 [] []]] [second] Tree-iter")23 88 44
In [30]:
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 [] []]] []]] []]]
In [31]:
define('to_set == [] swap [0 swap Tree-add] step')In [32]:
J('[3 9 5 2 8 6 7 8 4] to_set')[3 0 [2 0 [] []] [9 0 [5 0 [4 0 [] []] [8 0 [6 0 [] [7 0 [] []]] []]] []]]
In [33]:
define('unique == [to_set [first] Tree-iter] cons run')In [34]:
J('[3 9 3 5 2 9 8 8 8 6 2 7 8 4 3] unique') # Filter duplicate items.[7 6 8 4 5 9 2 3]
In [35]:
#define('Tree-iter-order == [not] [pop] [dup third] [[cons dip] dupdip [[first] dupdip] dip [rest rest rest first] dip i] genrec')
DefinitionWrapper.add_definitions('''
fourth == rest rest rest first
proc_left == [cons dip] dupdip
proc_current == [[first] dupdip] dip
proc_right == [fourth] dip i
Tree-iter-order == [not] [pop] [dup third] [proc_left proc_current proc_right] genrec
''', D)
In [36]:
J('[3 9 5 2 8 6 7 8 4] to_set Tree-iter-order')2 3 4 5 6 7 8 9
In [37]:
# 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
# definitions that are only in scope for a given definition. I don't want to implement
# that (yet) so...
define('''
Tree-get == [pop not] swap [] [
over [pop popop first] nullary
[[fourth] dipd i]
[popop second]
[[third] dipd i]
cmp
] genrec
''')In [38]:
J('["gary" 23 [] []] "mike" [popd " not in tree" +] Tree-get')'mike not in tree'
In [39]:
J('["gary" 23 [] []] "gary" [popop "err"] Tree-get')23
In [40]:
J('''
[] [[0 'a'] [1 'b'] [2 'c']] [i Tree-add] step
'c' [popop 'not found'] Tree-get
''')2
In [41]:
J('''
[] [[0 'a'] [1 'b'] [2 'c']] [i Tree-add] step
'd' [popop 'not found'] Tree-get
''')'not found'
Warning:
Output truncated. This notebook contains too many cells to display efficiently.