I wanted to do something like an Elixir pipe in Python

Example of pipe processing

Recently (in my mind) the topic of Elixir. It's a modern functional language that isn't as stiff as Haskell. Elixir has many charms, but among them, the pipe processing of functions is within me. I like it quite a lot.

Such a guy

[1, [2], 3, [4, 5] |> List.flatten |> Enum.map(&(&1 * 2))

You can pass the return value to the function on the right by using the operator |>.

I'm using Python and I wish I could create multiple functions as above.

What was made

I made it like this.

def hoge(a, b, c, d) :
  return a+b+c+d

pipe([1,2], 
  [
    pmap(lambda x: x*2),
    list,
    to(hoge, [3, 4]),
    print
  ]
)

pipe / 2, to / 2, pmap / 1 are the ones I made. Each role is pipe The first argument is the initial value for pipe processing, and the second argument is the list of functions. Implementation is

from functools import reduce
def pipe(elm, fn_list) :
    return reduce(lambda x, f: f(x), fn_list, elm)

to A higher-order function that transforms a self-made function into a form suitable for a pipe function. Implementation is

def to(fn, outer_args=None) :
    def _to(inner_args) :
        args = outer_args+inner_args if not outer_args == None else inner_args
        return fn(*args)
    return _to

pmap Wrapped existing map function for pipe function In addition to this, reduce and filter are also made, but for the time being

Implementation is

def pmap(fn) :
    def _pmap(li) :
        return map(fn, li)
    return _pmap

Impressions

I am satisfied because I was able to do what I wanted to do just by doing something fairly simple. However, the writing style is very different from the standard writing style, so I thought that I could only use it with my own code.

Recommended Posts

I wanted to do something like an Elixir pipe in Python
I want to do something in Python when I finish
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I wanted to solve ABC159 in Python
Do something like Redis transactions in Python
I wanted to do it like running an AtCoder test case.
[Machine learning] I tried to do something like passing an image
I want to do Dunnett's test in Python
I want to manipulate strings in Kotlin like Python!
Do something like a Python interpreter in Visual Studio Code
Something like JS setTimeout in python
An alternative to `pause` in Python
Something like tail -f in Python
I saw this commit and thought I wanted something like rake version: bump in Python too
When I got a list of study sessions in Python, I found something I wanted to make
A story that I wanted to do a function like before_action used in rails with django [Beginner learns python with a reference book]
I tried to implement PLSA in Python
I tried to implement permutation in Python
How to do R chartr () in Python
I tried to implement PLSA in Python 2
I wanted to solve ABC160 with Python
I tried to implement ADALINE in Python
I tried to implement PPO in Python
Try something like Python for-else in Ruby
I wanted to solve ABC172 with Python
Implemented DQN in TensorFlow (I wanted to ...)
#I tried something like Vlookup with Python # 2
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
It's more recent, but I wanted to do BMI calculation with python.
After all, what should I use to do type comparisons in Python?
I want to do a monkey patch only partially safely in Python
I wanted to solve NOMURA Contest 2020 with Python
[Python] What I did to do Unit Test
I was able to recurse in Python: lambda
Minimal implementation to do Union Find in Python
I wanted to install Python 3.4.3 with Homebrew + pyenv
I wrote "Introduction to Effect Verification" in Python
I want to merge nested dicts in Python
I tried to implement TOPIC MODEL in Python
What to do to get google spreadsheet in python
I tried to implement selection sort in python
I want to display the progress in Python!
[Python] What to do if an error occurs in pip (pyinstaller, pyautogui, etc.)
I want to write in Python! (1) Code format check
What I do when imitating embedded go in python
I tried to graph the packages installed in Python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I get an Import Error in Python Beautiful Soup
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I tried to implement a pseudo pachislot in Python
What to do if the print command itself causes an error in Maya python
I wanted to use the Python library from MATLAB
I want to randomly sample a file in Python
I tried to implement Dragon Quest poker in Python
What to do if you get an error when importing matplotlib in Python (Mac)
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to implement an artificial perceptron with python
I want to work with a robot in python.
I tried to implement GA (genetic algorithm) in Python