diff options
Diffstat (limited to 'js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-09_Expression_Based.html')
-rw-r--r-- | js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-09_Expression_Based.html | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-09_Expression_Based.html b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-09_Expression_Based.html new file mode 100644 index 0000000..0495cb0 --- /dev/null +++ b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-09_Expression_Based.html @@ -0,0 +1,221 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <title>09_Expression_Based - 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">09_Expression_Based</h1> + + + <section> + +<header> + +</header> + +<article> + <h1>No Explicit Return Statements</h1> +<h2>What are Implicit Returns?</h2> +<p>Functions automatically return the last evaluated expression without needing an explicit <code>return</code> statement.</p> +<pre class="prettyprint source lang-plaintext"><code>/* Functions return the last expression automatically */ +add : x y -> x + y; /* Automatically returns x + y */ +double : x -> x * 2; /* Automatically returns x * 2 */ +</code></pre> +<h2>Why is This Esoteric?</h2> +<p>Most programming languages require explicit <code>return</code> statements. Our language makes them <strong>implicit</strong> - the last expression is automatically returned.</p> +<h2>Basic Examples</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Simple functions with implicit returns */ +add : x y -> x + y; +result : add 5 3; /* 8 */ + +/* Single expression functions */ +double : x -> x * 2; +increment : x -> x + 1; +square : x -> x * x; + +/* All automatically return their last expression */ +result1 : double 5; /* 10 */ +result2 : increment 5; /* 6 */ +result3 : square 5; /* 25 */ +</code></pre> +<h2>Complex Functions</h2> +<p>Even complex functions with multiple expressions return the last one:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Function with multiple expressions */ +complex_function : x -> + doubled : x * 2; + incremented : doubled + 1; + squared : incremented * incremented; + squared; /* This is what gets returned */ + +result : complex_function 3; +/* Step 1: doubled = 3 * 2 = 6 */ +/* Step 2: incremented = 6 + 1 = 7 */ +/* Step 3: squared = 7 * 7 = 49 */ +/* Result: 49 */ +</code></pre> +<h2>Conditional Returns</h2> +<p>Functions with conditional logic return the last expression in the executed branch:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Function with conditional logic */ +classify_number : x -> + when x is + 0 then "zero" + when x % 2 = 0 then "even" + when x % 2 = 1 then "odd" + _ then "unknown"; + +/* Each branch returns its last expression */ +result1 : classify_number 0; /* "zero" */ +result2 : classify_number 4; /* "even" */ +result3 : classify_number 7; /* "odd" */ +</code></pre> +<h2>Nested Functions</h2> +<p>Nested functions also use implicit returns:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Nested function definitions */ +outer_function : x -> + inner_function : y -> y * 2; + inner_function x; + +/* The nested function returns its last expression */ +result : outer_function 5; /* 10 */ +</code></pre> +<h2>Table Operations</h2> +<p>Table operations return the last expression:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Function that creates and modifies tables */ +create_user_profile : name age -> + base_profile : {name: name, age: age}; + with_id : t.set base_profile "id" "user_123"; + with_timestamp : t.set with_id "created" "2024-01-01"; + with_timestamp; /* Returns the final table */ + +result : create_user_profile "Alice" 30; +/* Result: {name: "Alice", age: 30, id: "user_123", created: "2024-01-01"} */ +</code></pre> +<h2>Function Composition</h2> +<p>Implicit returns work seamlessly with function composition:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Functions that return functions */ +create_multiplier : factor -> + multiplier : x -> x * factor; + multiplier; /* Returns the multiplier function */ + +/* Use the returned function */ +double : create_multiplier 2; +triple : create_multiplier 3; + +result1 : double 5; /* 10 */ +result2 : triple 5; /* 15 */ +</code></pre> +<h2>Common Patterns</h2> +<h3>Data Transformation</h3> +<pre class="prettyprint source lang-plaintext"><code>/* Transform data with implicit returns */ +transform_user_data : user -> + with_full_name : t.set user "full_name" (user.first_name + " " + user.last_name); + with_age_group : t.set with_full_name "age_group" ( + when user.age < 18 then "minor" + when user.age < 65 then "adult" + _ then "senior" + ); + with_age_group; /* Returns the transformed user */ + +user : {first_name: "Alice", last_name: "Smith", age: 30}; +result : transform_user_data user; +/* Result: {first_name: "Alice", last_name: "Smith", age: 30, full_name: "Alice Smith", age_group: "adult"} */ +</code></pre> +<h3>Validation Functions</h3> +<pre class="prettyprint source lang-plaintext"><code>/* Validation with implicit returns */ +validate_user : user -> + name_valid : user.name != ""; + age_valid : user.age > 0 && user.age < 120; + email_valid : user.email.contains "@"; + name_valid && age_valid && email_valid; /* Returns boolean */ + +user : {name: "Alice", age: 30, email: "alice@example.com"}; +is_valid : validate_user user; /* true */ +</code></pre> +<h3>Configuration Builders</h3> +<pre class="prettyprint source lang-plaintext"><code>/* Build configuration with implicit returns */ +build_config : base_config environment -> + dev_config : when environment is + "development" then t.merge base_config {debug: true, log_level: "verbose"} + "production" then t.merge base_config {debug: false, log_level: "error"} + _ then base_config; + dev_config; /* Returns the final config */ + +base : {timeout: 30, retries: 3}; +result : build_config base "development"; +/* Result: {timeout: 30, retries: 3, debug: true, log_level: "verbose"} */ +</code></pre> +<h2>When to Use Implicit Returns</h2> +<p><strong>Implicit returns work well when:</strong></p> +<ul> +<li>Functions have a single, clear purpose</li> +<li>The return value is obvious from the function name</li> +<li>Functions are pure (no side effects)</li> +<li>Functions are used in composition chains</li> +<li>The logic is straightforward</li> +</ul> +<p><strong>Consider explicit structure when:</strong></p> +<ul> +<li>Functions have complex conditional logic</li> +<li>Multiple return paths are confusing</li> +<li>Functions perform side effects</li> +<li>The return value is not obvious</li> +</ul> +<h2>Key Takeaways</h2> +<ol> +<li><strong>Last expression returned</strong> - the last evaluated expression is automatically returned</li> +<li><strong>No return keyword</strong> - no explicit <code>return</code> statements needed</li> +<li><strong>Conditional returns</strong> - the last expression in the executed branch is returned</li> +<li><strong>Nested functions</strong> - nested functions also use implicit returns</li> +<li><strong>Composition friendly</strong> - works seamlessly with function composition</li> +</ol> +<h2>Why This Matters</h2> +<p>Implicit returns make the language more functional and concise:</p> +<ul> +<li><strong>Concise syntax</strong> - less boilerplate code</li> +<li><strong>Functional style</strong> - emphasizes expressions over statements</li> +<li><strong>Composition focus</strong> - functions are treated as expressions</li> +<li><strong>Mathematical thinking</strong> - functions are mathematical mappings</li> +<li><strong>Readability</strong> - clear flow from input to output</li> +</ul> +<p>This feature makes the language feel more like mathematical functions than traditional programming procedures! 🚀</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 |