Skip to main content

Express lambdas without `lamdbda`

Project description

Lamb 🐑: Concise Function Expression in Python

This module introduces a new way to express small functions in a compact and intutive way using a single object called lamb. Any expression in which a lamb appears turns into a function of which lamb is a parameter. Check demo.py to see a demonstration:

from lamb import *
# Primary purpose: Creating small anonymous functions
res = filter(lamb % 5 < 3, range(30))

# Arbitrary arithmetic and boolean operators are allowed:
f   = (lamb ** 2 % 6) - 28 != 5 / 4
f_l = lambda x: (x ** 2 % 6) - 28 != 5 / 4
f(2) == f_l(2) # The two expressions are equivalent

# Empty function calls, index and attribute access are possible as well
g = lamb().x[2] - 3 == None

# Lambs can be chained with other functions using the righshift operator
h = g >> f >> lamb + 2 # make sure the first function is a lamb

# For multi-variable lambs, different lamb names improve clarity
from lamb.vs import * # Imports a, b, c, ..., z
from lamb.l_vs_ import * # Import l_a_, l_b_, .. arbitrary pre- and postfixes are possible
g = (l_a_ - b) * c

# g is now a function of three arguments, or rather: functions returning functions,
# with one argument each (shoutout to Haskell)
g(1)(2)(3) == g(1, 2)(3) == g(1, 2, 3)

# Arguments replace lambs left-to-right. The following will output -3:
g(1, 2, 3)

# Note that `a_ is b_` and `a_ is lamb`; they differ only in their identifier. 
# In other words, the following are equivalent:
g2 = (lamb - lamb) * lamb
g(1, 2, 3) == g2(1, 2, 3)

# Lambs can be nested. Parents inherit un-evaluated lambs from their children.
# For clarity, lambs can be added as placeholder for unevaluated args:
h  = g(1, 2) + g(3) 
h2 = g(1, 2, a) + g(3, a, b)
h(1, 2, 3) == h2(1, 2, 3)

# In a select few cases, this will work, but is discouraged. Use g = lamb >> f instead.
f = lambda x: x + 2 == 5
g = f(lamb)
f(2) == g(2)

Installation

lamb is installable from PyPI:

python -m pip install lamb

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

lamb-0.0.1.post1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

lamb-0.0.1.post1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file lamb-0.0.1.post1.tar.gz.

File metadata

  • Download URL: lamb-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0

File hashes

Hashes for lamb-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 572e414054b912c868c54298a826455066c130f5349aefe6681421beb32f7d3b
MD5 3a52a44587b158eab5577a08e7168b2c
BLAKE2b-256 a4785e0cabe1b8ea642cb2d6d23b55d0744152a3431252f502f342a90d4ab914

See more details on using hashes here.

File details

Details for the file lamb-0.0.1.post1-py3-none-any.whl.

File metadata

  • Download URL: lamb-0.0.1.post1-py3-none-any.whl
  • Upload date:
  • Size: 4.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.0

File hashes

Hashes for lamb-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 8fd82e40921dd83320f01d9dbcb4bcd2e319810e7fca3e8be40d8b352b45ce11
MD5 c3d648ad5af1ce134c2b51756a39ef03
BLAKE2b-256 0cce7313985ea4369186f0c6a2caa9caec068e5bafe3b2d6e3bdab9fe4d74882

See more details on using hashes here.

Supported by

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