Skip to content

MathanGeurtsen/simple-pipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

simple-pipe

Checked with pyright

A Python package that provides a simple typed pipe operator for function composition.

This is a weekend hobby project. If you wish to use a pipe operator in your code, take a look at the alternatives, or simply check out the extremely short implementation in pipe.py.

You can install this package with pip using the following command:

pip install git+https://github.com/MathanGeurtsen/simple-pipe.git

Usage

simple-pipe allows for chaining together typed functions using a pipe operator (|). Pipes can be type checked by pyright.

For example, reverse a list and get the length of the last item:

>>> from simple_pipe import Pipe

>>> # Chain multiple functions together
>>> reversed_first = Pipe | list | reversed | iter | next | len

>>> # call the function with input
>>> result = reversed_first(("a", "bb", "ccc"))
>>> assert result == 3

Calculate the sum of the squares of the first 10 integers:

>>> from itertools import islice, count
>>> from simple_pipe import Pipe

>>> first_ten_quad = Pipe | count | (lambda nrs: map(lambda x:x**int(2), nrs)) | (lambda nrs: islice(nrs, 10)) | sum

>>> result = first_ten_quad(0)
>>> assert result == 285

All of these calls are fully compatible with type checking, but will also function without specific type annotations for example as in the use of the lambdas above.

Requirements

  • Python 3.12+

Comparison to alternatives

How does this compare to Pipe? First off, simple-pipe is just a weekend hobby project whereas Pipe has had a couple of years of development, so in terms of code maturity, there's no comparison.

The intended use of simple-pipe's Pipe is to be a Forward Pipe Operator and nothing more, making no assumptions as to what kind of data passes through.

Pipe's class Pipe.Pipe is specific to generators, so the two work slightly differently in use.

However, since Pipe.Pipe exposes its internal function through .function, we are actually compatible and can use those same functions in simple-pipe pipes:

from itertools import count
from pipe import select, take
from simple_pipe import Pipe

function_simple_pipe = Pipe | count | select(lambda x: x **2).function | take(10).function | sum
res_simple_pipe = function_simple_pipe(0)
assert res_pipe_pipe == res_simple_pipe == 285

Note that for function_simple_pipe the definition is completely defined in forward composition: the functions are applied in the order in which you read them. It's also reusable since the pipe defines a function instead of executing directly.

Development

For testing and linting, setup the extra dependencies:

uv venv
uv sync --extra dev

You can check if tests and building work with poe build.

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A Python package that provides a simple typed pipe operator for function composition.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages