OSDN Git Service

Recover the square spiral example code.
[joypy/Thun.git] / docs / sphinx_docs / _build / html / _sources / notebooks / Replacing.rst.txt
1 Replacing Functions in the Dictionary
2 =====================================
3
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.
11
12 .. code:: ipython2
13
14     from notebook_preamble import D, J, V
15
16 A long trace
17 ------------
18
19 .. code:: ipython2
20
21     V('[23 18] average')
22
23
24 .. parsed-literal::
25
26                                       . [23 18] average
27                               [23 18] . average
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 /
65                                  41.0 . 2 /
66                                41.0 2 . /
67                                  20.5 . 
68
69
70 Replacing ``size`` with a Python version
71 ----------------------------------------
72
73 Both ``sum`` and ``size`` each convert a sequence to a single value.
74
75 ::
76
77     sum == 0 swap [+] step
78    size == 0 swap [pop ++] step
79
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.
83
84 .. code:: ipython2
85
86     from joy.library import SimpleFunctionWrapper
87     from joy.utils.stack import iter_stack
88     
89     
90     @SimpleFunctionWrapper
91     def size(stack):
92         '''Return the size of the sequence on the stack.'''
93         sequence, stack = stack
94         n = 0
95         for _ in iter_stack(sequence):
96             n += 1
97         return n, stack
98
99 Now we replace the old version in the dictionary with the new version,
100 and re-evaluate the expression.
101
102 .. code:: ipython2
103
104     D['size'] = size
105
106 A shorter trace
107 ---------------
108
109 You can see that ``size`` now executes in a single step.
110
111 .. code:: ipython2
112
113     V('[23 18] average')
114
115
116 .. parsed-literal::
117
118                                       . [23 18] average
119                               [23 18] . average
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 /
144                                  41.0 . 2 /
145                                41.0 2 . /
146                                  20.5 . 
147