OSDN Git Service

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