Skip to main content

Add composition and applying operators to python.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_composition-0.1.0.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

python_composition-0.1.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file python_composition-0.1.0.tar.gz.

File metadata

  • Download URL: python_composition-0.1.0.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.10.0rc2 Windows/10

File hashes

Hashes for python_composition-0.1.0.tar.gz
Algorithm Hash digest
SHA256 182a0437d0fa2a93673f417e2aa151cd979701c98f97734823821ab8c751386c
MD5 52d822b25942810c1cb0ea799a6f319a
BLAKE2b-256 668aef39ec69912bcb58ec28df0dcb8360894c99849f283517a081fe124d0f91

See more details on using hashes here.

File details

Details for the file python_composition-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for python_composition-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 728e26e408c34dcc1abc02c39646dfdcb9b1bcb9399e68ecdc5ed85597d69eba
MD5 9305b79a838b137cd0b07de343d4c8fc
BLAKE2b-256 37fec0250d56b90e5189f853237825b7e6a2050a6a2f0d864676e79326a7793e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page