Functional
Source: examples/functional.ion in the repo.
// Functional programming patterns
// Higher-order functionsfn compose(f, g) { |x| f(g(x))}
let double = |x| x * 2;let inc = |x| x + 1;let double_then_inc = compose(inc, double);io::println(f"double_then_inc(5) = {double_then_inc(5)}"); // 11
// Curryingfn add(a) { |b| a + b}let add10 = add(10);io::println(f"add10(5) = {add10(5)}"); // 15
// Method chaining pipelinelet result = [1, 2, 3, 4, 5] .map(|x| x * x) .filter(|x| x > 5) .fold(0, |a, b| a + b);io::println(f"Sum of squares > 5: {result}"); // 9 + 16 + 25 = 50
// flat_maplet nested = [[1, 2], [3, 4], [5]];let flat = nested.flat_map(|xs| xs.map(|x| x * 10));io::println(f"flat_map result: {flat}"); // [10, 20, 30, 40, 50]
// reduce (no initial value needed)let product = [1, 2, 3, 4, 5].reduce(|a, b| a * b);io::println(f"Product: {product}"); // 120
// ziplet names = ["Alice", "Bob", "Charlie"];let scores = [95, 82, 73];let paired = names.zip(scores);io::println(f"Zipped: {paired}"); // [(Alice, 95), (Bob, 82), (Charlie, 73)]
// chunklet data = [1, 2, 3, 4, 5, 6, 7];io::println(f"Chunks of 3: {data.chunk(3)}"); // [[1, 2, 3], [4, 5, 6], [7]]
// sort_bylet words = ["banana", "apple", "cherry"];let sorted = words.sort_by(|a, b| a.len() - b.len());io::println(f"By length: {sorted}"); // [apple, banana, cherry]
// Comprehensionslet evens = [x * 2 for x in 0..10];io::println(f"Evens: {evens}");
let doubled = [(x, x * 2) for x in 0..4 if x > 0];io::println(f"Doubled: {doubled}");
// Dict comprehensionlet squares = #{str(n): n * n for n in 1..=5};io::println(f"Squares: {squares}");
// Pipeline operator |>// |> passes as first argument: x |> f(y) = f(x, y)let words = "hello world";let result = words.split(" ") .map(|w| w.to_upper()) .join(" -> ");io::println(f"Pipeline: {result}"); // HELLO -> WORLD
// any/alllet nums = [2, 4, 6, 8];io::println(f"All even? {nums.all(|x| x % 2 == 0)}"); // trueio::println(f"Any > 5? {nums.any(|x| x > 5)}"); // true