OSDN Git Service

Minor docs edit.
[joypy/Thun.git] / docs / 4. Replacing Functions in the Dictionary.rst
1
2 Preamble
3 ~~~~~~~~
4
5 .. code:: ipython2
6
7     from notebook_preamble import D, J, V
8
9 A long trace
10 ~~~~~~~~~~~~
11
12 .. code:: ipython2
13
14     V('[23 18] average')
15
16
17 .. parsed-literal::
18
19                                       . [23 18] average
20                               [23 18] . average
21                               [23 18] . [sum 1.0 *] [size] cleave /
22                   [23 18] [sum 1.0 *] . [size] cleave /
23            [23 18] [sum 1.0 *] [size] . cleave /
24            [23 18] [sum 1.0 *] [size] . [i] app2 [popd] dip /
25        [23 18] [sum 1.0 *] [size] [i] . app2 [popd] dip /
26     [23 18] [[sum 1.0 *] [23 18]] [i] . infra first [[size] [23 18]] [i] infra first [popd] dip /
27                   [23 18] [sum 1.0 *] . i [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
28                               [23 18] . sum 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
29                                    41 . 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
30                                41 1.0 . * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
31                                  41.0 . [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
32                        41.0 [[23 18]] . swaack first [[size] [23 18]] [i] infra first [popd] dip /
33                        [23 18] [41.0] . first [[size] [23 18]] [i] infra first [popd] dip /
34                          [23 18] 41.0 . [[size] [23 18]] [i] infra first [popd] dip /
35         [23 18] 41.0 [[size] [23 18]] . [i] infra first [popd] dip /
36     [23 18] 41.0 [[size] [23 18]] [i] . infra first [popd] dip /
37                        [23 18] [size] . i [41.0 [23 18]] swaack first [popd] dip /
38                               [23 18] . size [41.0 [23 18]] swaack first [popd] dip /
39                               [23 18] . 0 swap [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
40                             [23 18] 0 . swap [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
41                             0 [23 18] . [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
42                    0 [23 18] [pop ++] . step [41.0 [23 18]] swaack first [popd] dip /
43                         0 23 [pop ++] . i [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
44                                  0 23 . pop ++ [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
45                                     0 . ++ [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
46                                     1 . [18] [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
47                                1 [18] . [pop ++] step [41.0 [23 18]] swaack first [popd] dip /
48                       1 [18] [pop ++] . step [41.0 [23 18]] swaack first [popd] dip /
49                         1 18 [pop ++] . i [41.0 [23 18]] swaack first [popd] dip /
50                                  1 18 . pop ++ [41.0 [23 18]] swaack first [popd] dip /
51                                     1 . ++ [41.0 [23 18]] swaack first [popd] dip /
52                                     2 . [41.0 [23 18]] swaack first [popd] dip /
53                      2 [41.0 [23 18]] . swaack first [popd] dip /
54                      [23 18] 41.0 [2] . first [popd] dip /
55                        [23 18] 41.0 2 . [popd] dip /
56                 [23 18] 41.0 2 [popd] . dip /
57                          [23 18] 41.0 . popd 2 /
58                                  41.0 . 2 /
59                                41.0 2 . /
60                                  20.5 . 
61
62
63 Replacing ``sum`` and ``size`` with "compiled" versions.
64 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65
66 Both ``sum`` and ``size`` are
67 `catamorphisms <https://en.wikipedia.org/wiki/Catamorphism>`__, they
68 each convert a sequence to a single value.
69
70 .. code:: ipython2
71
72     J('[sum] help')
73
74
75 .. parsed-literal::
76
77     Given a quoted sequence of numbers return the sum.
78     
79     sum == 0 swap [+] step
80     
81
82
83 .. code:: ipython2
84
85     J('[size] help')
86
87
88 .. parsed-literal::
89
90     0 swap [pop ++] step
91     
92
93
94 We can use "compiled" versions (they're not really compiled in this
95 case, they're hand-written in Python) to speed up evaluation and make
96 the trace more readable. The ``sum`` function is already in the library.
97 It gets shadowed by the definition version above during
98 ``initialize()``.
99
100 .. code:: ipython2
101
102     from joy.library import SimpleFunctionWrapper, primitives
103     from joy.utils.stack import iter_stack
104     
105     
106     @SimpleFunctionWrapper
107     def size(stack):
108         '''Return the size of the sequence on the stack.'''
109         sequence, stack = stack
110         n = 0
111         for _ in iter_stack(sequence):
112             n += 1
113         return n, stack
114     
115     
116     sum_ = next(p for p in primitives if p.name == 'sum')
117
118 Now we replace them old versions in the dictionary with the new versions
119 and re-evaluate the expression.
120
121 .. code:: ipython2
122
123     old_sum, D['sum'] = D['sum'], sum_
124     old_size, D['size'] = D['size'], size
125
126 You can see that ``size`` and ``sum`` now execute in a single step.
127
128 .. code:: ipython2
129
130     V('[23 18] average')
131
132
133 .. parsed-literal::
134
135                                       . [23 18] average
136                               [23 18] . average
137                               [23 18] . [sum 1.0 *] [size] cleave /
138                   [23 18] [sum 1.0 *] . [size] cleave /
139            [23 18] [sum 1.0 *] [size] . cleave /
140            [23 18] [sum 1.0 *] [size] . [i] app2 [popd] dip /
141        [23 18] [sum 1.0 *] [size] [i] . app2 [popd] dip /
142     [23 18] [[sum 1.0 *] [23 18]] [i] . infra first [[size] [23 18]] [i] infra first [popd] dip /
143                   [23 18] [sum 1.0 *] . i [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
144                               [23 18] . sum 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
145                                    41 . 1.0 * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
146                                41 1.0 . * [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
147                                  41.0 . [[23 18]] swaack first [[size] [23 18]] [i] infra first [popd] dip /
148                        41.0 [[23 18]] . swaack first [[size] [23 18]] [i] infra first [popd] dip /
149                        [23 18] [41.0] . first [[size] [23 18]] [i] infra first [popd] dip /
150                          [23 18] 41.0 . [[size] [23 18]] [i] infra first [popd] dip /
151         [23 18] 41.0 [[size] [23 18]] . [i] infra first [popd] dip /
152     [23 18] 41.0 [[size] [23 18]] [i] . infra first [popd] dip /
153                        [23 18] [size] . i [41.0 [23 18]] swaack first [popd] dip /
154                               [23 18] . size [41.0 [23 18]] swaack first [popd] dip /
155                                     2 . [41.0 [23 18]] swaack first [popd] dip /
156                      2 [41.0 [23 18]] . swaack first [popd] dip /
157                      [23 18] 41.0 [2] . first [popd] dip /
158                        [23 18] 41.0 2 . [popd] dip /
159                 [23 18] 41.0 2 [popd] . dip /
160                          [23 18] 41.0 . popd 2 /
161                                  41.0 . 2 /
162                                41.0 2 . /
163                                  20.5 . 
164