For the complete documentation index, see llms.txt. This page is also available as Markdown.

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


  • 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?