evhub / coconut

Simple, elegant, Pythonic functional programming.
http://coconut-lang.org
Apache License 2.0
4.04k stars 120 forks source link

Improve implicit partial application #841

Closed shilkazx closed 2 months ago

shilkazx commented 2 months ago

Allow multiple reference of implicit partial. What we can do is:

>>> [0, 1] |> .[1]
1

But we can't do this:

>>> [0, 1] |> .[1] + .[0]

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'operator.itemgetter' and 'operator.itemgetter'

A nagging crossover could work, but why not improve the implicit partial:

>>> [0, 1] |> =>_[1] + _[0]
1
evhub commented 2 months ago

Implicit partials are intended to be for simple, straightforward operations that can't easily be built out of other operations, not to replace lambdas completely. That being said, Coconut does actually have an easy way to write this without using a lambda (this style is known as point-free programming) using the lift built-in:

>>> [0, 1] |> lift(+)(.[1], .[0])
1

or:

>>> [0, 1] |> .[1] `lift(+)` .[0]
1
shilkazx commented 2 months ago

Oh, it does implement the feature but using 'lift' under such circumstances is weirder and more cumbersome than implicit lambda. Anyway, thanks for your good advice and I really love coconut.