Lambdas & Functional Pipelines

SP treats functions as first-class values. You can define anonymous arrow functions (params) => expr and pass them into higher-order functions like map, filter, and reduce.

lambdas.sp
1import std.io;
2import std.collections;
3
4fn main() -> void {
5 let numbers = [1, 2, 3, 4, 5, 6, 7, 8];
6
7 // 1. Arrow lambda syntax
8 let evens = numbers.filter((int n) => n % 2 == 0);
9
10 // 2. Chained transformation
11 let doubled = evens.map((int n) => n * 2);
12
13 // 3. Closure capturing lexical scope
14 int multiplier = 5;
15 let customScaled = numbers.map((int n) => n * multiplier);
16
17 println("Original length: ${numbers.length()}");
18 println("Evens length: ${evens.length()}");
19}

Lexical Closure Capture

Lambdas can capture variables from their enclosing lexical scope. Captured variables are packed into an automatic closure context struct and lowered into LLVM IR safely.