diff options
Diffstat (limited to 'js/scripting-lang/tutorials/04_Partial_Application_by_Default.md')
-rw-r--r-- | js/scripting-lang/tutorials/04_Partial_Application_by_Default.md | 233 |
1 files changed, 0 insertions, 233 deletions
diff --git a/js/scripting-lang/tutorials/04_Partial_Application_by_Default.md b/js/scripting-lang/tutorials/04_Partial_Application_by_Default.md deleted file mode 100644 index 80ec0d0..0000000 --- a/js/scripting-lang/tutorials/04_Partial_Application_by_Default.md +++ /dev/null @@ -1,233 +0,0 @@ -# Partial Application by Default (Currying) - -## What is Partial Application? - -Partial application means that functions automatically return new functions when called with fewer arguments than they expect. This is also called **currying**. - -```plaintext -/* Functions automatically return new functions when partially applied */ -add : x y -> x + y; -add_five : add 5; /* Returns a function that adds 5 */ -result : add_five 3; /* 8 */ -``` - -## Why is This Esoteric? - -Most programming languages require explicit syntax for partial application or currying. In our language, **every function is automatically curried** - no special syntax needed. - -## Basic Examples - -```plaintext -/* Define a two-argument function */ -add : x y -> x + y; - -/* Call with both arguments */ -result1 : add 5 3; /* 8 */ - -/* Call with one argument - returns a new function */ -add_five : add 5; /* Returns: y -> 5 + y */ - -/* Call the returned function */ -result2 : add_five 3; /* 8 */ - -/* Chain partial applications */ -add_ten : add 10; /* y -> 10 + y */ -add_ten_five : add_ten 5; /* 15 */ -``` - -## How It Works - -The language automatically handles partial application through nested function returns: - -```plaintext -/* When you define: add : x y -> x + y; */ -/* The language creates: add = x -> (y -> x + y) */ - -/* When you call: add 5 */ -/* It returns: y -> 5 + y */ - -/* When you call: add 5 3 */ -/* It calls: (y -> 5 + y)(3) = 5 + 3 = 8 */ -``` - -## Multi-Argument Functions - -Partial application works with any number of arguments: - -```plaintext -/* Three-argument function */ -multiply_add : x y z -> x * y + z; - -/* Partial application examples */ -multiply_by_two : multiply_add 2; /* y z -> 2 * y + z */ -multiply_by_two_add_ten : multiply_add 2 5; /* z -> 2 * 5 + z */ - -/* Full application */ -result1 : multiply_add 2 5 3; /* 2 * 5 + 3 = 13 */ -result2 : multiply_by_two 5 3; /* 2 * 5 + 3 = 13 */ -result3 : multiply_by_two_add_ten 3; /* 2 * 5 + 3 = 13 */ -``` - -## Standard Library Functions - -All standard library functions support partial application: - -```plaintext -/* Arithmetic functions */ -double : multiply 2; /* x -> 2 * x */ -increment : add 1; /* x -> x + 1 */ -decrement : subtract 1; /* x -> x - 1 */ - -/* Comparison functions */ -is_positive : greaterThan 0; /* x -> x > 0 */ -is_even : equals 0; /* This won't work as expected - see below */ - -/* Logical functions */ -always_true : logicalOr true; /* x -> true || x */ -always_false : logicalAnd false; /* x -> false && x */ -``` - -## Common Patterns - -```plaintext -/* Pattern 1: Creating specialized functions */ -numbers : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - -/* Create specialized filters */ -is_even : x -> x % 2 = 0; -is_odd : x -> x % 2 = 1; -is_greater_than_five : greaterThan 5; - -/* Use with map and filter */ -evens : filter is_even numbers; /* {2, 4, 6, 8, 10} */ -odds : filter is_odd numbers; /* {1, 3, 5, 7, 9} */ -large_numbers : filter is_greater_than_five numbers; /* {6, 7, 8, 9, 10} */ - -/* Pattern 2: Creating transformation functions */ -double : multiply 2; -triple : multiply 3; -add_ten : add 10; - -/* Apply transformations */ -doubled : map double numbers; /* {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} */ -tripled : map triple numbers; /* {3, 6, 9, 12, 15, 18, 21, 24, 27, 30} */ -plus_ten : map add_ten numbers; /* {11, 12, 13, 14, 15, 16, 17, 18, 19, 20} */ -``` - -## Function Composition with Partial Application - -Partial application works seamlessly with function composition: - -```plaintext -/* Create specialized functions */ -double : multiply 2; -increment : add 1; -square : x -> x * x; - -/* Compose partially applied functions */ -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 */ -``` - -## Table Operations with Partial Application - -The `t.` namespace functions also support partial application: - -```plaintext -/* Create specialized table operations */ -get_name : t.get "name"; -get_age : t.get "age"; -has_admin : t.has "admin"; - -/* Use with map */ -people : { - alice: {name: "Alice", age: 30, admin: true}, - bob: {name: "Bob", age: 25, admin: false}, - charlie: {name: "Charlie", age: 35, admin: true} -}; - -names : map get_name people; /* {alice: "Alice", bob: "Bob", charlie: "Charlie"} */ -ages : map get_age people; /* {alice: 30, bob: 25, charlie: 35} */ -admins : map has_admin people; /* {alice: true, bob: false, charlie: true} */ -``` - -## The `each` Combinator with Partial Application - -The `each` combinator works well with partial application: - -```plaintext -/* Create specialized comparison functions */ -is_greater_than_three : greaterThan 3; -is_less_than_seven : lessThan 7; - -/* Use with each for element-wise comparison */ -numbers : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - -greater_than_three : each is_greater_than_three numbers; -/* Result: {false, false, false, true, true, true, true, true, true, true} */ - -less_than_seven : each is_less_than_seven numbers; -/* Result: {true, true, true, true, true, true, false, false, false, false} */ -``` - -## When to Use Partial Application - -**Use partial application when:** -- Creating specialized functions from general ones -- Building function composition chains -- Working with combinators like `map`, `filter`, `reduce` -- Creating reusable function components -- Simplifying complex function calls - -**Don't use partial application when:** -- You need to call functions with all arguments immediately -- You're working with simple, single-purpose functions -- You need to modify the function behavior significantly - -## Common Patterns - -```plaintext -/* Pattern 1: Function factories */ -create_multiplier : factor -> multiply factor; -double : create_multiplier 2; -triple : create_multiplier 3; -quadruple : create_multiplier 4; - -/* Pattern 2: Specialized validators */ -create_range_validator : min max -> x -> x >= min && x <= max; -is_valid_age : create_range_validator 0 120; -is_valid_score : create_range_validator 0 100; - -/* Pattern 3: Configuration functions */ -create_formatter : prefix suffix -> x -> prefix + x + suffix; -format_name : create_formatter "Name: " "!"; -format_age : create_formatter "Age: " " years"; - -/* Usage */ -result1 : format_name "Alice"; /* "Name: Alice!" */ -result2 : format_age "30"; /* "Age: 30 years" */ -``` - -## Key Takeaways - -1. **Automatic currying** - every function is automatically curried -2. **No special syntax** - just call with fewer arguments -3. **Nested functions** - partial application creates nested function calls -4. **Composable** - works seamlessly with function composition -5. **Reusable** - create specialized functions from general ones - -## Why This Matters - -Partial application by default makes the language more functional and composable: - -- **Function factories** - create specialized functions easily -- **Composition focus** - encourages building complex functions from simple ones -- **Reusability** - general functions can be specialized for specific use cases -- **Mathematical thinking** - functions are treated as mathematical objects -- **Concise syntax** - no explicit currying syntax needed - -This feature makes the language feel more like mathematical function theory than traditional programming! 🚀 \ No newline at end of file |