diff options
Diffstat (limited to 'js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-07_Function_References.html')
-rw-r--r-- | js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-07_Function_References.html | 239 |
1 files changed, 239 insertions, 0 deletions
diff --git a/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-07_Function_References.html b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-07_Function_References.html new file mode 100644 index 0000000..88951fe --- /dev/null +++ b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-07_Function_References.html @@ -0,0 +1,239 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <title>07_Function_References - 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">07_Function_References</h1> + + + <section> + +<header> + +</header> + +<article> + <h1>Function References with <code>@</code> Symbol</h1> +<h2>What are Function References?</h2> +<p>Function references allow you to pass functions as values without calling them immediately. The <code>@</code> symbol creates a reference to a function.</p> +<pre class="prettyprint source lang-plaintext"><code>/* @ symbol for function references */ +double : x -> x * 2; +numbers : {1, 2, 3}; +result : map @double numbers; /* @double is a function reference */ +</code></pre> +<h2>Why is This Esoteric?</h2> +<p>The <code>@</code> symbol for function references is unique to our language. Most languages use just the function name or different syntax like <code>&function</code> or <code>function.bind()</code>.</p> +<h2>Basic Examples</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Define a function */ +double : x -> x * 2; + +/* Function reference vs function call */ +function_ref : @double; /* Reference to the function */ +function_call : double 5; /* Call the function with argument 5 */ + +/* Use function reference with combinators */ +numbers : {1, 2, 3, 4, 5}; +doubled : map @double numbers; /* {2, 4, 6, 8, 10} */ +</code></pre> +<h2>How It Works</h2> +<p>The <code>@</code> symbol tells the language to treat the identifier as a function reference rather than calling the function:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Without @ - function is called immediately */ +result1 : map double numbers; /* Error: double is not a function reference */ + +/* With @ - function reference is passed */ +result2 : map @double numbers; /* Works: @double is a function reference */ +</code></pre> +<h2>Common Use Cases</h2> +<h3>With <code>map</code></h3> +<pre class="prettyprint source lang-plaintext"><code>/* Map function references over collections */ +numbers : {1, 2, 3, 4, 5}; + +/* Arithmetic functions */ +doubled : map @double numbers; /* {2, 4, 6, 8, 10} */ +incremented : map @increment numbers; /* {2, 3, 4, 5, 6} */ +squared : map @square numbers; /* {1, 4, 9, 16, 25} */ + +/* Custom functions */ +add_ten : x -> x + 10; +plus_ten : map @add_ten numbers; /* {11, 12, 13, 14, 15} */ +</code></pre> +<h3>With <code>filter</code></h3> +<pre class="prettyprint source lang-plaintext"><code>/* Filter with function references */ +numbers : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + +/* Built-in comparison functions */ +evens : filter @is_even numbers; /* {2, 4, 6, 8, 10} */ +positives : filter @is_positive numbers; /* {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} */ + +/* Custom filter functions */ +is_greater_than_five : x -> x > 5; +large_numbers : filter @is_greater_than_five numbers; /* {6, 7, 8, 9, 10} */ +</code></pre> +<h3>With <code>reduce</code></h3> +<pre class="prettyprint source lang-plaintext"><code>/* Reduce with function references */ +numbers : {1, 2, 3, 4, 5}; + +/* Arithmetic operations */ +sum : reduce @add 0 numbers; /* 15 */ +product : reduce @multiply 1 numbers; /* 120 */ + +/* Custom reduce functions */ +max_value : reduce @max 0 numbers; /* 5 */ +min_value : reduce @min 1000 numbers; /* 1 */ +</code></pre> +<h3>With <code>each</code></h3> +<pre class="prettyprint source lang-plaintext"><code>/* Each with function references */ +numbers1 : {1, 2, 3, 4, 5}; +numbers2 : {10, 20, 30, 40, 50}; + +/* Element-wise operations */ +sums : each @add numbers1 numbers2; /* {11, 22, 33, 44, 55} */ +products : each @multiply numbers1 numbers2; /* {10, 40, 90, 160, 250} */ +</code></pre> +<h2>Function Composition with References</h2> +<p>Function references work seamlessly with composition:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Compose function references */ +double : x -> x * 2; +increment : x -> x + 1; +square : x -> x * x; + +/* Compose references */ +double_then_increment : compose @increment @double; +increment_then_square : compose @square @increment; + +/* Use in pipelines */ +result1 : double_then_increment 5; /* double(5)=10, increment(10)=11 */ +result2 : increment_then_square 5; /* increment(5)=6, square(6)=36 */ +</code></pre> +<h2>The <code>via</code> Operator with References</h2> +<p>Function references work with the <code>via</code> operator:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Via with function references */ +result1 : @double via @increment 5; /* 12 */ +result2 : @double via @increment via @square 3; /* 20 */ + +/* Complex pipeline */ +pipeline : @sum via @map @double via @filter @is_even; +result3 : pipeline {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; /* 60 */ +</code></pre> +<h2>Table Operations with References</h2> +<p>The <code>t.</code> namespace functions can be referenced:</p> +<pre class="prettyprint source lang-plaintext"><code>/* Table operation references */ +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} */ + +/* Check if key exists */ +has_a : @t.has data "a"; /* true */ +</code></pre> +<h2>When to Use Function References</h2> +<p><strong>Use function references when:</strong></p> +<ul> +<li>Passing functions to combinators like <code>map</code>, <code>filter</code>, <code>reduce</code></li> +<li>Building function composition chains</li> +<li>Creating reusable function components</li> +<li>Working with higher-order functions</li> +<li>Avoiding immediate function execution</li> +</ul> +<p><strong>Don't use function references when:</strong></p> +<ul> +<li>You want to call the function immediately</li> +<li>You're working with simple function calls</li> +<li>You need to pass arguments to the function</li> +</ul> +<h2>Common Patterns</h2> +<pre class="prettyprint source lang-plaintext"><code>/* Pattern 1: Function pipelines */ +numbers : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + +/* Pipeline with references */ +pipeline : @sum via @map @double via @filter @is_even; +result : pipeline numbers; /* 60 */ + +/* Pattern 2: Reusable function components */ +double : x -> x * 2; +increment : x -> x + 1; +square : x -> x * x; + +/* Create reusable transformations */ +double_transform : @map @double; +increment_transform : @map @increment; +square_transform : @map @square; + +/* Use transformations */ +data : {1, 2, 3, 4, 5}; +doubled : double_transform data; /* {2, 4, 6, 8, 10} */ +incremented : increment_transform data; /* {2, 3, 4, 5, 6} */ +squared : square_transform data; /* {1, 4, 9, 16, 25} */ + +/* Pattern 3: Conditional function application */ +condition : true; +function_to_use : when condition is + true then @double + _ then @square; + +result : map function_to_use {1, 2, 3, 4, 5}; +/* Result depends on condition */ +</code></pre> +<h2>Key Takeaways</h2> +<ol> +<li><strong>@ symbol</strong> - creates function references</li> +<li><strong>No immediate execution</strong> - function is not called when referenced</li> +<li><strong>Combinator compatibility</strong> - works with <code>map</code>, <code>filter</code>, <code>reduce</code>, <code>each</code></li> +<li><strong>Composition support</strong> - works with <code>compose</code>, <code>pipe</code>, <code>via</code></li> +<li><strong>Higher-order functions</strong> - enables passing functions as arguments</li> +</ol> +<h2>Why This Matters</h2> +<p>Function references with the <code>@</code> symbol make the language more functional:</p> +<ul> +<li><strong>Higher-order functions</strong> - functions can be passed as values</li> +<li><strong>Composability</strong> - functions can be combined and reused</li> +<li><strong>Functional style</strong> - emphasizes function composition over method calls</li> +<li><strong>Clear syntax</strong> - distinguishes between function calls and references</li> +<li><strong>Reusability</strong> - function references can be stored and reused</li> +</ul> +<p>This feature makes the language feel more like functional programming languages where functions are first-class citizens! 🚀</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 |