Minor docs edit.

This commit is contained in:
Simon Forman 2018-06-06 11:45:33 -07:00
parent c60d54d009
commit 3fb1e0ea81
1 changed files with 25 additions and 56 deletions

View File

@ -4,7 +4,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Preamble" "# Replacing Functions in the Dictionary"
] ]
}, },
{ {
@ -20,13 +20,15 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### A long trace" "## A long trace"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": {}, "metadata": {
"scrolled": true
},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
@ -85,63 +87,33 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Replacing `sum` and `size` with \"compiled\" versions.\n", "## Replacing `size` with a Python version\n",
"\n", "\n",
"Both `sum` and `size` are [catamorphisms](https://en.wikipedia.org/wiki/Catamorphism), they each convert a sequence to a single value." "Both `sum` and `size` 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')"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 5, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from joy.library import SimpleFunctionWrapper, primitives\n", "from joy.library import SimpleFunctionWrapper\n",
"from joy.utils.stack import iter_stack\n", "from joy.utils.stack import iter_stack\n",
"\n", "\n",
"\n", "\n",
@ -152,39 +124,36 @@
" n = 0\n", " n = 0\n",
" for _ in iter_stack(sequence):\n", " for _ in iter_stack(sequence):\n",
" n += 1\n", " n += 1\n",
" return n, stack\n", " return n, stack"
"\n",
"\n",
"sum_ = next(p for p in primitives if p.name == 'sum')"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 6, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"old_sum, D['sum'] = D['sum'], sum_\n", "D['size'] = size"
"old_size, D['size'] = D['size'], size"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 7, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {