diff options
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.html | 260 |
1 files changed, 0 insertions, 260 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 deleted file mode 100644 index 2752548..0000000 --- a/js/scripting-lang/docs/baba-yaga/0.0.1/tutorial-05_Pattern_Matching.html +++ /dev/null @@ -1,260 +0,0 @@ -<!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 "zero" - 1 then "one" - _ then "other"; -</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 "zero" - 1 then "one" - 2 then "two" - _ then "other"; -/* Result: "other" */ - -/* Pattern matching with numbers */ -grade : 85; -letter_grade : when grade is - 90 then "A" - 80 then "B" - 70 then "C" - 60 then "D" - _ then "F"; -/* Result: "B" */ -</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 "yes" - false then "no" - _ then "maybe"; -</code></pre> -<h3>Wildcard Pattern</h3> -<pre class="prettyprint source lang-plaintext"><code>/* _ matches anything */ -result : when x is - 0 then "zero" - _ then "not zero"; -</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 "doubling function" - @square then "squaring function" - _ then "other function"; - -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 "condition is true" - false then "condition is false"; -</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 "A" - when score >= 80 then "B" - when score >= 70 then "C" - when score >= 60 then "D" - _ then "F"; -/* Result: "B" */ - -/* Multiple conditions */ -x : 5; -y : 10; -result : when x is - when x = y then "equal" - when x > y then "x is greater" - when x < y then "x is less" - _ then "impossible"; -/* Result: "x is less" */ -</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 "FizzBuzz" - 0 _ then "Fizz" - _ 0 then "Buzz" - _ _ then n; - -/* Test the FizzBuzz function */ -result1 : fizzbuzz 15; /* "FizzBuzz" */ -result2 : fizzbuzz 3; /* "Fizz" */ -result3 : fizzbuzz 5; /* "Buzz" */ -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: "admin", level: 5}; - -access_level : when user.role is - "admin" then "full access" - "user" then "limited access" - _ then "no access"; -/* Result: "full access" */ -</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 "even number" - false then "odd number"; - -/* Test the classification */ -result1 : classify 4; /* "even number" */ -result2 : classify 7; /* "odd number" */ -</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 "even" -when (complex_func x y) is result then "matched" -</code></pre> -<p>This won't work:</p> -<pre class="prettyprint source lang-plaintext"><code>when is_even n is true then "even" /* 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 "both zero" - _ then "x is zero" - 1 then when y is - 1 then "both one" - _ then "x is one" - _ then when y is - 0 then "y is zero" - 1 then "y is one" - _ then "neither special"; -/* Result: "neither special" */ -</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 "zero" - (x % 2 = 0) then "even" - (x % 2 = 1) then "odd" - _ then "unknown"; - -/* Use the function */ -result1 : classify_number 0; /* "zero" */ -result2 : classify_number 4; /* "even" */ -result3 : classify_number 7; /* "odd" */ -</code></pre> -<h2>Common Patterns</h2> -<pre class="prettyprint source lang-plaintext"><code>/* Value classification */ -classify_age : age -> when age is - (age < 13) then "child" - (age < 20) then "teenager" - (age < 65) then "adult" - _ then "senior"; - -/* Error handling */ -safe_divide : x y -> when y is - 0 then "error: division by zero" - _ then x / y; - -/* Status mapping */ -status_code : 404; -status_message : x -> - when x is - 200 then "OK" - 404 then "Not Found" - 500 then "Internal Server Error" - _ then "Unknown Error"; -</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 |