On Tue, 30 Nov 2021 at 04:55, Chris Angelico <ros...@gmail.com> wrote:
>
> if this:
>
> 1 |> add(2)
>
> is exactly equivalent to this:
>
> add(1, 2)
>
> then neither the iterator nor the consumer needs to be aware of the
> new protocol.
>

Along with such an operator form of functools.partial:

```
x |> f   # equivalent to partial(f, x) as a callable object
```

I would also like

x *|> f  or something for partial(f, *x) and
x **|> f  or something for partial(f, **x) .

Assuming that the interpretation of the operator is going to be
controlled by a new dunder method (logically to be provided by the
built-in class 'object' for general objects, I understand), another,
close possibility for enabling application of functions from the right
(not conflicting with the other) may be (just) a method (again
provided by the class 'object') through which any function can be
applied on the object, say

```
x.apply(function, *args, **kwargs)
#
# ---> to be equivalent to function(*args, x, **kwargs)
# or functools.partial(function, *args, **kwargs)(x)
```

(so partial(operator.methodcaller, "apply") would be a restricted
version of partial).

Instead of the class 'object', a specialized class can provide such a
method, and can be created by a user of course.  However, since not
every function returns an instance of that class, a bit more
complication will then be involved in chaining applications of
arbitrary functions through the method, as well as the applications of
other methods.  Thus, I think it would be the simplest if the
universal base class provided such a method.

Finally, we can also imagine "unpacking" versions of the above:

```
x.apply1(function, *args, **kwargs)
# ---> function(*args, *x, **kwargs)

x.apply2(function, *args, **kwargs)
# ---> function(*args, **x, **kwargs)
```

(as well as perhaps something more flexible..).

Best regards,
Takuo Matsuoka
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3NUQ6EFDGK2ISWOFMO7A4MG5VDIWZFQO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to