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;34fn main() -> void {5 let numbers = [1, 2, 3, 4, 5, 6, 7, 8];67 // 1. Arrow lambda syntax8 let evens = numbers.filter((int n) => n % 2 == 0);910 // 2. Chained transformation11 let doubled = evens.map((int n) => n * 2);1213 // 3. Closure capturing lexical scope14 int multiplier = 5;15 let customScaled = numbers.map((int n) => n * multiplier);1617 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.