-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.coal
98 lines (73 loc) · 2.1 KB
/
test.coal
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Defining functions
def replaceInString:(String str) word:(String) with:(String) -> Bool
def writeMessage:(String msg) -> Void
def actuallyPrint:(String it) -> Void
[print: it]
end
[actuallyPrint: msg]
end
[writeMessage: [str stringAfterReplacing: word with: with]]
return true
end
let result: Any?
result = [replaceInString: "Hello, world!" word: "world" with: "Coal"]
[print: ["The call returned: " concat: result]]
// Conditionals
let test: Any?
// test = true
test = 42
// test = 43
if test == true do
[print: "It's the truth!"]
elif test == 42 do
[print: "It's the meaning of life!"]
elif test > 42 do
[print: "It's more than the meaning of life!"]
else
[print: "It's a lie!"]
end
// Iterable objects
let fruits: List = ("Apple", "Banana")
let more_fruits: List = (fruits, "Cocoa", "Melon", "Peach")
let bla_bla: List = (0, 1, more_fruits)
[print: fruits]
[print: fruits{1}]
[print: more_fruits]
[print: more_fruits{0}]
[print: more_fruits{0}{1}]
let a_list: List = (0, 1, 2, 3, 4, 5)
[print: a_list]
a_list{2} = "Two"
[print: a_list]
let other_list: List = (6, 7, 8, 9)
[a_list update: other_list]
[print: a_list]
[print: a_list{2,-1}]
// Loops
let colors: List = ("red", "yellow", "brown", "green", "yellow")
let fruits: List = ("apple", "banana", "coconut", "melon", "peach")
let items: List?
for 0, [fruits length] - 1 -> i
items = (colors{i}, fruits{i})
if i == 0 do
[print: ["I have a {} {}, " format: items] sep: ""]
elif i < [fruits length] - 1 do
[print: ["a {} {}, " format: items] sep: ""]
else
[print: ["and I have a {} {}, too." format: items]]
end
end
// Instead of using
// for 0, [fruits length] - 1 -> i
// You can feed a range to "each" using
// each [fruits iterate] -> i
each [fruits iterate] -> i
items = (colors{i}, fruits{i})
if i == 0 do
[print: ["I have a {} {}, " format: items] sep: ""]
elif i < [fruits length] - 1 do
[print: ["a {} {}, " format: items] sep: ""]
else
[print: ["and I have a {} {}, too." format: items]]
end
end