Getting Started with Lambda
Learn how to leverage Lambda's functional programming capabilities for clean, efficient, and maintainable code.
📦 Installation
npm Installation
npm install @lambda/core
CDN Usage
<script src="/lambda-beta-xi.com/dist/index.js"></script>
📘 Core Concepts
1. Pure Functions
Lambda enforces functional purity for predictable outcomes. Example:
const safeAdd = createLambda((a, b) => a + b);
console.log(safeAdd(2, 3)); // 5
2. Function Composition
Chain functions seamlessly with built-in combinators:
const formatter = compose(
JSON.stringify,
JSON.parse,
s => s.replace(/\s+/g, '')
);
formatter('{ "test": 123 }'); // '{"test":123}'
âš¡ Advanced Usage
Lazy Evaluation
Delay execution until output is required:
const lazy = delay(() => {
console.log("Computing...");
return 42;
});
console.log("Before computation");
lazy(); // "Computing..."
Tail Call Optimization
Prevent stack overflows in recursive functions:
const factorial = createLambda((n, acc = 1) =>
n === 0 ? acc : factorial(n - 1, n * acc)
);
factorial(5); // 120
✅ Best Practices
Immutable Data
Always treat data as immutable. Use:
const updated = pipe(
original,
map(x => x * 2),
filter(x => x > 10)
);
Function Caching
Optimize repeated computations with memoization:
const fib = createLambda((n) =>
n <= 1 ? n : fib(n-1) + fib(n-2)
);
const cachedFib = memoize(fib);
cachedFib(40); // Only computes once