Try a functional programming pipe in Python

Pipe processing

Consider pipe processing in a functional language in Python.

The ideal is result = [3,1,2] |> sorted |> reversed ## [3,2,1]

Implementation

--Wrap with values and functions in your own class. --"|>" is not in the operator. → Substitute with ">>".

Original class PIPE

PIPE.py


# >>Pipe processing with
class PIPE:
    def __init__(self, val):
        self.val = val

    def __rshift__(self, other):
        if callable(other.val):
            return PIPE(other.val(self.val))
        return other.val
        
    def __str__(self):
        return str(self.val)

Usage

result = PIPE([3,1,2]) >> PIPE(sorted)
print(result) # [1, 2, 3]

sample1

#You can use both lambda and function.
#print too.
_ = PIPE([3,1,2]) \
    >> PIPE(sorted) \
    >> PIPE(reversed) \
    >> PIPE(lambda lst: [l+1 for l in lst]) \
    >> PIPE(list) \
    >> PIPE(print) # [4, 3, 2]

sample2

##When you want to use format with print.
_ = PIPE({"K0":1, "K1":2}) \
    >> PIPE(lambda d: sum([v for k,v in d.items()])) \
    >> PIPE(lambda s: "sum is {}".format(s)) \
    >> PIPE(print) # sum is 3

sample3

_ = PIPE({"K0":1, "K1":2}) \
    >> PIPE(lambda d: {k:v+1 for k,v in d.items()}) \
    >> PIPE(lambda d: {k.replace("K", "KEY"): v for k,v in d.items()}) \
    >> PIPE(print) # {'KEY0': 2, 'KEY1': 3}

Summary

--Pipe processing was possible. --Easy to read than print (list (reversed (sorted ([3,1,2])))). --It's easy to forget the "\" at the end of the line. ――I feel that it is surprisingly versatile.

Recommended Posts

Try a functional programming pipe in Python
Functional programming in Python Project Euler 1
Functional programming in Python Project Euler 3
Functional programming in Python Project Euler 2
Programming in python
Try sending a SYN packet in Python
Try drawing a simple animation in Python
Learn dynamic programming in Python (A ~ E)
Try gRPC in Python
Python programming in Excel
Try 9 slices in Python
What is "functional programming" and "object-oriented" in Python?
Take a screenshot in Python
Try to make a Python module in C language
Create a function in Python
Try programming with a shell!
Try searching for a million character profile in Python
Try embedding Python in a C ++ program with pybind11
Try LINE Notify in Python
Redis pipe lining in Python
Try implementing Yubaba in Python 3
Draw a heart in Python
Get in touch with functional programming in JavaScript or Python 3
[Python] Dynamic programming TDPC A
Try creating a Deep Zoom file format .DZI in Python
A Python program in "A book that gently teaches difficult programming"
Try building a neural network in Python without using a library
Try running a function written in Python using Fn Project
Just try to receive a webhook in ngrok and python
Maybe in a python (original title: Maybe in Python)
Write a binary search in Python
GUI programming in Python using Appjar
[python] Manage functions in a list
Hit a command in Python (Windows)
Try throwing a query in Redash
Create a DI Container in Python
Draw a scatterplot matrix in python
Try implementing extension method in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Try using LevelDB in Python (plyvel)
Let's try Fizz Buzz in Python
Try to calculate Trace in Python
Try PLC register access in Python
Solve ABC036 A ~ C in Python
Start SQLite in a programming language
Write a pie chart in Python
Write a vim plugin in Python
Write a depth-first search in Python
Implementing a simple algorithm in Python 2
Create a Kubernetes Operator in Python
Solve ABC037 A ~ C in Python
Run a simple algorithm in Python
Draw a CNN diagram in Python
Create a random string in Python
Try using Leap Motion in Python
Schedule a Zoom meeting in Python
When writing a program in Python
Try to get a list of breaking news threads in Python.
Try python
Programming with Python / JavaScript / VBScript inline scripting in Automation Anywhere A 2019