An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

[![image](https://img.shields.io/pypi/v/placeholder.svg)](https://pypi.org/project/placeholder/)
![image](https://img.shields.io/pypi/pyversions/placeholder.svg)
[![image](https://pepy.tech/badge/placeholder)](https://pepy.tech/project/placeholder)
![image](https://img.shields.io/pypi/status/placeholder.svg)
[![build](https://github.com/coady/placeholder/actions/workflows/build.yml/badge.svg)](https://github.com/coady/placeholder/actions/workflows/build.yml)
[![image](https://codecov.io/gh/coady/placeholder/branch/main/graph/badge.svg)](https://codecov.io/gh/coady/placeholder/)
[![CodeQL](https://github.com/coady/placeholder/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/coady/placeholder/actions/workflows/github-code-scanning/codeql)
[![CodSpeed Badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/coady/placeholder)
[![image](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![image](https://mypy-lang.org/static/mypy_badge.svg)](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]
```