Skip to main content

Syntactic Sugar for Lambda Expressions

Project description

Highlambder - there can be only one... lambda to rule them all

Syntactic sugar for Python lambda expressions. Make your anonymous functions shorter, more expressive, and (let's be honest) just more fun.

there can be only one

Image used under fair use, for illustrative and non-commercial purposes. All rights to the character and image belong to their respective owners.

What is Highlambder?

Highlambder is an experimental library that lets you write more elegant and compact code by replacing Python’s lambda syntax with something shorter: a symbolic placeholder that behaves like a function.

In practice, lambda x: x + 1 can be written simply as λ + 1.

Installation

pip install highlambder

Quick Examples

from highlambder import L as λ

λ (10)  # -> 10

(λ + 5) (10)  # -> 15

(λ * 5) (10)  # -> 50

(3 + λ * 2) (10)  # -> 23

(40 / λ / 5) (2)  # -> 4

(10 * λ[1]) ([1, 2, 3])  # -> 20

(-1 + λ * 5 / λ + 1) (13)  # -> 5

(λ * 2 + λ * 4 + λ) (10)  # -> 70

(λ['A'] + λ['B']) ({'A': 3, 'B': 4})  # -> 7

(λ + λ) (2)  # -> 4

map(λ * 2, range(5)) # -> [0, 2, 4, 6, 8]

("It's a Me, " + λ) ('Mario!') # ->  'It's a Me, Mario!'

pandas and NumPy support (partially supported, work-in-progress)

import pandas as pd
import numpy as np
from highlambder import L as λ

# pandas:
s = pd.Series([1, 2, 3, 4])
assert pd.Series.equals(
    2 * s,
    s.map(2 * λ),
)

df = pd.DataFrame({
    'A': [1, 1, 2, 2],
    'B': [5, 6, 7, 8],
    'C': ['banana', 'apple', 'kiwi', 'orange'],
})

assert pd.DataFrame.equals(
    df.assign(D=lambda d: d.A + 20),
    df.assign(D=λ.A + 20),
)

# String operations
assert pd.DataFrame.equals(
    df.assign(D=lambda d: d['C'].str.len() * 2),
    df.assign(D=λ['C'].str.len * 2),
)

# NumPy:
assert (λ + 2)(np.int64(2)) == 4

assert (λ.max)(np.array([3, 4, 5, 6, 7, 8])) == 8

Limitations (for now)

  • Only single-argument functions are supported.
  • Calling a function passing λ as an arument usually doesn't work. For example: len(λ).

These limitations may be lifted in future versions.

License

MIT

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

highlambder-0.1.3.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

highlambder-0.1.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file highlambder-0.1.3.tar.gz.

File metadata

  • Download URL: highlambder-0.1.3.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for highlambder-0.1.3.tar.gz
Algorithm Hash digest
SHA256 34c69e55d2aba497db7e9d6f590321858e6306101e9797ae340cb2101fcd919f
MD5 63f70b916de95206e3102cb94c44c8ac
BLAKE2b-256 86d8e4bb9daf97135751c3a14199275b4ceabaef5731588ff36c1928ca1dedd1

See more details on using hashes here.

File details

Details for the file highlambder-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: highlambder-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.2

File hashes

Hashes for highlambder-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 44a5eec1441ef37e02778fb0265c8761fd4fb32c212d72672483e8994dd9965a
MD5 0987efb71ad44d7a0a7270f07f4c89e7
BLAKE2b-256 8c19e38c9e4f785f8d5f95df3f95b7099a4b2644e5c0605e21591ed230550884

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