-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.gr
86 lines (64 loc) · 1.58 KB
/
basic.gr
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
// grain filename
// 1 type declaration
record Person { name: String, mut age: Number }
// 2 tagged union declaration
// u cant create tagget union on the place
// need to declarate all tags in separated types
record LoggedIn {name: String}
enum User {
LoggedIn(LoggedIn),
Guest()
}
// 3 hello world
print("Hello world")
// 4 creating instance of type
let bob = { name: "Bob", age: 42 }
// mutation
let mut b = "sas"
b = "sus"
// 7 create function for type
let haveBirthday = (person: Person) => {
// 6 change instance field
// age marked as mutable
person.age += 1
person
}
// 5 call function
haveBirthday(bob)
// 8 loop
// there are no for loop, use recursive funcs
// or Range type
import Range from "range"
Range.forEach(val => print(val), Range.Exclusive(1, 3))
// 9 if
if (false) {
print("false")
}
enum Topping { Cheese, Pepperoni, Peppers, Pineapple }
enum Menu { Pizza(Topping), Calzone(Topping) }
let item = Calzone(Peppers)
let sas = (x: String) => {
match (item) {
Calzone(Peppers) => print("These are half off this week."),
Pizza(Cheese) => print("We never discount this item."),
_ => print("No current specials.")
}
}
// 10 Switch
let getName = (user: User) => {
match (user) {
// LoggedIn(loggedIn) => print(loggedIn.name),
_ => print("")
}
}
// 11 arrays
let arr = [1, 2]
// A list spread can only appear at the end of a list.
let newArrPrepend = [0, ...arr]
import { map } from "list"
let mapped = map((n) => n + 3, [1, 2, 3])
// 12 map
import Map from "map"
// map gets type after first set
let hMap = Map.make()
Map.set("b", "a", hMap)