about summary refs log tree commit diff stats
path: root/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-03_Table_Operations.html
blob: e6d372e9e7175d1333e50bd01c11ae761b6c0bb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>03_Table_Operations - Documentation</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav>
    <li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Tutorials</li><li class="nav-item"><a href="tutorial-00_Introduction.html">00_Introduction</a></li><li class="nav-item"><a href="tutorial-01_Function_Calls.html">01_Function_Calls</a></li><li class="nav-item"><a href="tutorial-02_Function_Composition.html">02_Function_Composition</a></li><li class="nav-item"><a href="tutorial-03_Table_Operations.html">03_Table_Operations</a></li><li class="nav-item"><a href="tutorial-04_Currying.html">04_Currying</a></li><li class="nav-item"><a href="tutorial-05_Pattern_Matching.html">05_Pattern_Matching</a></li><li class="nav-item"><a href="tutorial-06_Immutable_Tables.html">06_Immutable_Tables</a></li><li class="nav-item"><a href="tutorial-07_Function_References.html">07_Function_References</a></li><li class="nav-item"><a href="tutorial-08_Combinators.html">08_Combinators</a></li><li class="nav-item"><a href="tutorial-09_Expression_Based.html">09_Expression_Based</a></li><li class="nav-item"><a href="tutorial-10_Tables_Deep_Dive.html">10_Tables_Deep_Dive</a></li><li class="nav-item"><a href="tutorial-11_Standard_Library.html">11_Standard_Library</a></li><li class="nav-item"><a href="tutorial-12_IO_Operations.html">12_IO_Operations</a></li><li class="nav-item"><a href="tutorial-13_Error_Handling.html">13_Error_Handling</a></li><li class="nav-item"><a href="tutorial-14_Advanced_Combinators.html">14_Advanced_Combinators</a></li><li class="nav-item"><a href="tutorial-15_Integration_Patterns.html">15_Integration_Patterns</a></li><li class="nav-item"><a href="tutorial-16_Best_Practices.html">16_Best_Practices</a></li><li class="nav-item"><a href="tutorial-README.html">README</a></li><li class="nav-heading"><a href="global.html">Globals</a></li><li class="nav-item"><span class="nav-item-type type-member">M</span><span class="nav-item-name"><a href="global.html#callStackTracker">callStackTracker</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#debugError">debugError</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#debugLog">debugLog</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#executeFile">executeFile</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#initializeStandardLibrary">initializeStandardLibrary</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#interpreter">interpreter</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#lexer">lexer</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#main">main</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#parser">parser</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#readFile">readFile</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="global.html#run">run</a></span></li>
</nav>

<div id="main">
    
    <h1 class="page-title">03_Table_Operations</h1>
    

    <section>

<header>
    
</header>

<article>
    <h1>Table Operations</h1>
<h2>What are Element-Wise Operations?</h2>
<p>Element-wise operations automatically apply functions to every element in a table without explicit loops or iteration syntax like <code>forEach</code>.</p>
<pre class="prettyprint source lang-plaintext"><code>/* 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} */
</code></pre>
<p>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.</p>
<h2>Basic Examples</h2>
<pre class="prettyprint source lang-plaintext"><code>/* 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) */
</code></pre>
<h2>Table-Specific Operations</h2>
<p>The <code>t.</code> namespace provides additional element-wise operations especially meant for tables.</p>
<pre class="prettyprint source lang-plaintext"><code>/* Table-specific operations */
data : {a: 1, b: 2, c: 3};

/* Get all keys */
keys : t.keys data;  /* {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;} */

/* Get all values */
values : t.values data;  /* {1, 2, 3} */

/* Get key-value pairs */
pairs : t.pairs data;  /* {{key: &quot;a&quot;, value: 1}, {key: &quot;b&quot;, value: 2}, {key: &quot;c&quot;, value: 3}} */

/* Check if key exists */
has_a : t.has data &quot;a&quot;;  /* true */
has_d : t.has data &quot;d&quot;;  /* false */

/* Get value by key */
value_a : t.get data &quot;a&quot;;  /* 1 */
</code></pre>
<h2>Complex Examples</h2>
<pre class="prettyprint source lang-plaintext"><code>/* 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 */
</code></pre>
<h2>Nested Tables</h2>
<p>Element-wise operations work with nested table structures, too</p>
<pre class="prettyprint source lang-plaintext"><code>/* Nested table */
people : {
  alice: {name: &quot;Alice&quot;, age: 30, scores: {85, 90, 88}},
  bob: {name: &quot;Bob&quot;, age: 25, scores: {92, 87, 95}},
  charlie: {name: &quot;Charlie&quot;, 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} */
</code></pre>
<h2>The <code>each</code> Combinator</h2>
<p>The <code>each</code> combinator provides multi-argument element-wise operations:</p>
<pre class="prettyprint source lang-plaintext"><code>/* 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} */
</code></pre>
<h2>Immutability</h2>
<p>All element-wise operations return new tables. In Baba Yaga all values, including tables are immutable.</p>
<pre class="prettyprint source lang-plaintext"><code>/* 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} */
</code></pre>
</article>

</section>

</div>

<br class="clear">

<footer>
    Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 4.0.4</a> on Tue Jul 29 2025 23:15:00 GMT-0400 (Eastern Daylight Time) using the Minami theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>