Introduce pipe operators and function synthesis to Python (provisional)

Premise

class F:
    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kwargs):
        return self.f(*args, **kwargs)

    def __mul__(self, other):
        def _f(*args, **kwargs):
            return self.f(other(*args, **kwargs))
        return _f

    def __ror__(self, other):
        return self.f(other)


@F
def f(x):
    return x + 1

@F
def g(x):
    return x * x

Consequential

>>> 1 | f
2

>>> 2 | g
4

>>> 1 | f | g
4

>>> (f * g)(1)
2

>>> (g * f)(1)
4

Recommended Posts

Introduce pipe operators and function synthesis to Python (provisional)
Function synthesis and application in Python
I tried function synthesis and curry with python
[Python] How to use hash function and tuple.
Python 3.6 on Windows ... and to Xamarin.
[Introduction to Udemy Python3 + Application] 49. Function citation and return value declaration
Python logging and dump to json
Selenium and python to open google
How to use python zip function
[Python] Difference between function and method
[Python] Function arguments * (star) and ** (double star)
[Python] How to use the enumerate function (extract the index number and element)
How to package and distribute Python scripts
From Python to using MeCab (and CaboCha)
[Road to intermediate Python] Use ternary operators
How to install and use pandas_datareader [Python]
[Introduction to Udemy Python3 + Application] 48. Function definition
[python] How to use __command__, function explanation
Fractal to make and play with Python
[Introduction to Udemy Python3 + Application] 45. enumerate function
[Introduction to Udemy Python3 + Application] 41. Input function
Function to save images by date [python3]
Porting and modifying doublet-solver from python2 to python3.
[Introduction to Udemy Python3 + Application] 44. range function
[Introduction to Udemy Python3 + Application] 46. Zip function
Read Python csv and export to txt
Lambda function to take AMI backup (python)
python: How to use locals () and globals ()
zipline Function to buy and sell stocks
Introduce Python library TRML2PDF to MacOSX (10.11.xx)
[Python] How to calculate MAE and RMSE
How to use Python zip and enumerate
Compress python data and write to sqlite
How to use is and == in Python
[Python] Introduce UIKit3 to your Django project
Introduce postfix to WSL to relay mail addressed to Office365 and process it with Python
python function ①
[Python] function
I want to get the file name, line number, and function name in Python 3.4
python function ②
Introduction to Thano Function definition and automatic differentiation
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Automatically register function arguments to argparse in Python
How to generate permutations in Python and C ++
To execute a Python enumerate function in JavaScript
[Introduction to Python3 Day 12] Chapter 6 Objects and Classes (6.3-6.15)
Image characters and post to slack (python slackbot)
[Python] How to read data from CIFAR-10 and CIFAR-100
I compared "python dictionary type" and "excel function"
[Introduction to Python3 Day 22] Chapter 11 Concurrency and Networking (11.1 to 11.3)
Send messages to Skype and Chatwork in Python
How to convert SVG to PDF and PNG [Python]
[Introduction to Udemy Python3 + Application] 64. Namespace and Scope
[Introduction to Python3 Day 11] Chapter 6 Objects and Classes (6.1-6.2)
Output python log to both console and file
Introduction to Deep Learning ~ Localization and Loss Function ~
Try using tensorflow ① Build python environment and introduce tensorflow
List of Python code to move and remember
To represent date, time, time, and seconds in Python
How to plot autocorrelation and partial autocorrelation in python