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.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.3-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c5a6b6665012a57c3be6ca2fd093d60069d3ddaae85234ebc499e8a3010f6b9 |
|
MD5 | bf475c7280370b0fe1fb1abaf5d55f6d |
|
BLAKE2b-256 | bd4b8058698f3f63b6f7d10f25f500801f44320413bd0330f077a2aa2ada6801 |
Hashes for placeholder-1.3-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07cab015f6dffd9334d0e19be7b8575947a971164dc07f9f98cfca85bda18bcb |
|
MD5 | db25a1ee97e4edb61ff2f6e42b5a6dcb |
|
BLAKE2b-256 | b39d20fd4073d13ebe32dea7faff75bb4f3c84d427073871b375d735cd92a255 |
Hashes for placeholder-1.3-cp310-cp310-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc4fdecbb62cb2505888822076640794e461ef4c17bcb21525f7b0e8b1c60ae7 |
|
MD5 | e3c8be6ab322ae2853ccc91232cdb928 |
|
BLAKE2b-256 | 54768e989d9941830ad05e75fb3c67d9db7e1960ea36375a5b2dd0d3545b329d |
Hashes for placeholder-1.3-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e3af3ed8b176f1ea783ee3442d423af8aada5741d9867bb33565583cf8888e2d |
|
MD5 | 8ca0cdfc1fb126ff481a90c1853c87ff |
|
BLAKE2b-256 | 0ae5cfcf09f9fd118a0a18ad60fdea24e4e65d0119b3f640fa6c3e76b2078e5e |
Hashes for placeholder-1.3-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3728a3e303616e7457e774cca4a93a973fa84282dcb71b825198966d41fddce7 |
|
MD5 | 3bbe2704db279336ae916203f47fdf78 |
|
BLAKE2b-256 | 57f539983314486ced2a3cb72104621df1300cc12965c008e4e7e536cabf28d8 |
Hashes for placeholder-1.3-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2a6bb334458a92c3f0bc93983c934b1d7cb627b6d2f383f8ad0233976391e847 |
|
MD5 | 215060e46649898cec8b303a8b9a86bb |
|
BLAKE2b-256 | 781c610ad5b4d1b8be7fdfeacf051e37e32ededc7d9db03b8efbe6373070f9d5 |
Hashes for placeholder-1.3-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 602a5195c23a5148d7c741c8288b6f699cee3a5d7189413ace373af2d50a2ebf |
|
MD5 | 6bafd0986077802e7869035803aa27ff |
|
BLAKE2b-256 | 40441a1d823729bbc2ec81169340d81542d90a2c7533c3fa44e9cbc4ed43d7b6 |
Hashes for placeholder-1.3-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ef7811368fa56593814385dd4f7620e40e42e298d0ec2f23e82d90b58622b5a |
|
MD5 | 7bab54d8c9bb03ae33de3739c1ca19d4 |
|
BLAKE2b-256 | a730700fd05fe6a02c6f28c0a15edf328b4aef152d9e9c48d7a4b17fa81b1f7b |
Hashes for placeholder-1.3-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91192f7664f47f293203103f3997e4a125c5a89d1d4a35b991fedea3503cb8aa |
|
MD5 | f31ed2027885a62f0eeaab02d5f87249 |
|
BLAKE2b-256 | cef0eda98f6b052de351fbceb9e6ded0e1fec20b233446b09f1bdaf18a66f615 |
Hashes for placeholder-1.3-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 11dd75bd36a1ff260b8e315a7389fce57c2a7c70f81a2669702c65a380635872 |
|
MD5 | ff8b063d5ff44265194d6eb6b33cea79 |
|
BLAKE2b-256 | df730aaaecb4034141de01094e4da5e7a4953786d08e3af0acbedf24a1681ad3 |
Hashes for placeholder-1.3-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_24_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3892a419277c7cfea1070bb31761d543fff59a1d2706a039528cfa298990d0c |
|
MD5 | ca99ce367ea1a49a39e5cf2b047d2b33 |
|
BLAKE2b-256 | e763fb2bd28bfe84ca36782de30972ba0552a9b343fcb7b96f60c50dbd1b0df8 |
Hashes for placeholder-1.3-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c84d3ec2c4a6345abaa73e1bb163a49ca8c22a864500cb4e9fe76aa9021a11c |
|
MD5 | 85901bf517af937f4548e20b090430a9 |
|
BLAKE2b-256 | adbf3e25a3a1f0f0e04c71ebb2d00f72bcbaa378b88cfaff7bc2649099798fbb |