From f7364a6161d85ac52151da542651ea9110d77e10 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Wed, 24 Nov 2021 08:54:08 -0800 Subject: [PATCH] Updating the docs. Playing with Joy Kernel. --- docs/3._Developing_a_Program.ipynb | 10 +- docs/Joy in Jupyter.ipynb | 151 +++- docs/Multiples of 3 and 5.ipynb | 1417 ++++++++++++++++++++++++++++++++++++ joy/library.py | 13 +- 4 files changed, 1574 insertions(+), 17 deletions(-) create mode 100644 docs/Multiples of 3 and 5.ipynb diff --git a/docs/3._Developing_a_Program.ipynb b/docs/3._Developing_a_Program.ipynb index 5de85c6..82dd516 100644 --- a/docs/3._Developing_a_Program.ipynb +++ b/docs/3._Developing_a_Program.ipynb @@ -314,9 +314,7 @@ { "cell_type": "markdown", "metadata": {}, - "source": [ - "We only want the terms *less than* 1000." - ] + "source": [] }, { "cell_type": "code", @@ -1092,9 +1090,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 2", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "python2" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -1106,7 +1104,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.3" + "version": "3.7.10" } }, "nbformat": 4, diff --git a/docs/Joy in Jupyter.ipynb b/docs/Joy in Jupyter.ipynb index 895bbcf..3958691 100644 --- a/docs/Joy in Jupyter.ipynb +++ b/docs/Joy in Jupyter.ipynb @@ -29,6 +29,42 @@ { "cell_type": "code", "execution_count": 2, + "id": "cbc09c4a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "41 123" + ] + } + ], + "source": [ + "123" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f310ec86", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5043" + ] + } + ], + "source": [ + "*" + ] + }, + { + "cell_type": "code", + "execution_count": 4, "id": "8d86c75f", "metadata": {}, "outputs": [ @@ -44,7 +80,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 5, "id": "ff9b5754", "metadata": {}, "outputs": [ @@ -62,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 6, "id": "e1027ca3", "metadata": {}, "outputs": [ @@ -78,7 +114,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 7, "id": "aef6f509", "metadata": {}, "outputs": [ @@ -111,7 +147,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 8, "id": "f491e33f", "metadata": {}, "outputs": [ @@ -129,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 9, "id": "31d6ec54", "metadata": {}, "outputs": [ @@ -149,6 +185,111 @@ "source": [ "trace" ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "f85a149a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "2e13763d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[dup cons]" + ] + } + ], + "source": [ + "[dup cons]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "e4509e6a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " [dup cons] • x\n", + " [dup cons] • dup cons\n", + "[dup cons] [dup cons] • cons\n", + "[[dup cons] dup cons] • \n", + "\n", + "[[dup cons] dup cons]" + ] + } + ], + "source": [ + "[x] trace" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "8170053c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[dup cons] dup cons] • i\n", + " • [dup cons] dup cons\n", + " [dup cons] • dup cons\n", + "[dup cons] [dup cons] • cons\n", + "[[dup cons] dup cons] • \n", + "\n", + "[[dup cons] dup cons]" + ] + } + ], + "source": [ + "[i] trace" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "50c24687", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "21c86a84", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { diff --git a/docs/Multiples of 3 and 5.ipynb b/docs/Multiples of 3 and 5.ipynb new file mode 100644 index 0000000..50a5f97 --- /dev/null +++ b/docs/Multiples of 3 and 5.ipynb @@ -0,0 +1,1417 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "754f6c90", + "metadata": {}, + "source": [ + "# [Project Euler, first problem](https://projecteuler.net/problem=1)\n", + "\n", + "## Multiples of 3 and 5\n", + "\n", + "> If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", + "\n", + "> Find the sum of all the multiples of 3 or 5 below 1000." + ] + }, + { + "cell_type": "markdown", + "id": "73e9f177", + "metadata": {}, + "source": [ + "Let's create a predicate that returns `True` if a number is a multiple of 3 or 5 and `False` otherwise. It is simple enough using the modulus operator." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "5e19337b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[false false true false false true false true]" + ] + } + ], + "source": [ + "[1 2 3 4 5 6 10 15] [3 % not] map" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "1fde6380", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "13b54b65", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[false false false false true false true true]" + ] + } + ], + "source": [ + "[1 2 3 4 5 6 10 15] [5 % not] map" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "8babb9ca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "markdown", + "id": "07d952db", + "metadata": {}, + "source": [ + "Now we can run them both using `fork` and then `or` the results:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "0d89ddfa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 false" + ] + } + ], + "source": [ + "23 [3 % not] [5 % not] fork or" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "d32ee42e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "f6d9f728", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[false false true false true true true true]" + ] + } + ], + "source": [ + "[1 2 3 4 5 6 10 15] [[3 % not] [5 % not] fork or] map" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c6a05e39", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "markdown", + "id": "109f6b8a", + "metadata": {}, + "source": [ + "Given the predicate function (and a `filter` function, to be defined later) a suitable program is:\n", + "\n", + " 1000 range [[3 % not] [5 % not] fork or] filter sum" + ] + }, + { + "cell_type": "markdown", + "id": "241fad42", + "metadata": {}, + "source": [ + "This function generates a list of the integers from 0 to 999, filters that list by the predicate, and then sums the result.\n", + "\n", + "Logically this is fine, but pragmatically we are doing more work than we should. We generate one thousand integers but actually use less than half of them. A better solution would be to generate just the multiples we want to sum, and to add them as we go rather than storing them and adding summing them at the end." + ] + }, + { + "cell_type": "markdown", + "id": "7b1ed895", + "metadata": {}, + "source": [ + "Consider the first few terms in the series:\n", + "\n", + " 3 5 6 9 10 12 15 18 20 21 ...\n", + "\n", + "Subtract each number from the one after it (subtracting 0 from 3):\n", + "\n", + " 3 5 6 9 10 12 15 18 20 21 24 25 27 30 ...\n", + " 0 3 5 6 9 10 12 15 18 20 21 24 25 27 ...\n", + " -------------------------------------------\n", + " 3 2 1 3 1 2 3 3 2 1 3 1 2 3 ...\n", + "\n", + "You get this lovely repeating palindromic sequence:\n", + "\n", + " 3 2 1 3 1 2 3\n", + "\n", + "To make a counter that increments by factors of 3 and 5 you just add\n", + "these differences to the counter one-by-one in a loop." + ] + }, + { + "cell_type": "markdown", + "id": "e0e27e85", + "metadata": {}, + "source": [ + "To make use of this sequence to increment a counter and sum terms as we go we need a function that will accept the sum, the counter, and the next term to add, and that adds the term to the counter and a copy of the counter to the running sum. This function will do that:\n", + "\n", + " + [+] dupdip" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "c53bfbd2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 0 0 3 • + [+] dupdip\n", + " 0 3 • [+] dupdip\n", + "0 3 [+] • dupdip\n", + " 0 3 • + 3\n", + " 3 • 3\n", + " 3 3 • \n", + "\n", + "3 3" + ] + } + ], + "source": [ + "0 0 3 [+ [+] dupdip] trace" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "a49d3728", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "20587ac5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60 15" + ] + } + ], + "source": [ + "0 0 [3 2 1 3 1 2 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "markdown", + "id": "f560b91e", + "metadata": {}, + "source": [ + "So one `step` through all seven terms brings the counter to 15 and the total to 60." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "f68bf653", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0" + ] + } + ], + "source": [ + "clear 0 0" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "b7780729", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60 15" + ] + } + ], + "source": [ + "[3 2 1 3 1 2 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "9316698e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "225 30" + ] + } + ], + "source": [ + "[3 2 1 3 1 2 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "07671594", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "495 45" + ] + } + ], + "source": [ + "[3 2 1 3 1 2 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "adee52a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "870 60" + ] + } + ], + "source": [ + "[3 2 1 3 1 2 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "markdown", + "id": "42302528", + "metadata": {}, + "source": [ + "Going through one sequence of the palindrome counts off 15 of the 1000. So how many \"flights\" in total do we need?" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "4e9d4907", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "bfa89997", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "66" + ] + } + ], + "source": [ + "1000 15 /" + ] + }, + { + "cell_type": "markdown", + "id": "a04fe808", + "metadata": {}, + "source": [ + "So sixty-six times and a few left over. How many?" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "86e454a7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "990" + ] + } + ], + "source": [ + "15 *" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "7e9b42b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " 990 • 1000 swap -\n", + "990 1000 • swap -\n", + "1000 990 • -\n", + " 10 • \n", + "\n", + "10" + ] + } + ], + "source": [ + "[1000 swap -] trace" + ] + }, + { + "cell_type": "markdown", + "id": "67ce48f8", + "metadata": {}, + "source": [ + "We only want the terms *less than* 1000." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5f3d0371", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9" + ] + } + ], + "source": [ + "clear 999 990 -" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "fc10c5c5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 9" + ] + } + ], + "source": [ + "[3 2 1 3] sum" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "d5fb8099", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "true" + ] + } + ], + "source": [ + "=" + ] + }, + { + "cell_type": "markdown", + "id": "0fa9a216", + "metadata": {}, + "source": [ + "That means we want to run the full list of numbers sixty-six times to get to 990 and then the first four numbers 3 2 1 3 to get to 999." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "52e22965", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0" + ] + } + ], + "source": [ + "clear 0 0" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "741e0688", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "229185 990" + ] + } + ], + "source": [ + "66 [[3 2 1 3 1 2 3] [+ [+] dupdip] step] times" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "40e1379d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168 999" + ] + } + ], + "source": [ + "[3 2 1 3] [+ [+] dupdip] step" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "e6be7907", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168" + ] + } + ], + "source": [ + "pop" + ] + }, + { + "cell_type": "markdown", + "id": "f361dae9", + "metadata": {}, + "source": [ + "Ta-da!" + ] + }, + { + "cell_type": "markdown", + "id": "9ef9d882", + "metadata": {}, + "source": [ + "## Wee Hack\n", + "\n", + "This form uses no extra storage and produces no unused summands. It's good but there's one more trick we can apply. The list of seven terms takes up at least seven bytes. But notice that all of the terms are less than four, and so each can fit in just two bits. We could store all seven terms in just fourteen bits and use masking and shifts to pick out each term as we go. This will use less space and save time loading whole integer terms from the list.\n", + "\n", + " 3 2 1 3 1 2 3\n", + " 0b 11 10 01 11 01 10 11 == 14811" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "4c358435", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14811" + ] + } + ], + "source": [ + "clear 14811" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "97615452", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 3702" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "3acbda7d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 925" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "dcffaa52", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 231" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "39cb9487", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 57" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "95924445", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 14" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "c5334a6e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "72f6bce6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 0" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "feeeac23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 0 0" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "fb419282", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 0 0 0" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "markdown", + "id": "0e2956c9", + "metadata": {}, + "source": [ + "We can run it in a `loop`..." + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "5e637fc3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14811" + ] + } + ], + "source": [ + "clear 14811" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "335e0432", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3" + ] + } + ], + "source": [ + "? [[3 &] [2 >>] cleave ?] loop pop" + ] + }, + { + "cell_type": "markdown", + "id": "4160f1aa", + "metadata": {}, + "source": [ + "?" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "6f3ed92b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0 14811" + ] + } + ], + "source": [ + "clear 0 0 14811" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "eb742eef", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0 3 3702" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "8b935622", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 3 3702" + ] + } + ], + "source": [ + "[+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "68904907", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 3 2 925" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "dd0ed2f5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8 5 925" + ] + } + ], + "source": [ + "[+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "d110d282", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14 6 231" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave [+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "a29055bb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 9 57" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave [+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "ad0897e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "33 10 14" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave [+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "5434a3c8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "45 12 3" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave [+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "55e94d15", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60 15 0" + ] + } + ], + "source": [ + "[3 &] [2 >>] cleave [+ [+] dupdip] dip" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d7daba3f", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "31f009b5", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0" + ] + } + ], + "source": [ + "clear 0 0" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "ffec3de6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60 15" + ] + } + ], + "source": [ + "14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "ac639fe0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "225 30" + ] + } + ], + "source": [ + "14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "885bf996", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "e72ab115", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0" + ] + } + ], + "source": [ + "clear 0 0" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "6c44ab00", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "229185 990" + ] + } + ], + "source": [ + "66 [14811 7 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop] times" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "7f4c4931", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168 999" + ] + } + ], + "source": [ + "14811 4 [[3 &] [2 >>] cleave [+ [+] dupdip] dip] times pop" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "25dc0fd8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168" + ] + } + ], + "source": [ + "pop" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "02228693", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "4bd5cd29", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + } + ], + "source": [ + "clear\n", + "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "537162b4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 [3702 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + } + ], + "source": [ + "x" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "051a539b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3" + ] + } + ], + "source": [ + "x x x x x x pop" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "id": "b37462f1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "466" + ] + } + ], + "source": [ + "clear\n", + "7 66 * 4 +" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "id": "d5fc7e8f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [] + } + ], + "source": [ + "clear" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4719ad5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "0c68ecd4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + } + ], + "source": [ + "clear\n", + "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "id": "c9d44724", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3" + ] + } + ], + "source": [ + "466 [x] times pop" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "f38a9ba9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "999" + ] + } + ], + "source": [ + "enstacken sum" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4d66d76f", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "4929fa6b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0 [0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + } + ], + "source": [ + "clear 0 0\n", + "[0 swap [dup [pop 14811] [] branch [3 &] [2 >>] cleave] dip rest cons]" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "c57bf8a2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233168" + ] + } + ], + "source": [ + "466 [x [+ [+] dupdip] dip] times popop" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61a83de1", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Joypy", + "language": "", + "name": "thun" + }, + "language_info": { + "file_extension": ".joy", + "mimetype": "text/plain", + "name": "Joy" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/joy/library.py b/joy/library.py index 5faf22f..04b2f3b 100644 --- a/joy/library.py +++ b/joy/library.py @@ -223,12 +223,13 @@ def BinaryBuiltinWrapper(f): (a, (b, stack)) = stack except ValueError: raise StackUnderflowError('Not enough values on stack.') - if ( not isinstance(a, int) - or not isinstance(b, int) - or isinstance(a, bool) # Because bools are ints in Python. - or isinstance(b, bool) - ): - raise NotAnIntError + # Boolean predicates like "or" fail here. :( +## if ( not isinstance(a, int) +## or not isinstance(b, int) +## or isinstance(a, bool) # Because bools are ints in Python. +## or isinstance(b, bool) +## ): +## raise NotAnIntError result = f(b, a) return (result, stack), expression, dictionary return inner -- 2.11.0