Skip to main content

Composition

Description

The project is a Python library that allows for composition with currying. It provides a set of operators and functions that enable functional programming techniques such as partial function application and composition. With this library, you can easily compose functions, apply them to data, and manipulate collections in a functional style.

Features

  • Allows for composition with currying
  • Supports partial function application
  • Supports function assignment
  • Provides various operators for composition and application

Usage

from composition import C


C / list / map % (lambda x: x*2) @ range(1,5)
# Output: [2, 4, 6, 8]
# Instead of list(map(lambda x:x*2, range(1,5))) 

So, / is composition , % is partial and @ is applying.

(f / g)(x) is f(g(x))

f% (lambda x:x*2) sets the first variable to x*2. But it can also get a dictionary.

C / list / zip & C / list @ range(5) ^ [4,8,9,10,11]
# Output: [(0, 4), (1, 8), (2, 9), (3, 10), (4, 11)]

& is partial but in lower precedence . ^ is applying with lower precedence

C / set / reduce % (lambda x,y:x+y) @ (C /  self._hist_by_date.values() << (lambda s: list(s.keys())) )
#The same as set(reduce(lambda x,y:x+y, [list(s.keys()) for s in self._hist_by_date.values()]) )

Partial supports function assigment, which means you can do:

 def f(a,b,c):
    return (a,b,c)
 f % {'b': "->b*a*2"} @ (1,3)
 or f (lambda b: b*2}

It also supports currying.

The term is used freely here. It means that the original arguments can be passed to function along the chain, if it serves the purpose.

def load_user(user_id: int, db: sqlite3.Connection) -> User:
    ...


def find_computer(computer : str, db : sqlite3.Connection):
    ...

consider the above chain. db is common parameters to both.

Now you can call specify arugment to take from ret of previous function (X,Y,Z by the order) , or just use Orig to take the same parameter from original call.

C / find_computer % A(db=Orig,computer=X) / load_user

Installation

To install the project, simply run the following command:

pip install composition 

It has only one dependency (multimethod).

Notice

The implementation can be unsafe because it converts string to functions. So on undistilled input, please use CS instead of C. (otherwise an attacker might be able to inject a function in a string starting with ->).

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

Metadata

Release files for python-composition 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-composition 0.1.0
File Size Uploaded
python_composition-0.1.0.tar.gz 18.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-composition 0.1.0
File Interpreter ABI Platform
python_composition-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.8 kB

Release files / python_composition-0.1.0.tar.gz

Download URL python_composition-0.1.0.tar.gz
Size 18.1 kB
Tags Source
SHA-256 checksum
How to use checksums
182a0437d0fa2a93673f417e2aa151cd979701c98f97734823821ab8c751386c
BLAKE2b-256 checksum
How to use checksums
668aef39ec69912bcb58ec28df0dcb8360894c99849f283517a081fe124d0f91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.0rc2 Windows/10

Release files / python_composition-0.1.0-py3-none-any.whl

Download URL python_composition-0.1.0-py3-none-any.whl
Size 18.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
728e26e408c34dcc1abc02c39646dfdcb9b1bcb9399e68ecdc5ed85597d69eba
BLAKE2b-256 checksum
How to use checksums
37fec0250d56b90e5189f853237825b7e6a2050a6a2f0d864676e79326a7793e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.10.0rc2 Windows/10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page