diff options
Diffstat (limited to 'js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md')
-rw-r--r-- | js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md | 188 |
1 files changed, 0 insertions, 188 deletions
diff --git a/js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md b/js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md deleted file mode 100644 index 651cf91..0000000 --- a/js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md +++ /dev/null @@ -1,188 +0,0 @@ -# Juxtaposition-Based Function Application - -## What is Juxtaposition? - -Juxtaposition means "placing side by side" - in our language, this means you can call functions by simply placing the function name next to its arguments, **without parentheses**. - -```plaintext -/* Traditional syntax: f(x, y) */ -/* Our syntax: f x y */ -``` - -## Why is This Esoteric? - -Most programming languages require parentheses for function calls. Our language eliminates them entirely, making function application look like mathematical notation. - -## Basic Examples - -```plaintext -/* Simple function calls */ -add 5 3; /* Instead of add(5, 3) */ -multiply 4 7; /* Instead of multiply(4, 7) */ -subtract 10 3; /* Instead of subtract(10, 3) */ - -/* Function calls with tables */ -map double {1, 2, 3, 4, 5}; -filter is_even {1, 2, 3, 4, 5, 6}; -reduce add 0 {1, 2, 3, 4, 5}; -``` - -## How It Works - -The parser automatically translates juxtaposition into nested `apply` calls: - -```plaintext -/* f x y becomes: apply(apply(f, x), y) */ -/* map double {1, 2, 3} becomes: apply(apply(map, double), {1, 2, 3}) */ -``` - -## Precedence Rules - -Juxtaposition has **lower precedence** than operators: - -```plaintext -/* This works as expected */ -result : add 5 multiply 3 4; -/* Parsed as: add 5 (multiply 3 4) */ -/* Result: 5 + (3 * 4) = 17 */ - -/* Not as: (add 5 multiply) 3 4 */ -``` - -## Complex Examples - -```plaintext -/* Nested function calls */ -result : map double filter is_even {1, 2, 3, 4, 5, 6}; -/* Parsed as: map double (filter is_even {1, 2, 3, 4, 5, 6}) */ -/* Result: {4, 8, 12} */ - -/* Function composition with juxtaposition */ -double : x -> x * 2; -increment : x -> x + 1; -result : compose double increment 5; -/* Parsed as: (compose double increment) 5 */ -/* Result: double(increment(5)) = double(6) = 12 */ -``` - -## When to Use Juxtaposition - -**Use juxtaposition when:** -- Calling functions with arguments -- Building function composition chains -- Working with combinators like `map`, `filter`, `reduce` - -**Don't use juxtaposition when:** -- Defining functions (use `:` and `->`) -- Assigning values (use `:`) -- Using operators (use `+`, `-`, `*`, etc.) - -## Common Patterns - -```plaintext -/* Data processing pipeline */ -data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; -is_even : x -> x % 2 = 0; -double : x -> x * 2; -sum : x -> reduce add 0 x; - -/* Pipeline using juxtaposition */ -result : sum map double filter is_even data; -/* Reads: sum (map double (filter is_even data)) */ -/* Result: 60 */ -``` - -## Using Parentheses for Control - -While juxtaposition eliminates the need for parentheses in most cases, parentheses are still available when you need explicit control over precedence or grouping. - -### When to Use Parentheses - -**Use parentheses when:** -- **Controlling precedence** - when the default left-associative parsing doesn't give you what you want -- **Grouping expressions** - to make complex expressions more readable -- **Breaking ambiguity** - when the parser might misinterpret your intent -- **Debugging** - to isolate and test specific parts of complex expressions - -### Precedence Control Examples - -```plaintext -/* Without parentheses - left-associative */ -result1 : add 5 multiply 3 4; -/* Parsed as: add 5 (multiply 3 4) */ -/* Result: 5 + (3 * 4) = 17 */ - -/* With parentheses - explicit grouping */ -result2 : add (add 1 2) (multiply 3 4); -/* Explicitly: (1 + 2) + (3 * 4) = 3 + 12 = 15 */ - -/* Complex nested operations */ -result3 : map double (filter is_even (map increment {1, 2, 3, 4, 5})); -/* Step by step: - 1. map increment {1, 2, 3, 4, 5} → {2, 3, 4, 5, 6} - 2. filter is_even {2, 3, 4, 5, 6} → {2, 4, 6} - 3. map double {2, 4, 6} → {4, 8, 12} -*/ -``` - -### Readability and Clarity - -```plaintext -/* Hard to read without parentheses */ -complex : map double filter is_even map increment {1, 2, 3, 4, 5}; - -/* Much clearer with parentheses */ -complex : map double (filter is_even (map increment {1, 2, 3, 4, 5})); - -/* Or break it into steps for maximum clarity */ -step1 : map increment {1, 2, 3, 4, 5}; -step2 : filter is_even step1; -step3 : map double step2; -``` - -### Debugging with Parentheses - -```plaintext -/* When debugging, use parentheses to isolate parts */ -data : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - -/* Test each step separately */ -filtered : filter is_even data; -doubled : map double filtered; -final : reduce add 0 doubled; - -/* Or use parentheses to test intermediate results */ -test1 : filter is_even data; /* {2, 4, 6, 8, 10} */ -test2 : map double (filter is_even data); /* {4, 8, 12, 16, 20} */ -``` - -## Debugging Juxtaposition - -If you get unexpected results, check the precedence: - -```plaintext -/* Wrong: This doesn't work as expected */ -result : map double filter is_even {1, 2, 3, 4, 5}; - -/* Right: Use parentheses to control precedence */ -result : map double (filter is_even {1, 2, 3, 4, 5}); -``` - -## Key Takeaways - -1. **No parentheses needed** for function calls -2. **Left-associative** - `f x y` means `(f x) y` -3. **Lower precedence** than operators -4. **Mathematical notation** - looks like `f(x, y)` but written as `f x y` -5. **Nested automatically** - complex calls become nested `apply` calls - -## Why This Matters - -Juxtaposition makes the language feel more mathematical and less like traditional programming. It enables: - -- **Concise syntax** - less punctuation -- **Natural reading** - `map double numbers` reads like "map double over numbers" -- **Functional style** - emphasizes function application over method calls -- **Composition focus** - makes function composition the primary operation - -This is one of the most distinctive features of our language - it completely changes how you think about function calls! 🚀 \ No newline at end of file |