changeset 4425760ad7e8 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=4425760ad7e8 user: Simon Forman <sform****@hushm*****> date: Wed May 20 15:34:30 2020 -0700 description: Minor cleanup. changeset cb6f64ef5811 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=cb6f64ef5811 user: Simon Forman <sform****@hushm*****> date: Wed May 20 16:09:19 2020 -0700 description: Fix indentation in library docstrings. changeset d413fbede8a6 in joypy/Joypy details: http://hg.osdn.jp/view/joypy/Joypy?cmd=changeset;node=d413fbede8a6 user: Simon Forman <sform****@hushm*****> date: Wed May 20 19:15:47 2020 -0700 description: Update some of the docs. diffstat: docs/0._This_Implementation_of_Joy_in_Python.html | 307 ++- docs/0._This_Implementation_of_Joy_in_Python.ipynb | 243 +- docs/0._This_Implementation_of_Joy_in_Python.md | 204 +- docs/0._This_Implementation_of_Joy_in_Python.rst | 325 +- docs/1._Basic_Use_of_Joy_in_a_Notebook.html | 153 +- docs/1._Basic_Use_of_Joy_in_a_Notebook.ipynb | 139 +- docs/1._Basic_Use_of_Joy_in_a_Notebook.md | 131 +- docs/1._Basic_Use_of_Joy_in_a_Notebook.rst | 145 +- docs/2._Library_Examples.html | 858 +++--- docs/2._Library_Examples.ipynb | 280 +- docs/2._Library_Examples.md | 274 +- docs/2._Library_Examples.rst | 590 ++-- docs/3._Developing_a_Program.html | 781 +++--- docs/3._Developing_a_Program.ipynb | 561 ++-- docs/3._Developing_a_Program.md | 555 ++-- docs/3._Developing_a_Program.rst | 729 +++--- docs/Advent_of_Code_2017_December_1st.html | 40 +- docs/Advent_of_Code_2017_December_1st.ipynb | 14 +- docs/Advent_of_Code_2017_December_1st.md | 8 +- docs/Advent_of_Code_2017_December_1st.rst | 118 +- docs/Advent_of_Code_2017_December_2nd.html | 74 +- docs/Advent_of_Code_2017_December_2nd.ipynb | 60 +- docs/Advent_of_Code_2017_December_2nd.md | 48 +- docs/Advent_of_Code_2017_December_2nd.rst | 306 +- docs/Advent_of_Code_2017_December_3rd.html | 250 +- docs/Advent_of_Code_2017_December_3rd.ipynb | 184 +- docs/Advent_of_Code_2017_December_3rd.md | 168 +- docs/Advent_of_Code_2017_December_3rd.rst | 486 ++-- docs/Advent_of_Code_2017_December_4th.html | 16 +- docs/Advent_of_Code_2017_December_4th.ipynb | 14 +- docs/Advent_of_Code_2017_December_4th.md | 8 +- docs/Advent_of_Code_2017_December_4th.rst | 32 +- docs/Advent_of_Code_2017_December_5th.html | 71 +- docs/Advent_of_Code_2017_December_5th.ipynb | 53 +- docs/Advent_of_Code_2017_December_5th.md | 39 +- docs/Advent_of_Code_2017_December_5th.rst | 220 +- docs/Advent_of_Code_2017_December_6th.html | 52 +- docs/Advent_of_Code_2017_December_6th.ipynb | 28 +- docs/Advent_of_Code_2017_December_6th.md | 22 +- docs/Advent_of_Code_2017_December_6th.rst | 140 +- docs/Compiling_Joy.html | 1110 ++------- docs/Compiling_Joy.ipynb | 480 +--- docs/Compiling_Joy.md | 198 +- docs/Compiling_Joy.rst | 436 +-- docs/Newton-Raphson.html | 28 +- docs/Newton-Raphson.ipynb | 20 +- docs/Newton-Raphson.md | 14 +- docs/Newton-Raphson.rst | 154 +- docs/fun_with_scan.html | 49 +- docs/fun_with_scan.ipynb | 33 +- docs/fun_with_scan.md | 7 +- docs/fun_with_scan.rst | 209 +- docs/notebook_preamble.py | 6 +- docs/sphinx_docs/_build/html/_modules/joy/joy.html | 5 +- docs/sphinx_docs/_build/html/_modules/joy/library.html | 234 +- docs/sphinx_docs/_build/html/_modules/joy/utils/pretty_print.html | 25 +- docs/sphinx_docs/_build/html/library.html | 185 +- docs/sphinx_docs/_build/html/notebooks/Newton-Raphson.html | 32 +- docs/sphinx_docs/_build/html/pretty.html | 20 +- docs/sphinx_docs/_build/html/searchindex.js | 2 +- docs/sphinx_docs/notebooks/Newton-Raphson.rst | 154 +- joy/joy.py | 5 +- joy/library.py | 234 +- joy/utils/pretty_print.py | 25 +- 64 files changed, 5926 insertions(+), 6465 deletions(-) diffs (truncated from 23021 to 300 lines): diff -r e8ea90b5c2d1 -r d413fbede8a6 docs/0._This_Implementation_of_Joy_in_Python.html --- a/docs/0._This_Implementation_of_Joy_in_Python.html Tue May 19 19:39:44 2020 -0700 +++ b/docs/0._This_Implementation_of_Joy_in_Python.html Wed May 20 19:15:47 2020 -0700 @@ -13133,17 +13133,84 @@ <div class="prompt input_prompt">In [1]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="nn">joy.utils.stack</span> -<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="p">)</span> +<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="p">))</span> </pre></div> </div> </div> </div> +<div class="output_wrapper"> +<div class="output"> + + +<div class="output_area"> + + <div class="prompt"></div> + + +<div class="output_subarea output_stream output_stdout output_text"> +<pre>When talking about Joy we use the terms "stack", "quote", "sequence", +"list", and others to mean the same thing: a simple linear datatype that +permits certain operations such as iterating and pushing and popping +values from (at least) one end. + +There is no "Stack" Python class, instead we use the `cons list`_, a +venerable two-tuple recursive sequence datastructure, where the +empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the +recursive form of a stack with one or more items on it:: + + stack := () | (item, stack) + +Putting some numbers onto a stack:: + + () + (1, ()) + (2, (1, ())) + (3, (2, (1, ()))) + ... + +Python has very nice "tuple packing and unpacking" in its syntax which +means we can directly "unpack" the expected arguments to a Joy function. + +For example:: + + def dup((head, tail)): + return head, (head, tail) + +We replace the argument "stack" by the expected structure of the stack, +in this case "(head, tail)", and Python takes care of unpacking the +incoming tuple and assigning values to the names. (Note that Python +syntax doesn't require parentheses around tuples used in expressions +where they would be redundant.) + +Unfortunately, the Sphinx documentation generator, which is used to generate this +web page, doesn't handle tuples in the function parameters. And in Python 3, this +syntax was removed entirely. Instead you would have to write:: + + def dup(stack): + head, tail = stack + return head, (head, tail) + + +We have two very simple functions, one to build up a stack from a Python +iterable and another to iterate through a stack and yield its items +one-by-one in order. There are also two functions to generate string representations +of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are +printed left-to-right. These functions are written to support :doc:`../pretty`. + +.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists +</pre> +</div> +</div> + +</div> +</div> + </div> <div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt"> </div><div class="inner_cell"> @@ -13158,26 +13225,66 @@ <div class="prompt input_prompt">In [2]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">list_to_stack</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">list_to_stack</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span> </pre></div> </div> </div> </div> +<div class="output_wrapper"> +<div class="output"> + + +<div class="output_area"> + + <div class="prompt output_prompt">Out[2]:</div> + + + + +<div class="output_text output_subarea output_execute_result"> +<pre>(1, (2, (3, ())))</pre> +</div> + +</div> + +</div> +</div> + </div> <div class="cell border-box-sizing code_cell rendered"> <div class="input"> <div class="prompt input_prompt">In [3]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))))</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))))</span> </pre></div> </div> </div> </div> +<div class="output_wrapper"> +<div class="output"> + + +<div class="output_area"> + + <div class="prompt output_prompt">Out[3]:</div> + + + + +<div class="output_text output_subarea output_execute_result"> +<pre>[1, 2, 3]</pre> +</div> + +</div> + +</div> +</div> + </div> <div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt"> </div><div class="inner_cell"> @@ -13192,19 +13299,38 @@ <div class="prompt input_prompt">In [4]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="n">stack</span> <span class="o">=</span> <span class="p">()</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="n">stack</span> <span class="o">=</span> <span class="p">()</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]:</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">n</span><span class="p">,</span> <span class="n">stack</span> -<span class="nb">print</span> <span class="n">stack</span> -<span class="nb">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">))</span> +<span class="nb">print</span><span class="p">(</span><span class="n">stack</span><span class="p">)</span> +<span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">)))</span> </pre></div> </div> </div> </div> +<div class="output_wrapper"> +<div class="output"> + + +<div class="output_area"> + + <div class="prompt"></div> + + +<div class="output_subarea output_stream output_stdout output_text"> +<pre>(3, (2, (1, ()))) +[3, 2, 1] +</pre> +</div> +</div> + +</div> +</div> + </div> <div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt"> </div><div class="inner_cell"> @@ -13228,15 +13354,64 @@ <div class="prompt input_prompt">In [5]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</span> - -<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">)</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</span> + +<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">))</span> </pre></div> </div> </div> </div> +<div class="output_wrapper"> +<div class="output"> + + +<div class="output_area"> + + <div class="prompt"></div> + + +<div class="output_subarea output_stream output_stdout output_text"> +<pre>def joy(stack, expression, dictionary, viewer=None): + '''Evaluate a Joy expression on a stack. + + This function iterates through a sequence of terms which are either + literals (strings, numbers, sequences of terms) or function symbols. + Literals are put onto the stack and functions are looked up in the + disctionary and executed. + + The viewer is a function that is called with the stack and expression + on every iteration, its return value is ignored. + + :param stack stack: The stack. + :param stack expression: The expression to evaluate. + :param dict dictionary: A ``dict`` mapping names to Joy functions. + :param function viewer: Optional viewer function. + :rtype: (stack, (), dictionary) + + ''' + while expression: + + if viewer: viewer(stack, expression) + + term, expression = expression + if isinstance(term, Symbol): + term = dictionary[term] + stack, expression, dictionary = term(stack, expression, dictionary) + else: + stack = term, stack + + if viewer: viewer(stack, expression) + return stack, expression, dictionary + +</pre> +</div> +</div> + +</div> +</div> + </div> <div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt"> </div><div class="inner_cell"> @@ -13274,9 +13449,9 @@ <div class="prompt input_prompt">In [6]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span> - -<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="p">)</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span> + +<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="p">))</span> </pre></div> </div> @@ -13330,7 +13505,7 @@ <div class="prompt input_prompt">In [7]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">_parse</span><span class="p">)</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">_parse</span><span class="p">))</span> </pre></div> </div> @@ -13391,7 +13566,7 @@ <div class="prompt input_prompt">In [8]:</div> <div class="inner_cell"> <div class="input_area"> -<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">'1 2 3 4 5'</span><span class="p">)</span> <span class="c1"># A simple sequence.</span> +<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">'1 2 3 4 5'</span><span class="p">)</span> <span class="c1"># A simple sequence.</span> </pre></div> </div>