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 | 265 |
1 files changed, 0 insertions, 265 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 3030653..0000000 --- a/js/scripting-lang/tutorials/01_Juxtaposition_Function_Application.md +++ /dev/null @@ -1,265 +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} */ -``` - -### Negative Numbers and Spacing - -**Important**: The language now supports unary minus without parentheses, but proper spacing is crucial for clarity. - -```plaintext -/* ✅ CORRECT - Unary minus without parentheses */ -result1 : -5 + 3; /* negate(5) + 3 */ -result2 : -5 >= 0; /* negate(5) >= 0 */ -result3 : f -5; /* f(negate(5)) */ - -/* ✅ CORRECT - Binary minus with spaces */ -result4 : 5 - 3; /* subtract(5, 3) */ -result5 : 10 - 5; /* subtract(10, 5) */ - -/* ✅ CORRECT - Legacy syntax still works */ -result6 : (-5) + 3; /* negate(5) + 3 (explicit grouping) */ -result7 : f (-5); /* f(negate(5)) (explicit grouping) */ - -/* ⚠️ LEGACY - No spaces around binary operators (not recommended) */ -result8 : 5-3; /* subtract(5, 3) (legacy fallback) */ -``` - -#### Spacing Rules - -The language uses spacing to distinguish between unary and binary operators: - -- **Unary minus**: `-5` (no leading space) → `negate(5)` -- **Binary minus**: `5 - 3` (spaces required) → `subtract(5, 3)` -- **Legacy fallback**: `5-3` → `subtract(5, 3)` (but spaces are recommended) - -#### Why This Matters - -The parser distinguishes between: -- **Unary minus** (negative numbers): `-5` → `negate(5)` -- **Binary minus** (subtraction): `5 - 3` → `subtract(5, 3)` - -Proper spacing makes expressions unambiguous and follows functional language conventions. - -#### Common Patterns - -```plaintext -/* Function calls with negative numbers */ -double : x -> x * 2; -result : double -5; /* ✅ Correct - unary minus */ -result2 : double (-5); /* ✅ Correct - explicit grouping */ - -/* Comparisons with negative numbers */ -is_negative : x -> x < 0; -test1 : is_negative -5; /* ✅ Correct - unary minus */ - -/* Complex expressions with negative numbers */ -validate_age : age -> (age >= 0) and (age <= 120); -test2 : validate_age -5; /* ✅ Correct - unary minus */ - -/* Arithmetic with proper spacing */ -result3 : -5 + 3; /* ✅ Correct - unary minus + binary plus */ -result4 : 5 - 3; /* ✅ Correct - binary minus with spaces */ -result5 : (-5) + 3; /* ✅ Correct - explicit grouping */ -``` - -#### Best Practices - -- **Use spaces around binary operators**: `5 - 3`, `5 + 3`, `5 * 3` -- **Unary minus works without parentheses**: `-5`, `f -5` -- **Legacy syntax still works**: `(-5)`, `5-3` (but spaces are recommended) -- **When in doubt, use spaces**: It makes code more readable and follows conventions - -#### When You Might Encounter This - -- **Arithmetic operations**: `-5 + 3`, `5 - 3`, `(-5) + 3` -- **Comparisons**: `-5 >= 0`, `5 - 3 >= 0` -- **Function calls**: `f -5`, `f (-5)`, `map double -3` -- **Logical expressions**: `(-5 >= 0) and (-5 <= 120)` -- **Pattern matching**: `when x is -5 then "negative five"` - -**Remember**: Use spaces around binary operators for clarity and consistency! - -## 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 |