Lambdas -> Pure Functions
Lambdas are deterministic functions that operate only on their arguments and local variables - they do NOT capture surrounding scope.
Lambdas are BoxLang's implementation of pure, deterministic functions using the skinny arrow (->) syntax. Unlike Closures which capture surrounding scope, lambdas are isolated and only work with what they're explicitly given.
π¬ What Makes Lambdas Special?
Lambdas are fundamentally different from closures and functions in BoxLang:
β Deterministic - Same inputs always produce same outputs
β No Scope Capture - Cannot access variables from surrounding scope
β Pure Functions - Only use arguments passed in and local variables
β Predictable - No side effects from external state
β Thread-Safe - Safe for parallel operations without race conditions
β Testable - Easy to test due to deterministic behavior
Lambda vs Closure vs Function
Feature
Lambda ->
Closure =>
Function
Syntax
( args ) -> expression
( args ) => expression
function( args ) { }
Scope Capture
β No
β Yes
β No
Deterministic
β Yes
β No
β Yes
Thread-Safe
β Yes
β οΈ Depends
β Yes
Use Case
Pure transforms
Context-aware logic
General purpose
π Lambda Syntax
Lambdas use the skinny arrow -> operator:
Syntax Rules
Single expression
( args ) -> expression
Implicit return
Multi-line
( args ) -> { statements }
Explicit return required
No arguments
() -> expression
Parentheses required
Single argument
arg -> expression
Parentheses optional
Multiple arguments
( arg1, arg2 ) -> expression
Parentheses required
π« No Scope Capture
The defining characteristic of lambdas is that they cannot access surrounding scope. They are completely isolated.
What Lambdas Can Access
β Arguments - Parameters passed to the lambda
β Local Variables - Variables declared inside the lambda
β Built-in Functions - BoxLang BIFs (Built-In Functions)
β Static Values - Literals and constants
What Lambdas Cannot Access
β Outer Variables - Variables from surrounding scope
β Class Properties - this.property references
β Closure Scope - Parent function's local variables
β Global State - Unless passed as arguments
Scope Isolation Example
Comparison: Lambda vs Closure
β‘ Deterministic Behavior
Lambdas are deterministic - given the same inputs, they always produce the same output. This makes them predictable and safe.
Deterministic Example
Non-Deterministic Operations
Some operations would break determinism - these require external state that lambdas cannot access:
π Common Use Cases
1. Array Transformations
Lambdas are perfect for pure data transformations:
2. String Processing
3. Object/Struct Transformations
4. Sorting
5. Validation & Predicates
π¨ Practical Examples
Example 1: Data Pipeline
Example 2: Number Processing
Example 3: Price Calculator
Example 4: Functional Composition
π‘ Best Practices
1. Keep Lambdas Pure
2. Use Lambdas for Simple Transformations
3. Leverage Determinism for Testing
4. Use for Parallel Operations
5. Compose Small Lambdas
6. Name Complex Lambdas
β οΈ Common Mistakes
Mistake 1: Trying to Access Outer Scope
Mistake 2: Side Effects
Mistake 3: Using Non-Deterministic Functions
Mistake 4: Confusing Lambda and Closure Syntax
π§ When to Use Lambdas vs Closures
Use Lambdas (->) When:
β Pure data transformations
β Mathematical operations
β String manipulations
β Array/collection operations
β Sorting and filtering
β Validation predicates
β Deterministic behavior required
β Thread-safety is important
Use Closures (=>) When:
β Need to access surrounding scope
β Event handlers
β Callbacks with context
β State management
β Delayed execution with captured variables
β Dynamic behavior based on environment
π Summary
Lambdas in BoxLang are pure, deterministic functions perfect for functional programming:
β
Skinny Arrow - Use -> syntax
β No Scope Capture - Only arguments and local variables
β Deterministic - Same inputs = same outputs
β Thread-Safe - No shared state concerns
β Pure Functions - No side effects
β Testable - Easy to test and verify
β Composable - Chain together easily
π Related Documentation
Closures - Context-aware functions with scope capture (
=>)Functions - User-defined functions
Arrays - Array methods that use lambdas
Structures - Struct methods with lambdas
Variable Scopes - Understanding BoxLang scopes
Last updated
Was this helpful?
