6 from notebook_preamble import D, J, V
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 /
60 ### Replacing `sum` and `size` with "compiled" versions.
62 Both `sum` and `size` are [catamorphisms](https://en.wikipedia.org/wiki/Catamorphism), they each convert a sequence to a single value.
69 Given a quoted sequence of numbers return the sum.
71 sum == 0 swap [+] step
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()`.
88 from joy.library import SimpleFunctionWrapper, primitives
89 from joy.utils.stack import iter_stack
92 @SimpleFunctionWrapper
94 '''Return the size of the sequence on the stack.'''
95 sequence, stack = stack
97 for _ in iter_stack(sequence):
102 sum_ = next(p for p in primitives if p.name == 'sum')
105 Now we replace them old versions in the dictionary with the new versions and re-evaluate the expression.
109 old_sum, D['sum'] = D['sum'], sum_
110 old_size, D['size'] = D['size'], size
113 You can see that `size` and `sum` now execute in a single step.
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 /