about summary refs log tree commit diff stats
path: root/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html
diff options
context:
space:
mode:
Diffstat (limited to 'js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html')
-rw-r--r--js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html260
1 files changed, 260 insertions, 0 deletions
diff --git a/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html
new file mode 100644
index 0000000..2752548
--- /dev/null
+++ b/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html
@@ -0,0 +1,260 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width,initial-scale=1">
+    <title>05_Pattern_Matching - 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">05_Pattern_Matching</h1>
+    
+
+    <section>
+
+<header>
+    
+</header>
+
+<article>
+    <h1><code>when</code> Expressions (Pattern Matching)</h1>
+<h2>What are <code>when</code> Expressions?</h2>
+<p>This is kinda where the whole idea for Baba Yaga started. Pattern matching is an approach to flow control. We do this in Baba Yaga using the <code>when</code> expression. It provides pattern matching functionality, allowing you to match values against patterns and execute different code based on the match.</p>
+<pre class="prettyprint source lang-plaintext"><code>/* Pattern matching with when expressions */
+result : when x is
+  0 then &quot;zero&quot;
+  1 then &quot;one&quot;
+  _ then &quot;other&quot;;
+</code></pre>
+<p>Baba Yaga's pattern matching syntax has a lot of insporations, but especially <code>cond</code> patterns, Gleam's pattern matching, and Roc's, too.</p>
+<h2>Basic Examples</h2>
+<pre class="prettyprint source lang-plaintext"><code>/* Simple pattern matching */
+x : 5;
+result : when x is
+  0 then &quot;zero&quot;
+  1 then &quot;one&quot;
+  2 then &quot;two&quot;
+  _ then &quot;other&quot;;
+/* Result: &quot;other&quot; */
+
+/* Pattern matching with numbers */
+grade : 85;
+letter_grade : when grade is
+  90 then &quot;A&quot;
+  80 then &quot;B&quot;
+  70 then &quot;C&quot;
+  60 then &quot;D&quot;
+  _ then &quot;F&quot;;
+/* Result: &quot;B&quot; */
+</code></pre>
+<h2>Pattern Types</h2>
+<h3>Literal Patterns</h3>
+<pre class="prettyprint source lang-plaintext"><code>/* Match exact values */
+result : when value is
+  true then &quot;yes&quot;
+  false then &quot;no&quot;
+  _ then &quot;maybe&quot;;
+</code></pre>
+<h3>Wildcard Pattern</h3>
+<pre class="prettyprint source lang-plaintext"><code>/* _ matches anything */
+result : when x is
+  0 then &quot;zero&quot;
+  _ then &quot;not zero&quot;;
+</code></pre>
+<h3>Function Reference Patterns</h3>
+<pre class="prettyprint source lang-plaintext"><code>/* Match function references using @ operator */
+double : x -> x * 2;
+square : x -> x * x;
+
+which : x -> when x is
+  @double then &quot;doubling function&quot;
+  @square then &quot;squaring function&quot;
+  _ then &quot;other function&quot;;
+
+test1 : which double;
+test2 : which square;
+</code></pre>
+<p>As is called out elsewhere, too, the <code>@</code> operator is required when matching function references in patterns. This distinguishes between calling a function and matching against the function itself.</p>
+<h3>Boolean Patterns</h3>
+<pre class="prettyprint source lang-plaintext"><code>/* Match boolean values */
+result : when condition is
+  true then &quot;condition is true&quot;
+  false then &quot;condition is false&quot;;
+</code></pre>
+<h2>Complex Examples</h2>
+<pre class="prettyprint source lang-plaintext"><code>/* Grade classification with ranges */
+score : 85;
+grade : when score is
+  when score >= 90 then &quot;A&quot;
+  when score >= 80 then &quot;B&quot;
+  when score >= 70 then &quot;C&quot;
+  when score >= 60 then &quot;D&quot;
+  _ then &quot;F&quot;;
+/* Result: &quot;B&quot; */
+
+/* Multiple conditions */
+x : 5;
+y : 10;
+result : when x is
+  when x = y then &quot;equal&quot;
+  when x > y then &quot;x is greater&quot;
+  when x &lt; y then &quot;x is less&quot;
+  _ then &quot;impossible&quot;;
+/* Result: &quot;x is less&quot; */
+</code></pre>
+<h2>Advanced Pattern Matching</h2>
+<p>You can match multiple values with complex expressions:</p>
+<pre class="prettyprint source lang-plaintext"><code>/* FizzBuzz implementation using multi-value patterns */
+fizzbuzz : n ->
+  when (n % 3) (n % 5) is
+    0 0 then &quot;FizzBuzz&quot;
+    0 _ then &quot;Fizz&quot;
+    _ 0 then &quot;Buzz&quot;
+    _ _ then n;
+
+/* Test the FizzBuzz function */
+result1 : fizzbuzz 15;  /* &quot;FizzBuzz&quot; */
+result2 : fizzbuzz 3;   /* &quot;Fizz&quot; */
+result3 : fizzbuzz 5;   /* &quot;Buzz&quot; */
+result4 : fizzbuzz 7;   /* 7 */
+</code></pre>
+<p>You can access table properties directly in patterns:</p>
+<pre class="prettyprint source lang-plaintext"><code>/* User role checking */
+user : {role: &quot;admin&quot;, level: 5};
+
+access_level : when user.role is
+  &quot;admin&quot; then &quot;full access&quot;
+  &quot;user&quot; then &quot;limited access&quot;
+  _ then &quot;no access&quot;;
+/* Result: &quot;full access&quot; */
+</code></pre>
+<p>You can use function calls in patterns. Be warned, though -- they require parentheses to help disambiguate them from other references, though.</p>
+<pre class="prettyprint source lang-plaintext"><code>/* Even/odd classification */
+is_even : n -> n % 2 = 0;
+
+classify : n ->
+  when (is_even n) is
+    true then &quot;even number&quot;
+    false then &quot;odd number&quot;;
+
+/* Test the classification */
+result1 : classify 4;  /* &quot;even number&quot; */
+result2 : classify 7;  /* &quot;odd number&quot; */
+</code></pre>
+<p>Function calls in patterns must be wrapped in parentheses!</p>
+<p>This'll work:</p>
+<pre class="prettyprint source lang-plaintext"><code>when (is_even n) is true then &quot;even&quot;
+when (complex_func x y) is result then &quot;matched&quot;
+</code></pre>
+<p>This won't work:</p>
+<pre class="prettyprint source lang-plaintext"><code>when is_even n is true then &quot;even&quot;  /* Ambiguous parsing */
+</code></pre>
+<p>You can nest <code>when</code> expressions for complex logic:</p>
+<pre class="prettyprint source lang-plaintext"><code>/* Nested pattern matching */
+x : 5;
+y : 10;
+result : when x is
+  0 then when y is
+    0 then &quot;both zero&quot;
+    _ then &quot;x is zero&quot;
+  1 then when y is
+    1 then &quot;both one&quot;
+    _ then &quot;x is one&quot;
+  _ then when y is
+    0 then &quot;y is zero&quot;
+    1 then &quot;y is one&quot;
+    _ then &quot;neither special&quot;;
+/* Result: &quot;neither special&quot; */
+</code></pre>
+<h2>Using <code>when</code> with Functions</h2>
+<pre class="prettyprint source lang-plaintext"><code>/* Function that uses pattern matching */
+classify_number : x -> when x is
+            0 then &quot;zero&quot;
+  (x % 2 = 0) then &quot;even&quot;
+  (x % 2 = 1) then &quot;odd&quot;
+            _ then &quot;unknown&quot;;
+
+/* Use the function */
+result1 : classify_number 0;   /* &quot;zero&quot; */
+result2 : classify_number 4;   /* &quot;even&quot; */
+result3 : classify_number 7;   /* &quot;odd&quot; */
+</code></pre>
+<h2>Common Patterns</h2>
+<pre class="prettyprint source lang-plaintext"><code>/* Value classification */
+classify_age : age -> when age is
+  (age &lt; 13) then &quot;child&quot;
+  (age &lt; 20) then &quot;teenager&quot;
+  (age &lt; 65) then &quot;adult&quot;
+  _ then &quot;senior&quot;;
+
+/* Error handling */
+safe_divide : x y -> when y is
+  0 then &quot;error: division by zero&quot;
+  _ then x / y;
+
+/* Status mapping */
+status_code : 404;
+status_message : x -> 
+    when x is
+      200 then &quot;OK&quot;
+      404 then &quot;Not Found&quot;
+      500 then &quot;Internal Server Error&quot;
+        _ then &quot;Unknown Error&quot;;
+</code></pre>
+<h2>When to Use <code>when</code> pattern matching</h2>
+<p><strong>Use <code>when</code> expressions when:</strong></p>
+<ul>
+<li>You need to match values against multiple patterns</li>
+<li>You want to replace complex if/else chains</li>
+<li>You're working with enumerated values</li>
+<li>You need to handle different cases based on value types</li>
+<li>You want to make conditional logic more readable</li>
+<li><strong>You need to match multiple values simultaneously</strong> (multi-value patterns)</li>
+<li><strong>You want to access table properties in patterns</strong> (table access)</li>
+<li><strong>You need to use function results in patterns</strong> (function calls with parentheses)</li>
+<li><strong>You're implementing complex validation logic</strong> (multi-field validation)</li>
+<li><strong>You need to match function references</strong> (using <code>@</code> operator)</li>
+</ul>
+<p><strong>Don't use <code>when</code> expressions when:</strong></p>
+<ul>
+<li>You only have a simple true/false condition (use logical operators)</li>
+<li>You're working with complex nested conditions (consider breaking into functions)</li>
+</ul>
+</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