Easy way to make anonymous functions by partial application of operators.
Project description
An easy way to make lambdas by partial application of python operators.
Inspired by Perl 6 one, see http://perlcabal.org/syn/S02.html#The_Whatever_Object
Usage
from whatever import _, that
# get a list of guys names
names = map(_.name, guys)
names = map(that.name, guys)
odd = map(_ * 2 + 1, range(10))
squares = map(_ ** 2, range(100))
small_squares = filter(_ < 100, squares)
best = max(tries, key=_.score)
sort(guys, key=-that.height)
factorial = lambda n: reduce(_ * _, range(2, n+1))
NOTE: chained comparisons cannot be implemented since there is no boolean overloading in python.
CAVEATS
In some special cases whatever can cause confusion:
_.attr # this makes callable
obj._ # this fetches '_' attribute of obj
_[key] # this works too
d[_] # KeyError, most probably
_._ # short for attrgetter('_')
_[_] # short for lambda d, k: d[k]
if _ == 'Any value':
# You will get here, definitely
# `_ == something` produces callable, which is true
[1, 2, _ * 2, None].index('hi') # => 2, since bool(_ * 2 == 'hi') is True
Also, whatever sometimes fails on late binding:
(_ * 2)('2') # -> NotImplemented
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
whatever-0.7.tar.gz
(6.9 kB
view details)
Built Distribution
File details
Details for the file whatever-0.7.tar.gz
.
File metadata
- Download URL: whatever-0.7.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82d9ab46297c41e2fd4947684f666d4f06c632edd458caf4468417da51c84581 |
|
MD5 | dcea2d2be8701b0f6d0f821617b2c4fd |
|
BLAKE2b-256 | 13d8e640240bc42f78846b691b16d66591e4fde0c3db7a3dd1085cd82b3130a8 |
File details
Details for the file whatever-0.7-py3-none-any.whl
.
File metadata
- Download URL: whatever-0.7-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a862efbb11a88e9b162e88a59a5eb8b1e656df6c39c6e818f7902bf1d27d728 |
|
MD5 | ffc0216738eea7d71e2942cb182ad3de |
|
BLAKE2b-256 | 752b3ae9940b7e3a368160ba183aa97d0493b803da3f73dd46bb9808be0314eb |