7 from notebook_preamble import D, J, V
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 /
63 Replacing ``sum`` and ``size`` with "compiled" versions.
64 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
66 Both ``sum`` and ``size`` are
67 `catamorphisms <https://en.wikipedia.org/wiki/Catamorphism>`__, they
68 each convert a sequence to a single value.
77 Given a quoted sequence of numbers return the sum.
79 sum == 0 swap [+] step
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
102 from joy.library import SimpleFunctionWrapper, primitives
103 from joy.utils.stack import iter_stack
106 @SimpleFunctionWrapper
108 '''Return the size of the sequence on the stack.'''
109 sequence, stack = stack
111 for _ in iter_stack(sequence):
116 sum_ = next(p for p in primitives if p.name == 'sum')
118 Now we replace them old versions in the dictionary with the new versions
119 and re-evaluate the expression.
123 old_sum, D['sum'] = D['sum'], sum_
124 old_size, D['size'] = D['size'], size
126 You can see that ``size`` and ``sum`` now execute in a single step.
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 /