Multiple-dispatch, partial functions and pipeline operator for Python
Project description
coppertop - multiple-dispatch, partial functions and pipeline style for Python
Coppertop provides an alternative programming experience in Python via the following:
- multiple-dispatch
- partial functions
- piping syntax
- an embryonic core library of common functions
Installation
OUT-OF-ORDER
pip install coppertop-bones-dm for the dm core library and the @coppertop decorator.
pip install coppertop just for the @coppertop decorator.
At the moment needs to be git cloned and the setup.py manually run.
Multiple-dispatch
To use multiple-dispatch decorate functions with @coppertop and use different type annotations. Missing annotations are taken as fallback wildcards. Class inheritance is ignored when matching caller and function signatures.
from coppertop.pipe import *
@coppertop
def addOne(x:int) -> int:
return x + 1
@coppertop
def addOne(x:str) -> str:
return x + 'One'
@coppertop
def addOne(x): # fallback
if isinstance(x, list):
return x + [1]
else:
raise NotYetImplemented()
assert addOne(1) == 2
assert addOne('Three Two ') == 'Three Two One'
assert addOne([0]) == [0, 1]
Partial (application of) functions
syntax: f(_, a) -> f(_)
where _ is used as a sentinel place-holder for arguments yet to be confirmed (TBC)
We create partials of @coppertop decorated functions when we use _ indicating deferred arguments. For example:
from coppertop.pipe import *
@coppertop
def appendStr(x, y):
assert isinstance(x, str) and isinstance(y, str)
return x + y
appendWorld = appendStr(_, " world!") # first argument is deferred
assert appendWorld("hello") == "hello world!"
Piping syntax
The @coppertop function decorator also extends functions with the >> operator
and so allows code to be written in a more essay style format - i.e. left-to-right and
top-to-bottom. The idea is to make it easier to express program syntax (aka sequence).
unary style - takes 1 piped argument and 0+ called arguments
syntax: A >> f(args) -> f(args)(A)
from coppertop.pipe import *
@coppertop(style=unary)
def addOne(x):
return x + 1
1 >> addOne
"hello" >> appendStr(_," ") >> appendStr(_, "world!")
1 >> partial(lambda x: x +1)
binary style - takes 2 piped arguments and 0+ called arguments
syntax: A >> f(args) >> B -> f(args)(A, B)
from bones.core.errors import NotYetImplemented
from dm.core import collect, inject
@coppertop(style=binary)
def add(x, y):
return x + y
@coppertop(style=binary)
def op(x, action, y):
if action == "+":
return x + y
else:
raise NotYetImplemented()
1 >> add >> 1
1 >> op(_,"+",_) >> 1
[1,2] >> collect >> (lambda x: x + 1)
[1,2,3] >> inject(_,0,_) >> (lambda x,y: x + y)
ternary style - takes 3 piped arguments and 0+ called arguments
syntax: A >> f(args) >> B >> C -> f(args)(A, B, C)
from dm.core import both
from dm.testing immport check, equals
actual = [1,2] >> both >> (lambda x, y: x + y) >> [3,4]
assert (1 >> equal >> 1) == True
actual >> check >> equal >> [4, 6]
Examples
Bag of M&Ms problem
In Why coppertop - MM problem from Think Bayes.ipynb we implement a coppertop solution to the problem and silently introduce intersection types.
Cluedo notepad
See algos.py, where we track a game of Cluedo and infer who did it. See ex_games.py and cluedo-pad.ipynb for example game input and notepad output.
a whimsical exercise for the ambitious
(both, collect, inject, addOne, appendStr, check, equals are all illustrated above)
from dm.core import to
[1,2] >> both >> (lambda x, y: x + y) >> [3,4]
>> collect >> (lambda x: x * 2)
>> inject(_,1,_) >> (lambda x,y: x * y)
>> addOne >> addOne >> addOne
>> to >> str >> appendStr(_," red balloons go by")
>> check >> equal >> ???
Other
Thanks
Inspired by
- Magrittr - a piping library for R
- Arthur Whitney's ideas especially kdb/q and C style e.g. a tiny k interpreter for educational purposes
- Smalltalk - especially the ideas of Alan Kay & Rebecca Wirfs-Brock and the VisualWorks and Cuis implementations
- Alexander A. Stepanov's ideas on templating and generics
Built using
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file coppertop_bones-2025.5.28.tar.gz.
File metadata
- Download URL: coppertop_bones-2025.5.28.tar.gz
- Upload date:
- Size: 67.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e1c3f2a7cdb9a1bbc3dde69a6f632f86f4e10e75e3692034fe5b2425e4a9de67
|
|
| MD5 |
b0d2e6d67051bd5eaae740771c68bf25
|
|
| BLAKE2b-256 |
465c3b96bbfdee0c9974b9b7b36e7ba53fecfe4322392427332e96e31e00f697
|