Minor docs edit.
This commit is contained in:
parent
c60d54d009
commit
3fb1e0ea81
|
|
@ -4,7 +4,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Preamble"
|
||||
"# Replacing Functions in the Dictionary"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -20,13 +20,15 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### A long trace"
|
||||
"## A long trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
|
|
@ -85,63 +87,33 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Replacing `sum` and `size` with \"compiled\" versions.\n",
|
||||
"## Replacing `size` with a Python version\n",
|
||||
"\n",
|
||||
"Both `sum` and `size` are [catamorphisms](https://en.wikipedia.org/wiki/Catamorphism), they each convert a sequence to a single value."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Given a quoted sequence of numbers return the sum.\n",
|
||||
"\n",
|
||||
"sum == 0 swap [+] step\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[sum] help')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0 swap [pop ++] step\n",
|
||||
"\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[size] help')"
|
||||
"Both `sum` and `size` each convert a sequence to a single value."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"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()`."
|
||||
" sum == 0 swap [+] step\n",
|
||||
" size == 0 swap [pop ++] step"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"An efficient `sum` function is already in the library. But for `size` we can use a “compiled” version hand-written in Python to speed up evaluation and make the trace more readable."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from joy.library import SimpleFunctionWrapper, primitives\n",
|
||||
"from joy.library import SimpleFunctionWrapper\n",
|
||||
"from joy.utils.stack import iter_stack\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
|
@ -152,39 +124,36 @@
|
|||
" n = 0\n",
|
||||
" for _ in iter_stack(sequence):\n",
|
||||
" n += 1\n",
|
||||
" return n, stack\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"sum_ = next(p for p in primitives if p.name == 'sum')"
|
||||
" return n, stack"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now we replace them old versions in the dictionary with the new versions and re-evaluate the expression."
|
||||
"Now we replace the old version in the dictionary with the new version, and re-evaluate the expression."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"old_sum, D['sum'] = D['sum'], sum_\n",
|
||||
"old_size, D['size'] = D['size'], size"
|
||||
"D['size'] = size"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"You can see that `size` and `sum` now execute in a single step."
|
||||
"## A shorter trace\n",
|
||||
"You can see that `size` now executes in a single step."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue