https://github.com/coady/placeholder
Operator overloading for fast anonymous functions.
https://github.com/coady/placeholder
functional lambda partial underscore
Last synced: 10 months ago
JSON representation
Operator overloading for fast anonymous functions.
- Host: GitHub
- URL: https://github.com/coady/placeholder
- Owner: coady
- License: other
- Created: 2015-09-19T22:56:35.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-09-28T05:49:03.000Z (over 1 year ago)
- Last Synced: 2024-10-14T09:41:34.139Z (over 1 year ago)
- Topics: functional, lambda, partial, underscore
- Language: Python
- Homepage: https://coady.github.io/placeholder
- Size: 775 KB
- Stars: 50
- Watchers: 2
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
[](https://pypi.org/project/placeholder/)

[](https://pepy.tech/project/placeholder)

[](https://github.com/coady/placeholder/actions/workflows/build.yml)
[](https://codecov.io/gh/coady/placeholder/)
[](https://github.com/coady/placeholder/actions/workflows/github-code-scanning/codeql)
[](https://codspeed.io/coady/placeholder)
[](https://github.com/astral-sh/ruff)
[](https://mypy-lang.org/)
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
```python
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.
```python
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.
```python
(func,) = _.age # operator.attrgetter('age')
```
Performance should generally be comparable to inlined expressions, and faster than lambda. Below are some example benchmarks.
```python
min(data, key=operator.itemgetter(-1)) # 1x
min(data, key=_[-1]) # 1.3x
min(data, key=lambda x: x[-1]) # 1.6x
```
## Installation
```console
% pip install placeholder
```
## Tests
100% branch coverage.
```console
% pytest [--cov]
```