-
Notifications
You must be signed in to change notification settings - Fork 1
/
hello-2.mars
77 lines (66 loc) · 1.44 KB
/
hello-2.mars
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
namespace hello {
// defines PI's symbol into the hello namespace
expose let PI: Float = 3.14f
expose fn add (a: Int, b: Int): Int {
a + b
}
expose fn main () {
Io.print("Hello World")
}
// Function overload
expose fn fib {
(number: Int): Int {
fib(number, 1, 0)
}
(number: Int, prev: Int, current: Int): Int {
if (number <= 0) {
current
} else {
fib(number - 1, prev + current, prev)
}
}
}
expose type Optional<T> implements Equals {
// Type constructors
Empty
Of(T value)
fn == (otherRef: Ref): Bool {
otherRef
.extract(Optional<T>)
.map((other-value) { value == other-value })
.or-else(true)
}
fn empty? (): Bool {
this.map<Bool>((_) { false }).or-else(true)
}
fn map<S> (mapper: (T) {S}): Optional<S> {
match this {
(Optional.Empty) {
Optional.Empty()
}
(Of value) {
Optional.Of(mapper(value))
}
}
}
fn or-else (default: T): Optional<S> {
match this {
(Optional.Empty) {
detault
}
(Of value) {
value
}
}
}
}
expose class Person {
public name: String
public age: Int
public address: Optional<String> = Optional.Empty()
birthday(): Person {
this.with(age=this.age+1)
}
}
expose let maniero = Person(name="Maniero", age=28)
}