[Python-ideas] add fluent operator to everything

Kyle Lahnakoski klahnakoski at mozilla.com
Thu Feb 21 20:20:05 EST 2019


On 2019-02-20 10:10, Steven D'Aprano wrote:
> Or if you're worried about the line length:
>     result = function(mystr.strip()
>                            .expandtabs()
>                            .lower()
>                            .replace('ham', 'spam')
>       I think         


It seems that this fluency discussion, and the vector discussion is
similar; I made a toy class [1] to demonstrate. It is much like
DavidMertz's vector [2], but focused on chained methods .

The `vector()` method lets us enter "vector mode". There are methods
that act on elements (eg. map), methods that act on the whole (eg.
sort), and methods that exit vector mode (eg. list).

output = vector([3, 2, 1]).append(4).sort().limit(10).list()

Fluency  can be had by entering vector mode on a singleton list:

output = (
    vector([mystr])
    .strip()
    .expandtabs()
    .lower()
    .replace("ham", "spam")
    .map(function)
    .first()
)

Given vector() is quite succinct, and Numpy and Pandas do vector
operations elegantly already, I do not think there is need for
vectorized operators or fluency operators in Python.

[1] My toy class -
https://github.com/klahnakoski/mo-vector/blob/master/mo_vector/__init__.py

[2] DavidMertz vector 0-
https://github.com/DavidMertz/stringpy/blob/master/vector.py




More information about the Python-ideas mailing list