An alternative to functional programming
Project description
CLEX (Chained-Lambda-Expressions)
CLEX is a lightweight, string-based Domain Specific Language (DSL) designed to clean up functional programming pipelines in Python.
If you have ever tried to chain multiple map, filter, and zip operations together in Python, you know how quickly the code turns into an unreadable nest of parentheses:
# Messy and tedious to maintain
result = list(map(lambda x: x * 2, filter(lambda x: x > 2, data)))
CLEX offers a clean, readable alternative by allowing you to write functional pipelines as a single, expressive string.
Getting Started:
Installation:
Install CLEX via pip:
pip install clex_py
Importing:
To get started, import the expression function. This compiles your pipeline string into a callable runner.
from CLEX import expression
# Compile the expression
add_one = expression("(x + 1:)")
# Run it by passing your iterables as keyword arguments
result = add_one(x=[1, 2, 3, 4, 5])
print(result) # Output: [2, 3, 4, 5, 6]
Syntax & Anatomy:
A CLEX pipeline lives inside a single string. It relies on a simple, strict syntax where everything is a single-purpose function.
A single CLEX function is written inside parentheses:
"input_iterable operation acting_value; return_variable"
The 4 Components of a Function:
-
Input Iterable (input_iterable): A single-character variable representing the collection you want to act on (e.g., x). This becomes the active iterable.
-
Operation (operation): The action to perform on each element.
-
Acting Value (acting_value): Can be a scalar (e.g., 5) or another iterable (e.g., y).
-
Scalar Behavior: Automatically scaled (broadcasted) to match the size of the input iterable.
-
Iterable Behavior: If you pass another collection, CLEX zips them together. If they are of unequal length, execution safely stops at the shortest length.
-
-
Return Variable (return_variable): The variable where the result is saved.
- The Shortcut: If you don't need to save the result to a specific variable name, replace the semicolon and variable ; r with a colon : to automatically assign it to the default register (_).
Supported Operators:
Arithmetic
| Operator | Description | Behavior |
|---|---|---|
| + | Addition | v1 + v2 |
| - | Subtraction | v1 - v2 |
| * | Multiplication | v1 * v2 |
| / | Division | v1 / v2 (safely skipped if v2 == 0) |
| % | Modulus | v1 % v2 |
Comparison & Filtering
Note: Comparison operators act as filters. Only elements that satisfy the condition are passed through to the output.
| Operator | Description | Behavior |
|---|---|---|
| > | Greater Than | Keeps elements where v1 > v2 |
| < | Less Than | Keeps elements where v1 < v2 |
| >= | Greater Than or Equal | Keeps elements where v1 >= v2 |
| <= | Less Than or Equal | Keeps elements where v1 <= v2 |
| = | Equal To | Keeps elements where v1 == v2 |
| != | Not Equal To | Keeps elements where v1 != v2 |
Advanced & Set Operations
| Operator | Description | Behavior |
|---|---|---|
| @ | Element At | v1[v2] (Only supports scalar indices) |
| & | Parse (Truthy Filter) | v1[i] if v2[i] is truthy |
| !& | Inverse Parse | v1[i] if v2[i] is falsy |
| ^ | Inset Set intersection | Retrieves elements found in both v1 and v2 |
| !^ | Not-Inset Set difference | Retrieves unique elements in v1 not present in v2 |
Chaining Pipelines
While CLEX is technically Turing Complete with just these operations, its true power comes from chaining operations together using the -> operator.
# Adds 1 to x, stores it in 'r', then takes 'r', adds 2, and returns it
pipeline = expression("(x + 1; r) -> (r + 2; r)")
To execute a pipeline, simply call the function returned by the expression function or call the eval method
result = pipeline(x=[1,2,3,4,5]) # Standard way
result = expression.eval(x=[1,2,3,4,5]) # Alternative way
# Both behave the exact same
During execution, the value of the last evaluated expression is automatically returned.
Full Example: Filtering Even Numbers
Let's build a program that takes an input x and filters out only the even numbers:
from CLEX import expression
# 1. (x % 2; r) -> Calculates modulo 2 for all elements, stores the 1s and 0s in 'r'
# 2. (x !& r:) -> Keeps elements of 'x' where 'r' is falsy (where mod 2 resulted in 0)
is_even = expression("(x % 2; r) -> (x !& r:)")
result = is_even(x=[1, 2, 3, 4, 5])
print(result)
# Output:
# [2, 4]
Roadmap:
- Bringing CLEX to Java, C#, and JS
- Expanding the built-in standard library of operations
- Allowing you to inject native Python functions directly into the pipeline string
Feel free to fork this project and report suggestions or bugs!
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clex_py-0.1.1.tar.gz.
File metadata
- Download URL: clex_py-0.1.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.15.0b1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c88b59c52dc6bf1e83e2bf030530369907c13ff1b0cd45c70827a360a0d01f66
|
|
| MD5 |
f1a66d96ee3481073b71d3287d831220
|
|
| BLAKE2b-256 |
a214e08690f90076390008530945d7e2bc117386fa8cc2be7a238f55316d82ea
|
File details
Details for the file clex_py-0.1.1-py3-none-any.whl.
File metadata
- Download URL: clex_py-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.15.0b1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d3f4eae2b932748920fbb053aecdb71a78279e61fd14198470658cfad2fd2e2d
|
|
| MD5 |
49cff53b36d51c91f2b0f154abee044b
|
|
| BLAKE2b-256 |
780fdd4fcd145d1d14776c52bebcfcb5d82bb8c4770409f68190584ef6fffa79
|