diff options
Diffstat (limited to 'js/scripting-lang/tutorials/10_Tables_Deep_Dive.md')
-rw-r--r-- | js/scripting-lang/tutorials/10_Tables_Deep_Dive.md | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/js/scripting-lang/tutorials/10_Tables_Deep_Dive.md b/js/scripting-lang/tutorials/10_Tables_Deep_Dive.md new file mode 100644 index 0000000..9d66d1b --- /dev/null +++ b/js/scripting-lang/tutorials/10_Tables_Deep_Dive.md @@ -0,0 +1,271 @@ +# Table Literals as Primary Data Structure + +## What are Table Literals? + +Tables are the **only** data structure in our language. They serve as objects, arrays, maps, and any other collection type you might need. + +```plaintext +/* Tables serve multiple purposes */ +/* As objects: {name: "Alice", age: 30} */ +/* As arrays: {1, 2, 3, 4, 5} */ +/* As maps: {key1: "value1", key2: "value2"} */ +/* As nested structures: {user: {name: "Alice", scores: {85, 90, 88}}} */ +``` + +## Why is This Esoteric? + +Most languages have separate types for different data structures (arrays, objects, maps, sets, etc.). Our language uses **one unified structure** for everything. + +## Basic Table Syntax + +### Key-Value Pairs (Objects) +```plaintext +/* Create object-like tables */ +person : {name: "Alice", age: 30, city: "New York"}; +user : {id: 123, email: "alice@example.com", active: true}; + +/* Access properties */ +name : person.name; /* "Alice" */ +age : person.age; /* 30 */ +``` + +### Array-Like Tables +```plaintext +/* Create array-like tables */ +numbers : {1, 2, 3, 4, 5}; +names : {"Alice", "Bob", "Charlie"}; +mixed : {1, "hello", true, 3.14}; + +/* Access by index (using bracket notation) */ +first_number : numbers[0]; /* 1 */ +second_name : names[1]; /* "Bob" */ +``` + +### Mixed Tables +```plaintext +/* Tables can mix key-value pairs and array elements */ +mixed_table : { + name: "Alice", + scores: {85, 90, 88}, + metadata: {created: "2024-01-01", version: 1.0} +}; +``` + +## Table Operations + +### Creating Tables +```plaintext +/* Empty table */ +empty : {}; + +/* Single element */ +single : {42}; + +/* Key-value pairs */ +config : {debug: true, timeout: 30, retries: 3}; + +/* Mixed content */ +complex : { + id: 123, + tags: {"important", "urgent"}, + settings: {theme: "dark", notifications: true} +}; +``` + +### Accessing Values +```plaintext +/* Dot notation for keys */ +data : {name: "Alice", age: 30}; +name : data.name; /* "Alice" */ + +/* Bracket notation for indices or dynamic keys */ +numbers : {1, 2, 3, 4, 5}; +first : numbers[0]; /* 1 */ +second : numbers[1]; /* 2 */ + +/* Dynamic key access */ +key : "name"; +value : data[key]; /* "Alice" */ +``` + +### Nested Tables +```plaintext +/* Deeply nested structures */ +user_profile : { + personal: { + name: "Alice", + age: 30, + contact: { + email: "alice@example.com", + phone: "555-1234" + } + }, + preferences: { + theme: "dark", + notifications: true, + languages: {"English", "Spanish"} + } +}; + +/* Access nested values */ +email : user_profile.personal.contact.email; /* "alice@example.com" */ +theme : user_profile.preferences.theme; /* "dark" */ +first_language : user_profile.preferences.languages[0]; /* "English" */ +``` + +## Table-Specific Operations + +The `t.` namespace provides table-specific operations: + +```plaintext +/* Table operations */ +data : {a: 1, b: 2, c: 3}; + +/* Get keys */ +keys : t.keys data; /* {"a", "b", "c"} */ + +/* Get values */ +values : t.values data; /* {1, 2, 3} */ + +/* Get key-value pairs */ +pairs : t.pairs data; /* {{key: "a", value: 1}, {key: "b", value: 2}, {key: "c", value: 3}} */ + +/* Check if key exists */ +has_a : t.has data "a"; /* true */ +has_d : t.has data "d"; /* false */ + +/* Get value by key */ +value_a : t.get data "a"; /* 1 */ + +/* Get table length */ +length : t.length data; /* 3 */ +``` + +## Element-Wise Operations + +Tables work seamlessly with element-wise operations: + +```plaintext +/* Map over table values - @ operator required for higher-order functions */ +numbers : {a: 1, b: 2, c: 3, d: 4, e: 5}; +double : x -> x * 2; +doubled : map @double numbers; /* {a: 2, b: 4, c: 6, d: 8, e: 10} */ + +/* Filter table values - @ operator required for higher-order functions */ +is_even : x -> x % 2 = 0; +evens : filter @is_even numbers; /* {b: 2, d: 4} */ + +/* Reduce table values - @ operator required for higher-order functions */ +sum : reduce @add 0 numbers; /* 15 */ +``` + +## Common Patterns + +### Configuration Objects +```plaintext +/* Build configuration objects */ +base_config : { + timeout: 30, + retries: 3, + debug: false +}; + +/* Environment-specific overrides */ +dev_config : t.merge base_config { + debug: true, + log_level: "verbose" +}; + +prod_config : t.merge base_config { + timeout: 60, + cache_enabled: true +}; +``` + +### Data Transformation +```plaintext +/* Transform data structures */ +raw_data : { + users: { + alice: {name: "Alice", age: 30, scores: {85, 90, 88}}, + bob: {name: "Bob", age: 25, scores: {92, 87, 95}} + } +}; + +/* Extract and transform user data */ +transform_user : user -> { + name: user.name, + age: user.age, + average_score: reduce @add 0 user.scores / 3 +}; + +transformed_users : map @transform_user raw_data.users; +/* Result: { + alice: {name: "Alice", age: 30, average_score: 87.67}, + bob: {name: "Bob", age: 25, average_score: 91.33} +} */ +``` + +### Nested Data Processing +```plaintext +/* Process nested table structures */ +company_data : { + departments: { + engineering: { + employees: { + alice: {name: "Alice", role: "Developer", salary: 80000}, + bob: {name: "Bob", role: "Manager", salary: 100000} + } + }, + marketing: { + employees: { + charlie: {name: "Charlie", role: "Designer", salary: 70000} + } + } + } +}; + +/* Extract all employee names - @ operator required for higher-order functions */ +get_names : dept -> map @(emp -> emp.name) dept.employees; +all_names : map @get_names company_data.departments; +/* Result: { + engineering: {"Alice", "Bob"}, + marketing: {"Charlie"} +} */ +``` + +## When to Use Tables + +**Use tables when you need:** +- **Objects** - key-value pairs for structured data +- **Arrays** - ordered collections of values +- **Maps** - dynamic key-value mappings +- **Nested structures** - complex hierarchical data +- **Mixed data** - combinations of different data types + +**Tables are perfect for:** +- Configuration objects +- User data and profiles +- API responses and requests +- Data transformation pipelines +- Complex nested structures + +## Key Takeaways + +1. **Unified structure** - one data type for all collections +2. **Flexible syntax** - supports both key-value pairs and array elements +3. **Nested support** - can contain other tables +4. **Element-wise operations** - works with `map`, `filter`, `reduce` (using `@` operator) +5. **Immutable operations** - all operations return new tables + +## Why This Matters + +Table literals as the primary data structure make the language simpler and more unified: + +- **Simplicity** - only one data structure to learn +- **Flexibility** - can represent any collection type +- **Consistency** - same operations work on all data +- **Composability** - tables can be nested and combined +- **Functional style** - immutable operations on all data + +This feature makes the language feel more like mathematical sets and relations than traditional programming data structures! 🚀 \ No newline at end of file |