Skip to main content

An alternative to functional programming

Project description

CLEX (Chained-Lambda-Expressions)

CLEX is a lightweight, string-based Domain Specific Language (DSL) for building clean, readable functional pipelines in Python.

If you've ever chained multiple map, filter, or zip calls together, you know how quickly things become unreadable:

result = list(map(lambda x: x * 2, filter(lambda x: x > 2, data)))

CLEX replaces this with a structured, expressive pipeline string that is easier to read, write, and maintain.


Installation

pip install clex_py

Quick Start

from CLEX import expression

# Compile a pipeline
add_one = expression("(x + 1:)")

# Execute it
result = add_one(x=[1, 2, 3, 4, 5])
print(result)  # [2, 3, 4, 5, 6]

Core Concept

A CLEX pipeline is a chain of small, single-purpose operations, written inside a string.

Function Format

(input operation value; return_variable)

Shortcut:

(input operation value:)

→ Automatically assigns the result to the default register _


Anatomy of a Function

Each function has four parts:

  1. Input Iterable (x)

    • The active dataset
  2. Operation

    • The transformation, condition, or function applied
  3. Acting Value

    • A scalar (broadcasted), or another iterable (zipped)
  4. Return Variable

    • Where the result is stored

Execution Model

  • Operations run element-wise
  • Scalars are automatically broadcasted
  • Iterables are zipped together
  • Execution stops at the shortest iterable
  • The final step’s result is automatically returned

Operators

Arithmetic

Operator Behavior
+ Addition
- Subtraction
* Multiplication
/ Division (skips division by zero)
% Modulus

Comparison (Filtering)

Comparison operators act as filters:

Operator Behavior
> Keep values greater than
< Keep values less than
>= Keep values ≥
<= Keep values ≤
= Keep equal values
!= Keep non-equal values

Advanced Operations

Operator Behavior
@ Index access (v1[v2])
& Truthy filter
!& Falsy filter
^ Intersection
!^ Difference
. Startswith check (returns True/False)

Chaining Pipelines

Use -> to chain operations:

pipeline = expression("(x + 1; r) -> (r + 2; r)")
result = pipeline(x=[1, 2, 3])

Example: Filtering Even Numbers

from CLEX import expression

is_even = expression("(x % 2; r) -> (x !& r:)")

result = is_even(x=[1, 2, 3, 4, 5])
print(result)  # [2, 4]

Custom Lambda Functions

CLEX supports user-defined transformations using square-bracket lambda syntax:

expr = expression("(x [lambda x, y: x + 1] 1; r)")
  • Note that lambdas are called for each element in an iterable passed as the first parameter with the second being the scalar.

Requirements

  • Must accept two parameters
  • Must return an iterable
  • Should produce results compatible with element-wise execution

Safety Model

Lambda expressions are executed using Python’s eval, but with strict controls:

  • Built-in functions are disabled by default
  • Safe functions can be enabled via a whitelist
  • Dunder methods are blocked unless explicitly allowed

This ensures controlled execution of dynamic expressions.


Parser & Engine

CLEX uses a modern parsing system designed for flexibility and performance:

  • Built with Lark
  • Supports arbitrarily long variable names
  • Optimized execution pipeline
  • Prefix-based result filtering system

Design Philosophy

CLEX is built around a few core ideas:

  • Clarity over nesting
  • Composable transformations
  • Minimal syntax, maximum expressiveness
  • Safe execution of dynamic logic

Roadmap

  • Port to Java, C#, and JavaScript
  • Expand built-in operations
  • Improve runtime performance and safety
  • Enable deeper integration with Python functions

Contributing

Contributions, issues, and suggestions are welcome.


Summary

CLEX provides:

  • A clean DSL for functional pipelines
  • A readable alternative to nested functional code
  • Safe execution of dynamic expressions
  • Extensible and composable transformations

It’s designed for developers who want powerful data transformations without sacrificing readability.

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

clex_py-0.1.6.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

clex_py-0.1.6-py3-none-any.whl (18.9 kB view details)

Uploaded Python 3

File details

Details for the file clex_py-0.1.6.tar.gz.

File metadata

  • Download URL: clex_py-0.1.6.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for clex_py-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f50561a2714a8f80a89c0e26bcfba4b9765c5127ae81fda9fbe9e62c560d537c
MD5 577c60a02a86d8f2483e1c477c09958f
BLAKE2b-256 8a8fb1c8a0022d6c34cddbd5e9ffd4458781505a2f76d6d6e5899434b8ebe157

See more details on using hashes here.

File details

Details for the file clex_py-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: clex_py-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 18.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.22 {"installer":{"name":"uv","version":"0.11.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for clex_py-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 07cf79fa2aa1640e924fc573fa5b8e21525c8c8d80f22f2519049a37a826f392
MD5 c824f798a10be08b75c225ef3f78b1af
BLAKE2b-256 789ef6fad9a8ba1cc5361a0f3c195300a6e64ba4c363ef580096e757687ddbd0

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