Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A tiny functional composition utility for Python. It allows you to create pipelines of functions using the | operator. Similar to |> (pipe forward) operator in F#.

Notifications You must be signed in to change notification settings

phantie/pipe-forward

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pipe-forward

A tiny functional composition utility for Python.

It allows you to create pipelines of functions using the | operator.

Similar to |> (pipe forward) operator in F#.


Usage

Basic example

from pipe_forward import P  # alias for StackPipe


assert "123" @ (P(int) | float) == 123.0 # transform "123" to int, then to float
                                         # quickly interpreted as written - direct flow

# inverted flow (thumbs down)
assert float(int("123"))

Call with standard semantics

from pipe_forward import P
fn = P(int) | float
assert (P(int) | float)("123") == fn("123") == 123.0

Call with @

from pipe_forward import P
fn = P(int) | float
assert "123" @ fn == fn @ "123" == 123.0 # use prefix or suffix notation

Composability

from pipe_forward import P

# linter correctly recognizes it as P[object, int]
to_int = P(int) # convert to int

# linter correctly recognizes it as P[object, str]
to_str = to_int | str # convert to int, and then to str

assert to_int(42.0) == 42
assert to_str(42.0) == "42"

StackPipe vs LoopPipe behavior

StackPipe and LoopPipe are provided, with P as a short alias for StackPipe. They inhibit equal behavior, but implemented differently.

from pipe_forward import StackPipe
from pipe_forward import LoopPipe

stack = StackPipe(float) | str
loop = LoopPipe(float) | str

assert stack(10) == loop(10) == "10.0"

Why use this?

  • Succinct function composition with | operator
  • Type hint support
  • Immutable
  • Readable one-liners
  • Explicit

Installation

pip install git+https://github.com/phantie/pipe-forward.git -U

About

A tiny functional composition utility for Python. It allows you to create pipelines of functions using the | operator. Similar to |> (pipe forward) operator in F#.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages