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

Form
Syntax
Return Behavior

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

Implicit Return: Single-expression lambdas automatically return the expression result. Multi-line lambdas require explicit return statements.

🚫 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


Last updated

Was this helpful?