about summary refs log tree commit diff stats
path: root/js/baba-yaga/example.baba
blob: 353882b44ab79f3c8be3a0a35d1d5e644d8efc67 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Example Baba Yaga Program
// Demonstrates core language features

// Variables and basic operations
x : 42;
y : x + 8;
message : "Hello, Baba Yaga!";

// Functions and currying
add : a b -> a + b;
multiply : a b -> a * b;
addTen : add 10;

// Higher-order functions with lists
numbers : [1, 2, 3, 4, 5];
doubled : map (x -> x * 2) numbers;
evens : filter (x -> x % 2 = 0) doubled;
sum : reduce (acc x -> acc + x) 0 evens;

// Pattern matching
classify : n ->
  when n is
    0 then "zero"
    1 then "one"
    Int then when (n > 10) is true then "big" _ then "small"
    _ then "unknown";

// Error handling with Result types
safeDivide : a b ->
  when b is
    0 then Err "Division by zero"
    _ then Ok (a / b);

// Table (object) operations
person : { name: "Alice", age: 30, city: "New York" };

// Output results
io.out "=== Baba Yaga Language Demo ===";
io.out "";

io.out "Basic values:";
io.out x;
io.out y;
io.out message;

io.out "";
io.out "Function results:";
io.out (addTen 5);
io.out (multiply 6 7);

io.out "";
io.out "List operations:";
io.out doubled;
io.out evens;
io.out sum;

io.out "";
io.out "Pattern matching:";
io.out (classify 0);
io.out (classify 15);
io.out (classify 3);

io.out "";
io.out "Error handling:";
result1 : safeDivide 10 2;
result2 : safeDivide 10 0;
io.out result1;
io.out result2;

io.out "";
io.out "Table operations:";
io.out person;

io.out "";
io.out "Demo complete!";