[Python-ideas] Dollar operator suggestion

Daniel Moisset dmoisset at machinalis.com
Thu Oct 26 08:53:06 EDT 2017


Expanding on the comments of the OP (to give more information, not
necessarily to support or be against it):

The "$" operator in Haskell is not a composition operator, it's essentially
the same as python's apply builtin (the python2 builtin, removed for python
3), but with an operator syntax; the main trick behind it is that it's
right associative, so you get that:

len $ set $ str $ foo => len $ (set $ (str $ foo)) -> len(set(str(foo)))

It looks a bit funky, and it works only reasonably with single-argument
functions (which in haskell doesn't matter given that it uses currying that
makes all function one argument functions with partial application). The
best way to think about it for non Haskellers is that it's exactly like the
"|" operators un UNIX-like shells, but with the reverse order; in UNIX, run
foo, filter the output through str, then set, then len would read like:

foo | str | set | len, which is the same as above but right to left. This
is to clarify that this si NOT about function composition, just an
alternate application syntax

The second part of the example in the post, where composition is discussed
actually relies in a completely orthogonal feature of Haskell  that allows
to define partial operator applications as functions, for example you can
define:
half = (/ 2) -- same as lambda x: x/2, so half 4 => 2
next = (+ 1) -- same as lambda x: x + 1, so next 7 => 8
invert = (1 /) -- same as lambda x: 1 / x, so invert 4 => 0.25

So this implies a new way of writing anonymous functions besides lambdas.
To make the second part of the proposal work, both features should be
present

Now going into the discussion itself, the second feature is much more
invasive on the syntax+implementation (and also backwards comptibility,
given that stuff like "(+ 1)" already mean something different in python).
The first feature by itself shouldn't break stuff, and even if different to
what we're used to is not very unidiomatic (it leads to cleaner code,
although its meaning is definitely not very discoverable).

To get a rough idea on how that could work, take a look at
https://gist.github.com/dmoisset/bd43b8c0ce26c9cff0ad297b7e1ba5f9 ; I just
used python ** operator because that's the only right associative one.
Something similar provided in the default function type (and at a low
level) could work. I'd probably would like to see some code samples where
this is applied to check that it's worth the trouble.

D.


On 26 October 2017 at 12:06, Yan Pas <yanp.bugz at gmail.com> wrote:

> I've looked up this feature in haskell. Dollar sign operator is used to
> avoid parentheses.
>
> Rationalle:
> Python tends to use functions instead of methods ( e.g. len([1,2,3])
> instead of [1,2,3].len() ). Sometimes the expression inside parentheses
> may become big  and using a lot of parentheses may tend to bad readability.
> I suggest the following syntax:
>
> len $ [1,2,3]
>
> Functions map be also  chained:
>
> len $ list $ map(...)
>
> This operator may be used for function composition too:
>
> foo = len $ set $
> in the same as
> foo = lambda *as,**kas : len(set(*as, **kas))
> in current syntax
>
> Regards,
> Yan
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 
Daniel F. Moisset - UK Country Manager - Machinalis Limited
www.machinalis.co.uk <http://www.machinalis.com>
Skype: @dmoisset T: + 44 7398 827139

1 Fore St, London, EC2Y 9DT

Machinalis Limited is a company registered in England and Wales. Registered
number: 10574987.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171026/d6dd0dc2/attachment-0001.html>


More information about the Python-ideas mailing list