Coder Social home page Coder Social logo

pipefun's Introduction

Pipefun

A simple, lightweighted, zero dependency python piping package.

Install

pip install pipefun

Usage

from pipefun import Pipable, out
from pipefun.Functionals import square

add_to = lambda a: lambda b: a + b # a curried **add** function

output = ~(x >> add_to(3) >> square)

print(output) # 36

The >> operator pushes a Pipable into a function and return a new Pipable with the return value of the function. The ~ operator pulls the value in a Pipable

Note that Pipable is immutable, so the returned Pipable doesn't equal the input and is a new one

x_out = x >> add_to(3) >> square

print(x_out == x) # False
print(~x_out == ~x) # False

Pipe merging

In daily use case, it's very possible that functions take more than 1 arg. To handle this, Pipable can store more than 1 values and plug them into a function when needed.

We use the | operator to merge Pipables. In Python, it has lower priority than >>.

x = Pipable(3)
y = Pipable(5)

out = x >> square # Pipable(9)

# merge two Pipables together
out = out | y # Pipable(9, 5)

# plug the pipes into a two args function
out = out >> add # Pipable(14)

print(~out) # 14

Alternatively, in one line.

out = ( x >> square | y) >> add # Pipable(14)
print(~out) # 14

Let's discard the ~ operator

There is a special function in Pipable package that do nothing. If a Pipable is piped into it, same thing will happen with what the ~ operator do.

from pipefun import Pipable, out

x = Pipable(2)

y = x >> square >> out
print(y) # 4

pipefun's People

Stargazers

Yap avatar Caroline Cui avatar  avatar  avatar  avatar Kurorin avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.