diff options
Diffstat (limited to 'js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-06_Immutable_Tables.html')
-rw-r--r-- | js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-06_Immutable_Tables.html | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-06_Immutable_Tables.html b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-06_Immutable_Tables.html new file mode 100644 index 0000000..3829487 --- /dev/null +++ b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-06_Immutable_Tables.html @@ -0,0 +1,266 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <title>06_Immutable_Tables - 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">06_Immutable_Tables</h1> + + + <section> + +<header> + +</header> + +<article> + <h1>Immutable Tables with Functional Operations</h1> +<h2>What are Immutable Tables?</h2> +<p>Immutable tables are data structures that <strong>cannot be modified after creation</strong>. All operations on tables return <strong>new tables</strong> rather than modifying the original.</p> +<pre class="prettyprint source lang-plaintext"><code>/* All table operations return new tables */ +original : {a: 1, b: 2, c: 3}; +modified : t.set original "d" 4; /* Returns new table */ +/* original is unchanged: {a: 1, b: 2, c: 3} */ +/* modified is: {a: 1, b: 2, c: 3, d: 4} */ +</code></pre> +<h2>Why is This Esoteric?</h2> +<p>Most programming languages allow direct modification of data structures. Our language enforces <strong>complete immutability</strong> - no mutation operations exist at all.</p> +<h2>Basic Examples</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Create a table */ +original : {name: "Alice", age: 30, city: "New York"}; + +/* All operations return new tables */ +with_job : t.set original "job" "Engineer"; +with_updated_age : t.set original "age" 31; +without_city : t.delete original "city"; + +/* Original table is unchanged */ +/* original is still {name: "Alice", age: 30, city: "New York"} */ +</code></pre> +<h2>Table Operations</h2> +<h3>Setting Values</h3> +<pre class="prettyprint source lang-plaintext"><code>/* t.set table key value - returns new table with key set */ +data : {a: 1, b: 2}; +updated : t.set data "c" 3; +/* updated: {a: 1, b: 2, c: 3} */ +/* data: {a: 1, b: 2} (unchanged) */ +</code></pre> +<h3>Deleting Keys</h3> +<pre class="prettyprint source lang-plaintext"><code>/* t.delete table key - returns new table without the key */ +data : {a: 1, b: 2, c: 3}; +without_b : t.delete data "b"; +/* without_b: {a: 1, c: 3} */ +/* data: {a: 1, b: 2, c: 3} (unchanged) */ +</code></pre> +<h3>Merging Tables</h3> +<pre class="prettyprint source lang-plaintext"><code>/* t.merge table1 table2 - returns new table with combined keys */ +table1 : {a: 1, b: 2}; +table2 : {c: 3, d: 4}; +merged : t.merge table1 table2; +/* merged: {a: 1, b: 2, c: 3, d: 4} */ +/* table1 and table2 unchanged */ +</code></pre> +<h3>Getting Values</h3> +<pre class="prettyprint source lang-plaintext"><code>/* t.get table key - returns value (doesn't modify table) */ +data : {name: "Alice", age: 30}; +name : t.get data "name"; /* "Alice" */ +age : t.get data "age"; /* 30 */ +/* data unchanged */ +</code></pre> +<h3>Checking Keys</h3> +<pre class="prettyprint source lang-plaintext"><code>/* t.has table key - returns boolean (doesn't modify table) */ +data : {name: "Alice", age: 30}; +has_name : t.has data "name"; /* true */ +has_job : t.has data "job"; /* false */ +/* data unchanged */ +</code></pre> +<h2>Element-Wise Operations</h2> +<p>All element-wise operations return new tables:</p> +<pre class="prettyprint source lang-plaintext"><code>/* map returns new table - @ operator required for higher-order functions */ +numbers : {a: 1, b: 2, c: 3}; +double : x -> x * 2; +doubled : map @double numbers; /* {a: 2, b: 4, c: 6} */ +/* numbers unchanged: {a: 1, b: 2, c: 3} */ + +/* filter returns new table - @ operator required for higher-order functions */ +is_greater_than_one : x -> x > 1; +filtered : filter @is_greater_than_one numbers; /* {b: 2, c: 3} */ +/* numbers unchanged: {a: 1, b: 2, c: 3} */ +</code></pre> +<h2>Complex Examples</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Building complex tables immutably */ +base_user : {name: "Alice", age: 30}; + +/* Add multiple properties */ +with_email : t.set base_user "email" "alice@example.com"; +with_address : t.set with_email "address" "123 Main St"; +with_phone : t.set with_address "phone" "555-1234"; + +/* Or merge with another table */ +contact_info : {email: "alice@example.com", phone: "555-1234"}; +complete_user : t.merge base_user contact_info; +/* Result: {name: "Alice", age: 30, email: "alice@example.com", phone: "555-1234"} */ +</code></pre> +<h2>Nested Tables</h2> +<p>Immutability works with nested table structures:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Nested table */ +user : { + name: "Alice", + profile: { + age: 30, + preferences: { + theme: "dark", + notifications: true + } + } +}; + +/* Update nested property - creates new nested structure */ +updated_preferences : t.set user.profile.preferences "theme" "light"; +/* This creates new tables at each level */ +/* user unchanged, updated_preferences has new nested structure */ +</code></pre> +<h2>Functional Programming Patterns</h2> +<p>Immutability enables pure functional programming patterns:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Pure function - no side effects */ +update_age : user new_age -> t.set user "age" new_age; + +/* Multiple updates create new tables */ +user1 : {name: "Alice", age: 30}; +user2 : update_age user1 31; +user3 : update_age user2 32; + +/* All tables exist independently */ +/* user1: {name: "Alice", age: 30} */ +/* user2: {name: "Alice", age: 31} */ +/* user3: {name: "Alice", age: 32} */ +</code></pre> +<h2>When to Use Immutable Tables</h2> +<p><strong>Use immutable tables when:</strong></p> +<ul> +<li>You want to prevent accidental data modification</li> +<li>You're building functional programming patterns</li> +<li>You need to track data changes over time</li> +<li>You want to ensure thread safety (if applicable)</li> +<li>You're working with complex data transformations</li> +</ul> +<p><strong>Don't use immutable tables when:</strong></p> +<ul> +<li>You need to modify data in place for performance reasons</li> +<li>You're working with very large datasets that can't be copied</li> +<li>You need to perform side effects on data structures</li> +</ul> +<h2>Common Patterns</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Pattern 1: Building up data structures */ +base_config : {debug: false, timeout: 30}; + +/* Add development settings */ +dev_config : t.merge base_config { + debug: true, + log_level: "verbose" +}; + +/* Add production settings */ +prod_config : t.merge base_config { + timeout: 60, + cache_enabled: true +}; + +/* Pattern 2: Data transformation pipeline */ +user_data : {name: "Alice", age: 30, scores: {85, 90, 88}}; + +/* Transform user data */ +with_average : t.set user_data "average_score" (reduce @add 0 user_data.scores / 3); +with_grade : t.set with_average "grade" (when with_average.average_score is + when with_average.average_score >= 90 then "A" + when with_average.average_score >= 80 then "B" + _ then "C"); + +/* Pattern 3: State management */ +initial_state : {count: 0, items: {}}; + +/* State transitions */ +increment_state : state -> t.set state "count" (state.count + 1); +add_item_state : state item -> t.set state "items" (t.set state.items item.id item); + +/* Apply transitions */ +state1 : increment_state initial_state; +state2 : add_item_state state1 {id: "item1", name: "First Item"}; +</code></pre> +<h2>Performance Considerations</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Immutability can be expensive for large tables */ +large_table : {/* ... many entries ... */}; + +/* Each operation creates a new copy */ +updated1 : t.set large_table "key" "value"; +updated2 : t.set updated1 "key2" "value2"; +/* This creates multiple copies of the large table */ + +/* Consider batching operations */ +batch_update : table -> t.merge table { + key1: "value1", + key2: "value2", + key3: "value3" +}; +/* Single operation instead of multiple */ +</code></pre> +<h2>Key Takeaways</h2> +<ol> +<li><strong>Complete immutability</strong> - no mutation operations exist</li> +<li><strong>New tables returned</strong> - all operations return new data structures</li> +<li><strong>Original unchanged</strong> - source tables are never modified</li> +<li><strong>Functional patterns</strong> - enables pure functional programming</li> +<li><strong>Composable operations</strong> - operations can be chained safely</li> +<li><strong>@ operator required</strong> - for higher-order functions like <code>map</code>, <code>filter</code>, <code>reduce</code></li> +</ol> +<h2>Why This Matters</h2> +<p>Immutable tables make the language safer and more functional:</p> +<ul> +<li><strong>No side effects</strong> - functions can't accidentally modify data</li> +<li><strong>Predictable behavior</strong> - data never changes unexpectedly</li> +<li><strong>Functional style</strong> - encourages pure functions and composition</li> +<li><strong>Debugging ease</strong> - data state is always predictable</li> +<li><strong>Thread safety</strong> - no shared mutable state issues</li> +</ul> +<p>This feature makes the language feel more like pure functional languages like Haskell! 🚀</p> +</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> \ No newline at end of file |