about summary refs log tree commit diff stats
path: root/awk/rawk/scratch/tests_old/real_world/demo.rawk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/scratch/tests_old/real_world/demo.rawk')
-rw-r--r--awk/rawk/scratch/tests_old/real_world/demo.rawk277
1 files changed, 277 insertions, 0 deletions
diff --git a/awk/rawk/scratch/tests_old/real_world/demo.rawk b/awk/rawk/scratch/tests_old/real_world/demo.rawk
new file mode 100644
index 0000000..14d2fa0
--- /dev/null
+++ b/awk/rawk/scratch/tests_old/real_world/demo.rawk
@@ -0,0 +1,277 @@
+# =============================================================================
+# rawk Demo: Fantasy Kingdom Data Processing
+# =============================================================================
+# This demo showcases most rawk features using whimsical fantasy-themed data
+# simulating a kingdom's census, magical artifacts, and adventurer logs
+
+# =============================================================================
+# FUNCTION DEFINITIONS
+# =============================================================================
+
+# Basic utility functions
+$is_magical = (item) -> index(item, "magic") > 0 || index(item, "spell") > 0 || index(item, "wand") > 0;
+$is_rare = (rarity) -> rarity == "legendary" || rarity == "epic";
+$is_hero = (level) -> level >= 10;
+$is_apprentice = (level) -> level < 5;
+$add = (x, y) -> x + y;
+$double = (x) -> x * 2;
+
+# Data processing functions
+$parse_adventurer = (line, result) -> {
+    split(line, result, "|")
+    return length(result)
+};
+
+$calculate_power = (level, magic_items) -> level * 2 + magic_items * 5;
+$format_title = (name, title) -> title " " name;
+$extract_magic_count = (inventory, result) -> {
+    split(inventory, result, ",")
+    magic_count = 0
+    for (i = 1; i <= length(result); i++) {
+        if (is_magical(result[i])) magic_count++
+    }
+    return magic_count
+};
+
+# Complex data transformation
+$process_kingdom_data = (data, result) -> {
+    # Split into lines and process each
+    split(data, lines, "\n")
+    processed_count = 0
+    
+    for (i = 1; i <= length(lines); i++) {
+        if (lines[i] != "") {
+            split(lines[i], fields, ",")
+            if (length(fields) >= 4) {
+                processed_count++
+                result[processed_count] = "Processed: " fields[1] " (" fields[2] ")"
+            }
+        }
+    }
+    return processed_count
+};
+
+# =============================================================================
+# MAIN PROCESSING
+# =============================================================================
+
+BEGIN {
+    print "🏰 Fantasy Kingdom Data Processing Demo"
+    print "======================================"
+    print ""
+    
+    # =============================================================================
+    # 1. BASIC FUNCTIONALITY & PREDICATES
+    # =============================================================================
+    print "1. Basic Functionality & Predicates"
+    print "-----------------------------------"
+    
+    # Test basic predicates
+    expect_true(is_number(42), "42 should be a number")
+    expect_true(is_string("magic"), "magic should be a string")
+    expect_true(is_email("wizard@tower.com"), "wizard@tower.com should be valid email")
+    expect_true(is_url("https://kingdom.gov"), "https://kingdom.gov should be valid URL")
+    expect_true(is_positive(15), "15 should be positive")
+    expect_true(is_even(8), "8 should be even")
+    expect_true(is_prime(7), "7 should be prime")
+    expect_true(is_palindrome("racecar"), "racecar should be palindrome")
+    expect_true(is_uuid("123e4567-e89b-12d3-a456-426614174000"), "should be valid UUID")
+    expect_true(is_hex("FF00AA"), "FF00AA should be hex")
+    print "✓ All basic predicates working"
+    print ""
+    
+    # =============================================================================
+    # 2. ARRAY UTILITIES
+    # =============================================================================
+    print "2. Array Utilities"
+    print "------------------"
+    
+    # Create test data
+    citizens[1] = "Gandalf|Wizard|15|legendary"
+    citizens[2] = "Frodo|Hobbit|3|common"
+    citizens[3] = "Aragorn|Ranger|12|epic"
+    citizens[4] = "Gimli|Dwarf|8|rare"
+    citizens[5] = "Legolas|Elf|11|epic"
+    
+    # Test array utilities
+    citizen_count = keys(citizens)
+    expect_equal(citizen_count, 5, "Should have 5 citizens")
+    
+    # Get keys and values
+    get_keys(citizens, citizen_keys)
+    get_values(citizens, citizen_values)
+    expect_equal(length(citizen_keys), 5, "Should have 5 keys")
+    expect_equal(length(citizen_values), 5, "Should have 5 values")
+    print "✓ Array utilities working"
+    print ""
+    
+    # =============================================================================
+    # 3. FUNCTIONAL PROGRAMMING
+    # =============================================================================
+    print "3. Functional Programming"
+    print "------------------------"
+    
+    # Test map function
+    parsed_count = map("parse_adventurer", citizens, parsed_citizens)
+    expect_equal(parsed_count, 5, "Should parse 5 citizens")
+    print "✓ Map function working"
+    
+    # Test reduce with custom function
+    levels[1] = 15; levels[2] = 3; levels[3] = 12; levels[4] = 8; levels[5] = 11
+    total_level = reduce("add", levels)
+    expect_equal(total_level, 49, "Total levels should be 49")
+    print "✓ Reduce function working"
+    
+    # Test pipe function
+    doubled = pipe(7, "double")
+    expect_equal(doubled, 14, "7 doubled should be 14")
+    print "✓ Pipe function working"
+    print ""
+    
+    # =============================================================================
+    # 4. ENHANCED ARRAY UTILITIES
+    # =============================================================================
+    print "4. Enhanced Array Utilities"
+    print "---------------------------"
+    
+    # Test filter function
+    hero_count = filter("is_hero", levels, heroes)
+    expect_equal(hero_count, 3, "Should have 3 heroes (level >= 10)")
+    print "✓ Filter function working"
+    
+    # Test find function
+    first_hero = find("is_hero", levels)
+    expect_true(first_hero >= 10, "First hero should be level 10+")
+    print "✓ Find function working"
+    
+    # Test findIndex function
+    hero_index = findIndex("is_hero", levels)
+    expect_true(hero_index > 0, "Should find hero index")
+    print "✓ FindIndex function working"
+    
+    # Test take and drop functions
+    first_three_count = take(3, levels, first_three)
+    expect_equal(first_three_count, 3, "Should take 3 levels")
+    
+    remaining_count = drop(2, levels, remaining)
+    expect_equal(remaining_count, 3, "Should have 3 remaining levels")
+    print "✓ Take and drop functions working"
+    print ""
+    
+    # =============================================================================
+    # 5. ADVANCED ARRAY TRANSFORMATION
+    # =============================================================================
+    print "5. Advanced Array Transformation"
+    print "--------------------------------"
+    
+    # Test flatMap with inventory processing
+    inventories[1] = "sword,shield,magic wand"
+    inventories[2] = "bow,arrows"
+    inventories[3] = "axe,magic ring,spell book"
+    
+    magic_items_count = flatMap("extract_magic_count", inventories, all_magic_items)
+    expect_equal(magic_items_count, 3, "Should have 3 magic items total")
+    print "✓ FlatMap function working"
+    print ""
+    
+    # =============================================================================
+    # 6. REAL-WORLD DATA PROCESSING
+    # =============================================================================
+    print "6. Real-World Data Processing"
+    print "-----------------------------"
+    
+    # Simulate CSV-like data processing
+    kingdom_data = "Gandalf,Wizard,15,legendary\nFrodo,Hobbit,3,common\nAragorn,Ranger,12,epic"
+    
+    processed_count = process_kingdom_data(kingdom_data, processed_data)
+    expect_equal(processed_count, 3, "Should process 3 kingdom records")
+    print "✓ CSV-like data processing working"
+    
+    # Test complex functional composition
+    # Filter heroes -> map power calculation -> take top 2
+    hero_levels[1] = 15; hero_levels[2] = 12; hero_levels[3] = 11; hero_levels[4] = 8
+    hero_count = filter("is_hero", hero_levels, heroes_only)
+    expect_equal(hero_count, 3, "Should have 3 heroes")
+    
+    # Calculate power for each hero (level * 2)
+    $calculate_hero_power = (level) -> level * 2;
+    powered_count = map("calculate_hero_power", heroes_only, hero_powers)
+    expect_equal(powered_count, 3, "Should calculate power for 3 heroes")
+    
+    # Take top 2 most powerful
+    top_two_count = take(2, hero_powers, top_two)
+    expect_equal(top_two_count, 2, "Should take top 2 heroes")
+    print "✓ Complex functional composition working"
+    print ""
+    
+    # =============================================================================
+    # 7. ERROR HANDLING & EDGE CASES
+    # =============================================================================
+    print "7. Error Handling & Edge Cases"
+    print "------------------------------"
+    
+    # Test with empty arrays
+    empty_filter_count = filter("is_positive", empty_array, empty_result)
+    expect_equal(empty_filter_count, 0, "Empty array should return 0")
+    
+    empty_take_count = take(5, empty_array, empty_take_result)
+    expect_equal(empty_take_count, 0, "Take from empty should return 0")
+    
+    empty_drop_count = drop(3, empty_array, empty_drop_result)
+    expect_equal(empty_drop_count, 0, "Drop from empty should return 0")
+    print "✓ Edge cases handled correctly"
+    print ""
+    
+    # =============================================================================
+    # 8. INTEGRATION TESTING
+    # =============================================================================
+    print "8. Integration Testing"
+    print "----------------------"
+    
+    # Complex pipeline: filter -> map -> filter -> take
+    adventurers[1] = 15; adventurers[2] = 3; adventurers[3] = 12; adventurers[4] = 8; adventurers[5] = 11
+    
+    # Step 1: Filter heroes
+    heroes_count = filter("is_hero", adventurers, heroes_list)
+    
+    # Step 2: Double their levels
+    doubled_count = map("double", heroes_list, doubled_heroes)
+    
+    # Step 3: Filter those with doubled level > 20
+    $is_very_powerful = (level) -> level > 20;
+    powerful_count = filter("is_very_powerful", doubled_heroes, powerful_heroes)
+    
+    # Step 4: Take the most powerful
+    final_count = take(1, powerful_heroes, final_hero)
+    
+    expect_true(final_count > 0, "Should have at least one very powerful hero")
+    print "✓ Complex integration pipeline working"
+    print ""
+    
+    # =============================================================================
+    # SUMMARY
+    # =============================================================================
+    print "🎉 Demo Summary"
+    print "==============="
+    print "✓ Basic functionality and predicates"
+    print "✓ Array utilities (keys, values, get_keys, get_values)"
+    print "✓ Functional programming (map, reduce, pipe)"
+    print "✓ Enhanced utilities (filter, find, findIndex)"
+    print "✓ Advanced transformation (flatMap, take, drop)"
+    print "✓ Real-world data processing (CSV-like, complex composition)"
+    print "✓ Error handling and edge cases"
+    print "✓ Integration testing with complex pipelines"
+    print ""
+    print "🏰 All rawk features working correctly!"
+    print "The kingdom's data processing system is fully operational."
+    print ""
+    print "Features demonstrated:"
+    print "- 20+ predicate functions (is_number, is_email, is_uuid, etc.)"
+    print "- Array utilities and manipulation"
+    print "- Functional programming (map, reduce, pipe)"
+    print "- Enhanced array utilities (filter, find, findIndex)"
+    print "- Advanced transformation (flatMap, take, drop)"
+    print "- Complex data processing pipelines"
+    print "- Error handling and edge cases"
+    print "- Integration testing"
+} 
\ No newline at end of file