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]
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.5-cp38-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e2d00afd020d97833dfe01a6d0cbc1d9114da4d794b58f5e89bfccf792c75b0 |
|
MD5 | dea61fc31f86033733222ce8896d7bd2 |
|
BLAKE2b-256 | d867f93730336b223691712b25175b908c69ef918a83f0aa6aece54770c1045f |
Hashes for placeholder-1.5-cp38-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a59b9c323ae40f08f64d8de5e7e39132297309ae67f50873c18a72e36d1d0350 |
|
MD5 | 359cfb278e3f4358cd4190313c0a482a |
|
BLAKE2b-256 | f8d061c3240eb2d7a00cd3df355f4323e3b1f0dba3a04c9843b8b3fa8d5d5441 |
Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7f1aac0735b860e6871cb22cd6c38070aa0a0b923ae9a65c6c040576c91edcb |
|
MD5 | 2e3db010e664dce235dd787a0d59ca9e |
|
BLAKE2b-256 | 310defcca660df27c43e29693b308459581f73411ce5ed57fce6ec26f3754258 |
Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 495e4fb47d419a87268f1cae413f3dd040c6158f467f69545e3c45e8e1d2dc8a |
|
MD5 | 17c7414cb8e17fcf3d57911684eca93d |
|
BLAKE2b-256 | 7065829180da3ef1b0fee491172a7e2a331efef95d0236cf95c3fc7898b0b030 |
Hashes for placeholder-1.5-cp38-abi3-musllinux_1_1_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4a5cf9e3dcb5b91513e7461b5852166cec3b1716e0c138a65e79c59cc3c3fdd |
|
MD5 | 8da8951671588a2cb3c2d335cf4879bb |
|
BLAKE2b-256 | 2f8055178e72a2e7530d3f6f96549f7b6994e851b000488876b888cda0932fb0 |
Hashes for placeholder-1.5-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0cbb4dce6dff86907ff04b461d8eab987e24be53f4ee2d8352586decad89a71 |
|
MD5 | cf61d6d6363b02d6664fd84e0a670701 |
|
BLAKE2b-256 | cd97b01aeb990d8a6f334bfb0e918cd4f5b496513bd5e61eb89f5d9e00a5012a |
Hashes for placeholder-1.5-cp38-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab843ac7df251ab6473bec1f5ba9347627925dbf1eb3374a68611ac0e5bad70b |
|
MD5 | f624a1e9c9da3dcc83f52cb710b2c223 |
|
BLAKE2b-256 | 19979a5dbd4ff4e10df0355d9ba99e43ec994dd37e13008faf6c5c15466ae2c1 |
Hashes for placeholder-1.5-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b877583ba7ed5514a4473a78eea98e7d55edbd34dea5c9a98ce15339b6fe8104 |
|
MD5 | 83d775e568772da24cc1b1eb2a8a69b7 |
|
BLAKE2b-256 | 19350a424184a42601b762af37a8d0d2f0157fb5226607d18ce88ea456654ef7 |
Hashes for placeholder-1.5-cp38-abi3-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dbd75adddf00c65540839c05576412d72d21ddeddcfed0284389f384fed0415 |
|
MD5 | 3385ab32dc0aaa2839ee72c18f79b30d |
|
BLAKE2b-256 | 30fff0540aaa49a1136cf971e5a9bff33f705325658d3f1659649678edf47d87 |
Hashes for placeholder-1.5-cp38-abi3-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcca22fc9d54d7972de186fe8aa5416ca39fd0ba9ecd1895370207b0f25da622 |
|
MD5 | 0197d36a1686b717978620fc56924961 |
|
BLAKE2b-256 | dedbf2340a0d080f3a68b089ec492a7da5724ca52d6586cd16fa6a24f8f4e8ae |