Skip to main content

Operator overloading for fast anonymous functions.

Project description

About Placeholder

https://img.shields.io/pypi/v/placeholder.svg https://img.shields.io/pypi/pyversions/placeholder.svg https://img.shields.io/pypi/status/placeholder.svg https://img.shields.io/travis/coady/placeholder.svg https://img.shields.io/codecov/c/github/coady/placeholder.svg

Operator overloading for fast anonymous functions.

A placeholder object 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.

>>> __[key]
operator.itemgetter(key)

>>> __.name
operator.attrgetter('name')

>>> 1 + __
(1).__add__

>>> __ - 1
(1).__rsub__

>>> -__
operator.neg

An F expression extends placeholders with function composition, but at a slight performance cost. They can be created explicitly from a function, or implicitly through operators as with placeholders.

>>> F(len) + 1
lambda obj: len(obj) + 1

>>> (___ * 2) + 1
lambda obj: obj * 2 + 1

__ and ___ are placeholder and F singletons which can be imported from the module, but each can of course be instantiated and bound to any desired name.

See tests for more example usage.

Installation

Standard installation from pypi or local download.

$ pip install placeholder
$ python setup.py install

Dependencies

Python 2.7 or 3.3+.

Tests

100% branch coverage.

$ py.test [--cov]

Changes

0.5

  • Unary operators

  • __call__ implements methodcaller

  • __getitem__ supports only single argument

  • Improved error handling

  • composer object depecated in favor of optimized F expression

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

placeholder-0.5.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file placeholder-0.5.tar.gz.

File metadata

  • Download URL: placeholder-0.5.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for placeholder-0.5.tar.gz
Algorithm Hash digest
SHA256 41e9cbc22e1fef582eefde85355fdeaae1c42932239c27ef28b6c732c352e85b
MD5 c39b74df3cfbe8ddcd6f9a6b677158b4
BLAKE2b-256 cd56e9aac9c8124c654ea95c124061611d443fa45b58a7548e8d4e843d4cb9a3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page