Quick Lambdas

from macropy.quick_lambda import macros, f, _

print(map(f[_ + 1], [1, 2, 3]))    # [2, 3, 4]
print(reduce(f[_ * _], [1, 2, 3])) # 6

Macropy provides a syntax for lambda expressions similar to Scala’s anonymous functions. Essentially, the transformation is:

f[_ * _] -> lambda a, b: a * b

where the underscores get replaced by identifiers, which are then set to be the parameters of the enclosing lambda. This works too:

print(map(f[_.split(' ')[0]], ["i am cow", "hear me moo"]))
# ['i', 'hear']

Quick Lambdas can be also used as a concise, lightweight, more-readable substitute for functools.partial

from macropy.quick_lambda import macros, f
basetwo = f[int(_, base=2)]
print(basetwo('10010')) # 18

is equivalent to

import functools
basetwo = functools.partial(int, base=2)
print(basetwo('10010')) # 18

Quick Lambdas can also be used entirely without the _ placeholders, in which case they wrap the target in a no argument lambda: ... thunk:

from random import random
thunk = f[random() * 2 + 3]
print(thunk()) # 4.522011062548173
print(thunk()) # 4.894243231792029

This cuts out reduces the number of characters needed to make a thunk from 7 (using lambda) to 2, making it much easier to use thunks to do things like emulating by name parameters. The implementation of quicklambda is about 30 lines of code, and is worth a look if you want to see how a simple (but extremely useful!) macro can be written.