# Table Operations ## What are Element-Wise Operations? Element-wise operations automatically apply functions to every element in a table without explicit loops or iteration syntax like `forEach`. ```plaintext /* Instead of for each element in table, apply function */ /* You write function table */ numbers : {1, 2, 3, 4, 5}; doubled : map @double numbers; /* {2, 4, 6, 8, 10} */ ``` Most main-stream programming languages require explicit loops or iteration. Baba Yaga takes a clue from array languages like APL, BQN, uiua, K, etc., and automatically handles element-wise operations. ## Basic Examples ```plaintext /* Define a simple function */ double : x -> x * 2; /* Apply to table elements automatically */ numbers : {1, 2, 3, 4, 5}; result : map @double numbers; /* Result: {2, 4, 6, 8, 10} */ /* Filter elements automatically */ is_even : x -> x % 2 = 0; evens : filter @is_even numbers; /* Result: {2, 4} */ /* Reduce all elements automatically */ sum : reduce @add 0 numbers; /* Result: 15 (1+2+3+4+5) */ ``` ## Table-Specific Operations The `t.` namespace provides additional element-wise operations especially meant for tables. ```plaintext /* Table-specific operations */ data : {a: 1, b: 2, c: 3}; /* Get all keys */ keys : t.keys data; /* {"a", "b", "c"} */ /* Get all values */ values : t.values data; /* {1, 2, 3} */ /* Get key-value pairs */ pairs : t.pairs data; /* {{key: "a", value: 1}, {key: "b", value: 2}, {key: "c", value: 3}} */ /* Check if key exists */ has_a : t.has data "a"; /* true */ has_d : t.has data "d"; /* false */ /* Get value by key */ value_a : t.get data "a"; /* 1 */ ``` ## Complex Examples ```plaintext /* Data processing pipeline */ data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* Define helper functions */ is_even : x -> x % 2 = 0; double : x -> x * 2; sum : x -> reduce @add 0 x; /* Complete pipeline: filter → map → reduce */ result : sum map double filter is_even data; /* Step 1: filter @is_even data → {2, 4, 6, 8, 10} */ /* Step 2: map @double {2, 4, 6, 8, 10} → {4, 8, 12, 16, 20} */ /* Step 3: sum {4, 8, 12, 16, 20} → 60 */ /* Result: 60 */ ``` ## Nested Tables Element-wise operations work with nested table structures, too ```plaintext /* Nested table */ people : { alice: {name: "Alice", age: 30, scores: {85, 90, 88}}, bob: {name: "Bob", age: 25, scores: {92, 87, 95}}, charlie: {name: "Charlie", age: 35, scores: {78, 85, 82}} }; /* Extract ages */ ages : map (x -> x.age) people; /* Result: {alice: 30, bob: 25, charlie: 35} */ /* Calculate average scores for each person */ get_average : person -> reduce add 0 person.scores / 3; averages : map get_average people; /* Result: {alice: 87.67, bob: 91.33, charlie: 81.67} */ ``` ## The `each` Combinator The `each` combinator provides multi-argument element-wise operations: ```plaintext /* each for multi-argument operations */ numbers : {1, 2, 3, 4, 5}; multipliers : {10, 20, 30, 40, 50}; /* Multiply corresponding elements */ result : each @multiply numbers multipliers; /* Result: {10, 40, 90, 160, 250} */ /* Compare corresponding elements */ is_greater : each @greaterThan numbers {3, 3, 3, 3, 3}; /* Result: {false, false, false, true, true} */ ``` ## Immutability All element-wise operations return new tables. In Baba Yaga all values, including tables are immutable. ```plaintext /* Original table */ original : {a: 1, b: 2, c: 3}; /* Operations return new tables */ doubled : map @double original; /* {a: 2, b: 4, c: 6} */ greater_then : x -> x > 1; filtered : filter @greater_then original; /* {b: 2, c: 3} */ /* Original is unchanged */ /* original is still {a: 1, b: 2, c: 3} */ ```