1 Replacing Functions in the Dictionary
2 =====================================
4 For now, there is no way to define new functions from within the Joy
5 language. All functions (and the interpreter) all accept and return a
6 dictionary parameter (in addition to the stack and expression) so that
7 we can implement e.g. a function that adds new functions to the
8 dictionary. However, there’s no function that does that. Adding a new
9 function to the dictionary is a meta-interpreter action, you have to do
10 it in Python, not Joy.
14 from notebook_preamble import D, J, V
28 [23 18] . [sum 1.0 *] [size] cleave /
29 [23 18] [sum 1.0 *] . [size] cleave /
30 [23 18] [sum 1.0 *] [size] . cleave /
31 [23 18] [sum 1.0 *] [size] . [i] app2 [popd] dip /
32 [23 18] [sum 1.0 *] [size] [i] . app2 [popd] dip /
33 [23 18] [[sum 1.0 *] [23 18]] [i] . infra first [[size] [23 18]] [i] infra first [popd] dip /
34 [23 18] [sum 1.0 *] . i [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
35 [23 18] . sum 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
36 41 . 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
37 41 1.0 . * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
38 41.0 . [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
39 41.0 [[23 18]] . swaack first [[size] [23 18]] [i] infra first [popd] dip /
40 [23 18] [41.0] . first [[size] [23 18]] [i] infra first [popd] dip /
41 [23 18] 41.0 . [[size] [23 18]] [i] infra first [popd] dip /
42 [23 18] 41.0 [[size] [23 18]] . [i] infra first [popd] dip /
43 [23 18] 41.0 [[size] [23 18]] [i] . infra first [popd] dip /
44 [23 18] [size] . i [41.0 [23 18]] swaack first [popd] dip /
45 [23 18] . size [41.0 [23 18]] swaack first [popd] dip /
46 [23 18] . 0 swap [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
47 [23 18] 0 . swap [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
48 0 [23 18] . [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
49 0 [23 18] [pop ++] . step [41.0 [23 18]] swaack first [popd] dip /
50 0 23 [pop ++] . i [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
51 0 23 . pop ++ [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
52 0 . ++ [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
53 1 . [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
54 1 [18] . [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
55 1 [18] [pop ++] . step [41.0 [23 18]] swaack first [popd] dip /
56 1 18 [pop ++] . i [41.0 [23 18]] swaack first [popd] dip /
57 1 18 . pop ++ [41.0 [23 18]] swaack first [popd] dip /
58 1 . ++ [41.0 [23 18]] swaack first [popd] dip /
59 2 . [41.0 [23 18]] swaack first [popd] dip /
60 2 [41.0 [23 18]] . swaack first [popd] dip /
61 [23 18] 41.0 [2] . first [popd] dip /
62 [23 18] 41.0 2 . [popd] dip /
63 [23 18] 41.0 2 [popd] . dip /
64 [23 18] 41.0 . popd 2 /
70 Replacing ``size`` with a Python version
71 ----------------------------------------
73 Both ``sum`` and ``size`` each convert a sequence to a single value.
77 sum == 0 swap [+] step
78 size == 0 swap [pop ++] step
80 An efficient ``sum`` function is already in the library. But for
81 ``size`` we can use a “compiled” version hand-written in Python to speed
82 up evaluation and make the trace more readable.
86 from joy.library import SimpleFunctionWrapper
87 from joy.utils.stack import iter_stack
90 @SimpleFunctionWrapper
92 '''Return the size of the sequence on the stack.'''
93 sequence, stack = stack
95 for _ in iter_stack(sequence):
99 Now we replace the old version in the dictionary with the new version,
100 and re-evaluate the expression.
109 You can see that ``size`` now executes in a single step.
120 [23 18] . [sum 1.0 *] [size] cleave /
121 [23 18] [sum 1.0 *] . [size] cleave /
122 [23 18] [sum 1.0 *] [size] . cleave /
123 [23 18] [sum 1.0 *] [size] . [i] app2 [popd] dip /
124 [23 18] [sum 1.0 *] [size] [i] . app2 [popd] dip /
125 [23 18] [[sum 1.0 *] [23 18]] [i] . infra first [[size] [23 18]] [i] infra first [popd] dip /
126 [23 18] [sum 1.0 *] . i [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
127 [23 18] . sum 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
128 41 . 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
129 41 1.0 . * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
130 41.0 . [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
131 41.0 [[23 18]] . swaack first [[size] [23 18]] [i] infra first [popd] dip /
132 [23 18] [41.0] . first [[size] [23 18]] [i] infra first [popd] dip /
133 [23 18] 41.0 . [[size] [23 18]] [i] infra first [popd] dip /
134 [23 18] 41.0 [[size] [23 18]] . [i] infra first [popd] dip /
135 [23 18] 41.0 [[size] [23 18]] [i] . infra first [popd] dip /
136 [23 18] [size] . i [41.0 [23 18]] swaack first [popd] dip /
137 [23 18] . size [41.0 [23 18]] swaack first [popd] dip /
138 2 . [41.0 [23 18]] swaack first [popd] dip /
139 2 [41.0 [23 18]] . swaack first [popd] dip /
140 [23 18] 41.0 [2] . first [popd] dip /
141 [23 18] 41.0 2 . [popd] dip /
142 [23 18] 41.0 2 [popd] . dip /
143 [23 18] 41.0 . popd 2 /