Friday, February 28, 2020

Implicit multiplication in Python - part 1

Quoting from a post from Guido van Rossum

    ... The power of visual processing really becomes apparent when you combine
    multiple operators. For example, consider the distributive law
       
mul(n, add(x, y)) == add(mul(n, x), mul(n, y))  (5)
    That was painful to write, and I believe that at first you won't see the
    pattern (or at least you wouldn't have immediately seen it if I hadn't
    mentioned this was the distributive law).
    Compare to
        n * (x + y) == n * x + n * y    (5a)
    Notice how this also uses relative operator priorities. Often
    mathematicians write this even more compact
        n(x+y) == nx + ny    (5b)
    but alas, that currently goes beyond the capacities of Python's parser.
    ...
    Now, programming isn't exactly the same activity as math, but we all know
    that Readability Counts, and this is where operator overloading in Python
    comes in.  ...
What if we could do something half-way between what Python currently allow
and what mathematicians would write by transforming something that is currently a SyntaxError into valid Python code?


    >>> from ideas.examples import implicit_multiplication as mul
    >>> hook = mul.add_hook()
    >>> from ideas import console
    >>> console.start()
    Configuration values for the console:
        callback_params: {'show_original': False, 
                          'show_transformed': False}
        transform_source from ideas.examples.implicit_multiplication
    --------------------------------------------------
    Ideas Console version 0.0.7a. [Python version: 3.7.3]

    ~>> 2(3 + 4)
    14
    ~>> a = 3
    ~>> b = 4
    ~>> 2a
    6
    ~>> a b
    12

All that is needed is to change the way the code is tokenized before the code is parsed by Python.

Monday, February 24, 2020

From a rejected Pycon talk to a new project.

Like many others, my talk proposal (early draft here) for Pycon US was rejected. So, I decided to spend some time putting everything in a new project instead. (Documentation here.)  It is still a rough draft, but usable ... and since I've mentioned it in a few other places, I thought I should mention it here as well.