Operator overloading for fast anonymous functions.
Project description
A placeholder
uses operator overloading to create partially bound functions on-the-fly. When used in a binary expression, it will return a callable object with the other argument bound. It's useful for replacing lambda
in functional programming, and resembles Scala's placeholders.
Usage
from placeholder import _ # single underscore
_.age < 18 # lambda obj: obj.age < 18
_[key] ** 2 # lambda obj: obj[key] ** 2
Note _
has special meaning in other contexts, such as the previous output in interactive shells. Assign to a different name as needed. Kotlin uses it
, but in Python it
is a common short name for an iterator.
_
is a singleton of an F
class, and F
expressions can also be used with functions.
from placeholder import F
-F(len) # lambda obj: -len(obj)
All applicable double underscore methods are supported.
Performance
Every effort is made to optimize the placeholder instance. It's 20-40x faster than similar libraries on PyPI.
Placeholders are also iterable, allowing direct access to the underlying functions.
(func,) = _.age # operator.attrgetter('age')
Performance should generally be comparable to inlined expressions, and faster than lambda. Below are some example benchmarks.
min(data, key=operator.itemgetter(-1)) # 1x
min(data, key=_[-1]) # 1.3x
min(data, key=lambda x: x[-1]) # 1.6x
Installation
% pip install placeholder
Tests
100% branch coverage.
% pytest [--cov]
Changes
1.4
- Stable abi wheels
- Removed
func
attribute
1.3
- Python >=3.7 required
- Deprecated accessing
func
attribute of partial object
1.2.1
- Setup fix
1.2
- Python >=3.6 required
- Optimized
partial
implementation
1.1
- Additional unary functions
1.0
- Removed
__
(double underscore) - Variable arguments of first function
- Method callers and multi-valued getters
0.7
- Deprecated
__
(double underscore)
0.6
- Optimized composite functions
- Renamed to
_
(single underscore) for consistency
0.5
- Unary operators
__call__
implementsmethodcaller
__getitem__
supports only single argument- Improved error handling
composer
object deprecated in favor of optimizedF
expression
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
Built Distributions
Hashes for placeholder-1.4-cp37-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c19fbebf8efee138090a3c257898aa0ddf9ee77971f45b0fecca4710f92213f |
|
MD5 | 44677750d83db715e502118229c6b4ed |
|
BLAKE2b-256 | c8e489d89a80984809e6c4402ee12ec5cf4441358e36d1ffa9b7bd4c59a7ce4e |
Hashes for placeholder-1.4-cp37-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25a83ac095ccd5b6fc89c7a145883c8a30b4dcb786174cdcd08a5ec698eee75d |
|
MD5 | 2ba74f8f60fb3fdeb22b55e98f305c0f |
|
BLAKE2b-256 | ef143180a1e32db20c674e9ec2844c9d519a7ad2e6afae9bd1b84f7fea81ca10 |
Hashes for placeholder-1.4-cp37-abi3-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7397f1608e276181ad64198f42a66d0538ecb4576cf194c678954fcf5d5a8a2d |
|
MD5 | 06bfde4000d3e5f19f1ffcb1cc567253 |
|
BLAKE2b-256 | 10bce1377ae69b6943d885817dd4b27de6dffa49a7d9c943dfe80b15561eff62 |
Hashes for placeholder-1.4-cp37-abi3-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fe26e4c3174a6e2cbcc41a5b6ef280ecf213576d62dea79b4eec51a332c0c00 |
|
MD5 | a4217864bee412a34a6f254c7646aec4 |
|
BLAKE2b-256 | 0d5c5ee0e45c7333178cec69132b3a89091678cbbef45c38fe47a72b4d6962db |
Hashes for placeholder-1.4-cp37-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6e1f07f147cce5aee5d1b93c9ba6e7083bd066e2ee4593f0faa93c7755ea22d9 |
|
MD5 | 26c2cb6d159a57e0fd7bb402ff1b86cc |
|
BLAKE2b-256 | 53b2dd04a12dc1b25d472508ee162b956addf5ca3a2a282ff7ff612f1c061657 |
Hashes for placeholder-1.4-cp37-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11f1693be323fb264379c26365ed0177bfcb5d77fe8f72619a94bacd89e0cb51 |
|
MD5 | ea7d12cfd475997b5785dafa4013667d |
|
BLAKE2b-256 | 81a154ab6126ccaeea9ab45344c57a441394375425e4c849dcaaecaace24d30c |
Hashes for placeholder-1.4-cp37-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75e9e380d22347439ff22eeb1ec16d440bed7977ae2ec865b62167eb36246f22 |
|
MD5 | cf3e4fcf5b248cc28697d01d16b434a4 |
|
BLAKE2b-256 | 795b46e381eb331222e33e28dc114c698af9187d93f8cc68e9a86fd9e664260d |