~repos /plum
git clone https://pyrossh.dev/repos/plum.git
A statically typed, imperative programming language inspired by rust, python
fc566dda
—
pyrossh 1 year ago
updates
- readme.md +0 -17
- scratch.pc +12 -36
- std/list.mi +32 -32
- std/str.mi +21 -21
- website/src/content/docs/declarations/enum.md +14 -2
- website/src/content/docs/declarations/functions.md +6 -6
- website/src/content/docs/declarations/records.md +11 -90
- website/src/content/docs/declarations/traits.md +6 -1
- website/src/content/docs/naming-convention.md +19 -0
readme.md
CHANGED
|
@@ -5,20 +5,3 @@
|
|
|
5
5
|
- Plans to be compiled to WASM
|
|
6
6
|
|
|
7
7
|
View the docs here [pacos.sh](https://pacos.sh)
|
|
8
|
-
|
|
9
|
-
## Rules
|
|
10
|
-
|
|
11
|
-
- Function parameters are passed by value only. You cannot modify a parameter. The compiler will throw an error if you try to.
|
|
12
|
-
- Strict naming convention
|
|
13
|
-
- Only one way of doing things ex: loops, condition
|
|
14
|
-
|
|
15
|
-
### General naming convention
|
|
16
|
-
|
|
17
|
-
| Item | Convention |
|
|
18
|
-
| ------------------------ | ----------------------- |
|
|
19
|
-
| Modules | snake_case |
|
|
20
|
-
| Types/Traits/Enum | UpperCamelCase |
|
|
21
|
-
| Fields/Functions/Methods | lowerCamelCase |
|
|
22
|
-
| Local variables | snake_case |
|
|
23
|
-
| Constants | SCREAMING_SNAKE_CASE |
|
|
24
|
-
| Generics | single uppercase letter |
|
scratch.pc
CHANGED
|
@@ -1,56 +1,31 @@
|
|
|
1
1
|
data.price?.take_if(|a| a != "")
|
|
2
2
|
|
|
3
|
-
trait HasEq[A](
|
|
4
|
-
fn eq(that: a): bool
|
|
5
|
-
)
|
|
6
|
-
|
|
7
|
-
trait Equatable[A]
|
|
8
|
-
`A trait that defines equal and not equal operations
|
|
9
|
-
|
|
10
|
-
fn eq(b: Equatable[A]): bool
|
|
11
|
-
fn ne(b: Equatable[A]): bool
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
enum Compare
|
|
15
|
-
| Less
|
|
16
|
-
| Equal
|
|
17
|
-
| Greater
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
trait Comparable[A] is Equatable[A]
|
|
21
|
-
fn lt(that: A): bool
|
|
22
|
-
fn le(that: A): bool
|
|
23
|
-
fn ge(that: A): bool
|
|
24
|
-
fn gt(that: A): bool
|
|
25
|
-
fn compare(that: a): Compare
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
enum
|
|
3
|
+
enum List<T> =
|
|
29
4
|
| empty
|
|
30
5
|
| link(v: T, rest: list<T>)
|
|
31
6
|
|
|
32
|
-
fn
|
|
7
|
+
fn List.of[A](values: ...A): list[A] =
|
|
33
8
|
list[A]().add(values)
|
|
34
9
|
|
|
35
|
-
fn (
|
|
10
|
+
fn (List) each(cb: fn(v: T)) =
|
|
36
|
-
match
|
|
11
|
+
match self
|
|
37
12
|
empty -> return
|
|
38
13
|
link(a, rest) ->
|
|
39
14
|
cb(a)
|
|
40
15
|
rest.each(cb)
|
|
41
16
|
|
|
42
|
-
fn (
|
|
17
|
+
fn (List) append(values: ...V) =
|
|
43
18
|
`adds the specified elements to the start of the list
|
|
44
19
|
range(values) |v|
|
|
45
|
-
|
|
20
|
+
self.add(v)
|
|
46
21
|
|
|
47
|
-
fn (
|
|
22
|
+
fn (List) append(v: V) =
|
|
48
23
|
`adds the specified elements to the start of the list
|
|
49
|
-
|
|
24
|
+
self.last().rest = link(v, empty)
|
|
50
25
|
|
|
51
|
-
fn (
|
|
26
|
+
fn (List) prepend(v: V) =
|
|
52
27
|
`adds the specified elements to the start of the list
|
|
53
|
-
|
|
28
|
+
self.first() = link(v, self.first())
|
|
54
29
|
|
|
55
30
|
|
|
56
31
|
(1 + 2).mod(3).pow(2).sqrt()
|
|
@@ -82,7 +57,8 @@ fn UserData.fromJsonStr(data: str): UserData | JsonParseError =
|
|
|
82
57
|
|
|
83
58
|
type FetchDataError = FetchError | IOError | JsonParseError
|
|
84
59
|
|
|
60
|
+
#effects(IO)
|
|
85
|
-
fn fetch_data(route: str):
|
|
61
|
+
fn fetch_data(route: str): Result[UserData] =
|
|
86
62
|
res := fetch(route)?
|
|
87
63
|
data := res.body.read_all()?
|
|
88
64
|
parse_json(data)?
|
std/list.mi
CHANGED
|
@@ -18,7 +18,7 @@ record node[V](
|
|
|
18
18
|
fn listOf[A](values: ...A): list[A] =
|
|
19
19
|
list[A]().add(values)
|
|
20
20
|
|
|
21
|
-
fn (
|
|
21
|
+
fn (List) get(i: int) -> A =
|
|
22
22
|
`gets the element at i'th index of the list
|
|
23
23
|
current := l.head
|
|
24
24
|
index := 0
|
|
@@ -29,36 +29,36 @@ fn (l: list) get(i: int) -> A =
|
|
|
29
29
|
current = current.next
|
|
30
30
|
index += 1
|
|
31
31
|
|
|
32
|
-
fn (
|
|
32
|
+
fn (List) set(i: int, v: V) -> A =
|
|
33
33
|
`sets the element at i'th index of the list
|
|
34
34
|
pass
|
|
35
35
|
|
|
36
|
-
fn (
|
|
36
|
+
fn (List) length() -> A =
|
|
37
37
|
`returns the no of elements in the list
|
|
38
38
|
_size
|
|
39
39
|
|
|
40
|
-
fn (
|
|
40
|
+
fn (List) add(values: ...V) =
|
|
41
41
|
`adds the specified elements to the start of the list
|
|
42
42
|
for v := range values
|
|
43
43
|
l.head = node(value: v, prev: nil, next: l.head)
|
|
44
44
|
l.head.next.prev = l.head
|
|
45
45
|
l._size += 1
|
|
46
46
|
|
|
47
|
-
fn (
|
|
47
|
+
fn (List) removeAt(i int) =
|
|
48
48
|
`removes the element at i'th index of the list
|
|
49
49
|
l.tail?.prev?.next = nil
|
|
50
50
|
`old tail node gets deallocated due to zero reference count
|
|
51
51
|
l.tail = list.tail?.prev
|
|
52
52
|
l._size -= 1
|
|
53
53
|
|
|
54
|
-
fn (
|
|
54
|
+
fn (List) remove(v V) =
|
|
55
55
|
`removes the element v from list
|
|
56
56
|
l.tail?.prev?.next = nil
|
|
57
57
|
`old tail node gets deallocated due to zero reference count
|
|
58
58
|
l.tail = list.tail?.prev
|
|
59
59
|
l._size -= 1
|
|
60
60
|
|
|
61
|
-
fn (
|
|
61
|
+
fn (List) clear() =
|
|
62
62
|
`removes all objects from this list
|
|
63
63
|
l.tail?.prev?.next = nil
|
|
64
64
|
`old tail node gets deallocated due to zero reference count
|
|
@@ -66,19 +66,19 @@ fn (l: list) clear() =
|
|
|
66
66
|
l._size -= 1
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
fn (
|
|
69
|
+
fn (List) reverse(v: fn(v: V): true): list[A] =
|
|
70
70
|
`returns a new list with the elements in reverse order.
|
|
71
71
|
pass
|
|
72
72
|
|
|
73
|
-
fn (
|
|
73
|
+
fn (List) sort(sorter: fn(v: V): true): list[A] =
|
|
74
74
|
`returns a new list with the elements sorted by sorter
|
|
75
75
|
pass
|
|
76
76
|
|
|
77
|
-
fn (
|
|
77
|
+
fn (List) find(search: V): V?, int =
|
|
78
78
|
`returns an item and index in the list if the item is is equal to search item
|
|
79
79
|
pass
|
|
80
80
|
|
|
81
|
-
fn (
|
|
81
|
+
fn (List) contains(v: V): bool =
|
|
82
82
|
`returns the index of an item in the list if present and comparable otherwise nil
|
|
83
83
|
pass
|
|
84
84
|
|
|
@@ -91,18 +91,18 @@ fn (l: list[T]) op_range(yld: fn(v: T): bool): bool =
|
|
|
91
91
|
current = current.next
|
|
92
92
|
true
|
|
93
93
|
|
|
94
|
-
fn (
|
|
94
|
+
fn (List) op_spread(other: list): list[A] =
|
|
95
95
|
`combines this list with other list and returns a new list
|
|
96
96
|
pass
|
|
97
97
|
|
|
98
|
-
fn (
|
|
98
|
+
fn (List) each(cb: fn(v: V)): void =
|
|
99
99
|
`calls f for each elem in the list
|
|
100
100
|
current := l.head
|
|
101
101
|
while current != nil
|
|
102
102
|
cb(current.value)
|
|
103
103
|
current = current.next
|
|
104
104
|
|
|
105
|
-
fn (
|
|
105
|
+
fn (List) map[B](cb: fn(v: V): B): list[A] =
|
|
106
106
|
`returns a list made up of B elements for each elem in the list
|
|
107
107
|
nl := []
|
|
108
108
|
current := l.head
|
|
@@ -111,74 +111,74 @@ fn (l: list) map[B](cb: fn(v: V): B): list[A] =
|
|
|
111
111
|
nl.push(item)
|
|
112
112
|
nl
|
|
113
113
|
|
|
114
|
-
fn (
|
|
114
|
+
fn (List) flatMap() =
|
|
115
115
|
`returns a new list with all elements shuffled`
|
|
116
116
|
pass
|
|
117
117
|
|
|
118
|
-
fn (
|
|
118
|
+
fn (List) retain(predicate: fn(v: V): A): list[A] =
|
|
119
119
|
`returns a new list with the elements that matched the predicate
|
|
120
120
|
pass
|
|
121
121
|
|
|
122
|
-
fn (
|
|
122
|
+
fn (List) reject(predicate: fn(v: V): A): list[A] =
|
|
123
123
|
`returns a new list with the elements that matched the predicate removed
|
|
124
124
|
pass
|
|
125
125
|
|
|
126
|
-
fn (
|
|
126
|
+
fn (List) any(predicate: fn(v: V): bool): bool =
|
|
127
127
|
`returns true if any element in the list satisfies the predicate
|
|
128
128
|
pass
|
|
129
129
|
|
|
130
|
-
fn (
|
|
130
|
+
fn (List) every(predicate: fn(v: V): bool): bool =
|
|
131
131
|
`returns true if all of the elements in the list satisfies the predicate
|
|
132
132
|
pass
|
|
133
133
|
|
|
134
|
-
fn (
|
|
134
|
+
fn (List) reduce[B](acc: B, cb: fn(v: V): A): B =
|
|
135
135
|
`returns the accumulated value of all the elements in the list
|
|
136
136
|
pass
|
|
137
137
|
|
|
138
|
-
fn (
|
|
138
|
+
fn (List) first(): A? =
|
|
139
139
|
`returns the first element in the list
|
|
140
140
|
l.head?.value
|
|
141
141
|
|
|
142
|
-
fn (
|
|
142
|
+
fn (List) last(): A? =
|
|
143
143
|
`returns the last element in the list
|
|
144
144
|
l.tail?.value
|
|
145
145
|
|
|
146
|
-
fn (
|
|
146
|
+
fn (List) sublist(start: int, end: int): list[A] =
|
|
147
147
|
`returns a list containing the first n elements of the given list
|
|
148
148
|
pass
|
|
149
149
|
|
|
150
|
-
fn (
|
|
150
|
+
fn (List) take(n: int): list[A] =
|
|
151
151
|
`returns a list containing the first n elements of the given list
|
|
152
152
|
pass
|
|
153
153
|
|
|
154
|
-
fn (
|
|
154
|
+
fn (List) skip(n: int): list[A] =
|
|
155
155
|
`returns a list containing the first n elements of the given list
|
|
156
156
|
pass
|
|
157
157
|
|
|
158
|
-
fn (
|
|
158
|
+
fn (List) drop(n: int): list[A] =
|
|
159
159
|
`Returns a list containing the first n elements of the given list
|
|
160
160
|
|
|
161
|
-
fn (
|
|
161
|
+
fn (List) sample() =
|
|
162
162
|
`returns a new list with some of the elements taken randomly`
|
|
163
163
|
pass
|
|
164
164
|
|
|
165
|
-
fn (
|
|
165
|
+
fn (List) shuffle() =
|
|
166
166
|
`returns a new list with all elements shuffled`
|
|
167
167
|
pass
|
|
168
168
|
|
|
169
|
-
fn (
|
|
169
|
+
fn (List) partition() =
|
|
170
170
|
`returns a new list with all elements shuffled`
|
|
171
171
|
pass
|
|
172
172
|
|
|
173
|
-
fn (
|
|
173
|
+
fn (List) chunk() =
|
|
174
174
|
`returns a new list with all elements shuffled`
|
|
175
175
|
pass
|
|
176
176
|
|
|
177
|
-
fn (
|
|
177
|
+
fn (List) groupBy() =
|
|
178
178
|
`returns a new list with all elements grouped`
|
|
179
179
|
pass
|
|
180
180
|
|
|
181
|
-
fn (
|
|
181
|
+
fn (List) join(sep: str = ","): str =
|
|
182
182
|
res := Buffer()
|
|
183
183
|
l.each() |v|
|
|
184
184
|
if @HasTrait(V, ToStr)
|
std/str.mi
CHANGED
|
@@ -15,19 +15,19 @@ record str(
|
|
|
15
15
|
fn str.of(bs ...byte): str =
|
|
16
16
|
data.add(bs)
|
|
17
17
|
|
|
18
|
-
fn
|
|
18
|
+
fn<str> get(i: int) =
|
|
19
19
|
data[i]
|
|
20
20
|
|
|
21
|
-
fn (
|
|
21
|
+
fn (str) contains(search: str): bool =
|
|
22
22
|
pass
|
|
23
23
|
|
|
24
|
-
fn (
|
|
24
|
+
fn (str) index_of(sub: str): int =
|
|
25
25
|
pass
|
|
26
26
|
|
|
27
|
-
fn (
|
|
27
|
+
fn (str) test(pattern: Regex): []str =
|
|
28
28
|
pass
|
|
29
29
|
|
|
30
|
-
fn (
|
|
30
|
+
fn (str) search(key str): int, bool =
|
|
31
31
|
low, mid, high := 0, 0, n.numItems
|
|
32
32
|
while low < high
|
|
33
33
|
mid = (low + high) / 2
|
|
@@ -51,51 +51,51 @@ fn (t BTree) delete(key str): bool =
|
|
|
51
51
|
return true
|
|
52
52
|
return false
|
|
53
53
|
|
|
54
|
-
fn (
|
|
54
|
+
fn (str) starts_with(search: str): bool =
|
|
55
55
|
pass
|
|
56
56
|
|
|
57
|
-
fn (
|
|
57
|
+
fn (str) concat(other: str): str =
|
|
58
58
|
s + other
|
|
59
59
|
|
|
60
|
-
fn (
|
|
60
|
+
fn (str) to_str(): str = s
|
|
61
61
|
|
|
62
|
-
fn (
|
|
62
|
+
fn (str) concat(other: str): str =
|
|
63
63
|
s + s
|
|
64
64
|
|
|
65
|
-
fn (
|
|
65
|
+
fn (str) starts_with(search: str): bool =
|
|
66
66
|
pass
|
|
67
67
|
|
|
68
|
-
fn (
|
|
68
|
+
fn (str) ends_with(search: str): bool =
|
|
69
69
|
pass
|
|
70
70
|
|
|
71
|
-
fn (
|
|
71
|
+
fn (str) contains(search: str): bool =
|
|
72
72
|
pass
|
|
73
73
|
|
|
74
|
-
fn (
|
|
74
|
+
fn (str) index_of(sub: str): int =
|
|
75
75
|
pass
|
|
76
76
|
|
|
77
|
-
fn (
|
|
77
|
+
fn (str) match(pattern: Regex): []str =
|
|
78
78
|
pass
|
|
79
79
|
|
|
80
|
-
fn (
|
|
80
|
+
fn (str) match_all(pattern: Regex): []str =
|
|
81
81
|
pass
|
|
82
82
|
|
|
83
|
-
fn (
|
|
83
|
+
fn (str) pad_start(sub: str, count: int): str =
|
|
84
84
|
pass
|
|
85
85
|
|
|
86
|
-
fn (
|
|
86
|
+
fn (str) pad_end(sub: str, count: int): str =
|
|
87
87
|
pass
|
|
88
88
|
|
|
89
|
-
fn (
|
|
89
|
+
fn (str) repeat(count: int): str =
|
|
90
90
|
pass
|
|
91
91
|
|
|
92
|
-
fn (
|
|
92
|
+
fn (str) replace(pattern: Regex, sub: str): str =
|
|
93
93
|
pass
|
|
94
94
|
|
|
95
|
-
fn (
|
|
95
|
+
fn (str) replace_all(pattern: Regex, sub: str): str =
|
|
96
96
|
pass
|
|
97
97
|
|
|
98
|
-
fn (
|
|
98
|
+
fn (str) search(pattern: Regex): str =
|
|
99
99
|
pass
|
|
100
100
|
|
|
101
101
|
fn slice(start: int, end: int): str =
|
website/src/content/docs/declarations/enum.md
CHANGED
|
@@ -11,7 +11,7 @@ enum Ordering =
|
|
|
11
11
|
| EQ
|
|
12
12
|
| GT
|
|
13
13
|
|
|
14
|
-
fn (Ordering->ToStr) toStr()
|
|
14
|
+
fn (Ordering->ToStr) toStr() str =
|
|
15
15
|
match self
|
|
16
16
|
LT -> "LT"
|
|
17
17
|
EQ -> "EQ"
|
|
@@ -22,9 +22,21 @@ enum Shape =
|
|
|
22
22
|
| Square(int)
|
|
23
23
|
| Rectangle(int, int)
|
|
24
24
|
|
|
25
|
-
fn (Shape) toStr() =
|
|
25
|
+
fn (Shape) toStr() str =
|
|
26
26
|
match self
|
|
27
27
|
Circle(r) -> printLn("Circle(${r})")
|
|
28
28
|
Square(s) -> printLn("Square(${s})")
|
|
29
29
|
Rectangle(w, h) -> printLn("Rectangle(${w}, ${h})")
|
|
30
|
+
|
|
31
|
+
fn (Shape) area() float =
|
|
32
|
+
match self
|
|
33
|
+
Circle(r) -> PI * r * r
|
|
34
|
+
Square(s) -> s * s
|
|
35
|
+
Rectangle(w, h) -> w * h
|
|
36
|
+
|
|
37
|
+
fn (Shape) draw() float =
|
|
38
|
+
match self
|
|
39
|
+
Circle(r) -> PI * r * r
|
|
40
|
+
Square(s) -> s * s
|
|
41
|
+
Rectangle(w, h) -> w * h
|
|
30
42
|
```
|
website/src/content/docs/declarations/functions.md
CHANGED
|
@@ -4,7 +4,7 @@ description: These are basic types
|
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
```rs
|
|
7
|
-
fn fib(n: int)
|
|
7
|
+
fn fib(n: int) -> int =
|
|
8
8
|
match n
|
|
9
9
|
0 | 1 -> n
|
|
10
10
|
_ -> fib(n - 1) + fib(n - 2)
|
|
@@ -12,13 +12,13 @@ fn fib(n: int): int =
|
|
|
12
12
|
fn log(level: str, msg: str) =
|
|
13
13
|
printLn("${level}: ${msg}")
|
|
14
14
|
|
|
15
|
-
fn info(msg: str) =
|
|
15
|
+
pub fn info(msg: str) =
|
|
16
|
-
|
|
16
|
+
log("INFO", msg)
|
|
17
17
|
|
|
18
|
-
fn warning(msg: str) =
|
|
18
|
+
pub fn warning(msg: str) =
|
|
19
|
-
|
|
19
|
+
log("WARN", msg)
|
|
20
20
|
|
|
21
|
-
fn addLists
|
|
21
|
+
fn addLists(a: List[T], b: List[T]): List[T] =
|
|
22
22
|
a.concat(b)
|
|
23
23
|
|
|
24
24
|
// Variadic function
|
website/src/content/docs/declarations/records.md
CHANGED
|
@@ -6,109 +6,30 @@ description: These are basic types
|
|
|
6
6
|
A record is a collect of data indexed by fields. It is a reference type and reference counted.
|
|
7
7
|
|
|
8
8
|
```rs
|
|
9
|
-
|
|
9
|
+
record Cat(name: str, age: int) where
|
|
10
|
-
name: str
|
|
11
|
-
age: int
|
|
12
|
-
|
|
13
|
-
static fn withName(name: str)
|
|
10
|
+
static fn withName(name: str) Cat =
|
|
14
11
|
Cat(name: name, age: 0)
|
|
15
12
|
|
|
16
|
-
fn fullname()
|
|
13
|
+
fn fullname() str =
|
|
17
14
|
name + age.toStr()
|
|
18
15
|
|
|
19
16
|
fn talk() =
|
|
20
17
|
printLn("cat ${name} says meow")
|
|
21
18
|
|
|
22
|
-
override fn plus(other: Cat)
|
|
19
|
+
override fn plus(other: Cat) Cat =
|
|
23
20
|
Cat(...self, age: self.age + other.age)
|
|
24
21
|
|
|
25
|
-
override fn toStr()
|
|
22
|
+
override fn toStr() str =
|
|
26
23
|
"Cat<${fullname()}, ${age}>"
|
|
27
24
|
|
|
28
|
-
struct Rand(seed: int) =
|
|
29
|
-
self.seed = seed
|
|
30
|
-
|
|
31
|
-
fn Rand.int(): int =
|
|
32
|
-
1 * 2 * seed
|
|
33
|
-
|
|
34
|
-
struct Cat(name: str, age: int) =
|
|
35
|
-
self.name = name
|
|
36
|
-
self.age = age
|
|
37
|
-
|
|
38
|
-
fn Cat::withName(name: str): Cat =
|
|
39
|
-
Cat(name: name, age: 0)
|
|
40
|
-
|
|
41
|
-
fn Cat.fullname(): str =
|
|
42
|
-
name + age.toStr()
|
|
43
|
-
|
|
44
|
-
fn Cat.talk() =
|
|
45
|
-
printLn("cat ${name} says meow")
|
|
46
|
-
|
|
47
|
-
fn Cat.plus(other: Cat): Cat =
|
|
48
|
-
Cat(...self, age: self.age + other.age)
|
|
49
|
-
|
|
50
|
-
fn Cat.toStr(): str =
|
|
51
|
-
"Cat<${fullname()}, ${age}>"
|
|
52
|
-
|
|
53
|
-
record Response
|
|
54
|
-
field _body: Buffer
|
|
55
|
-
field _headers: Map[str, str]
|
|
56
|
-
field _status: int
|
|
57
|
-
|
|
58
|
-
fn _init() =
|
|
59
|
-
_body = Buffer()
|
|
60
|
-
_headers = Map[str, str]()
|
|
61
|
-
_status = 0
|
|
62
|
-
|
|
63
|
-
fn header(kv Pair): Self =
|
|
64
|
-
_headers.add(kv)
|
|
65
|
-
|
|
66
|
-
fn body(b Buffer): Self =
|
|
67
|
-
_body = b
|
|
68
|
-
|
|
69
|
-
fn status(v int): Self =
|
|
70
|
-
_status = v
|
|
71
|
-
|
|
72
|
-
|
|
25
|
+
record Map[K, V](items: List[Pair[K, V]]) where
|
|
73
|
-
`A Map is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
|
|
74
|
-
`A Map is not an array. A slice describes a piece of an array.
|
|
75
|
-
self._items = List[Pair[K, V]](kvs)
|
|
76
|
-
self.index = int(0)
|
|
77
|
-
self.length = int(kvs.length)
|
|
78
|
-
|
|
79
|
-
fn Map.range(yld: fn(k: K, v: V): bool): bool =
|
|
80
|
-
`provide a range operation for map
|
|
81
|
-
for pair := range self._items
|
|
82
|
-
if !yld(pair.k, pair.v)
|
|
83
|
-
false
|
|
84
|
-
true
|
|
85
|
-
|
|
86
|
-
fn Map.get(k: K): Option[V] =
|
|
87
|
-
for pair := range self._items
|
|
88
|
-
if pair.k == k
|
|
89
|
-
return v
|
|
90
|
-
None
|
|
91
|
-
|
|
92
|
-
fn Map.set(k: K, v: V) =
|
|
93
|
-
for pair := range self._items
|
|
94
|
-
if pair.k == k
|
|
95
|
-
pair.v = v
|
|
96
|
-
return
|
|
97
|
-
self._items.add(k, v)
|
|
98
|
-
|
|
99
|
-
fn Map.putIf(k: K, v: V) =
|
|
100
|
-
`put a value into the Map if its not already present
|
|
101
|
-
return
|
|
102
|
-
|
|
103
|
-
struct Map[K, V] where
|
|
104
|
-
_items: List[Pair[K, V]]
|
|
105
26
|
|
|
106
|
-
static fn of[K, V](kvs: ...Pair[K, V])
|
|
27
|
+
static fn of[K, V](kvs: ...Pair[K, V]) Map[K, V]
|
|
107
28
|
Map[K, V]().add(kvs)
|
|
108
29
|
|
|
109
|
-
fn range(yld: fn(k: K, v: V): bool)
|
|
30
|
+
fn range(yld: fn(k: K, v: V): bool) bool
|
|
110
31
|
`allows the range operator to iterate over the Map
|
|
111
|
-
for item := range self.
|
|
32
|
+
for item := range self.items
|
|
112
33
|
if !yld(item.k, item.v)
|
|
113
34
|
false
|
|
114
35
|
true
|
|
@@ -125,11 +46,11 @@ struct Map[K, V] where
|
|
|
125
46
|
|
|
126
47
|
fn set(k: K, v: V) =
|
|
127
48
|
`put a value into the Map
|
|
128
|
-
for pair := range self.
|
|
49
|
+
for pair := range self.items
|
|
129
50
|
if pair.k == k
|
|
130
51
|
pair.v = v
|
|
131
52
|
return
|
|
132
|
-
self.
|
|
53
|
+
self.items.add(k, v)
|
|
133
54
|
|
|
134
55
|
fn putIf(k: K, v: V) =
|
|
135
56
|
`put a value into the Map if its not already present
|
website/src/content/docs/declarations/traits.md
CHANGED
|
@@ -10,8 +10,13 @@ trait Equatable[A] where
|
|
|
10
10
|
fn eq(other: A): bool
|
|
11
11
|
fn ne(other: A): bool
|
|
12
12
|
|
|
13
|
+
enum Compare =
|
|
14
|
+
| Less
|
|
15
|
+
| Equal
|
|
16
|
+
| Greater
|
|
17
|
+
|
|
13
18
|
trait Comparable[A: Ord] where
|
|
14
|
-
fn compareTo(other: A):
|
|
19
|
+
fn compareTo(other: A): Compare
|
|
15
20
|
|
|
16
21
|
trait ArithmeticOperator[A] where
|
|
17
22
|
fn add(other: A): A
|
website/src/content/docs/naming-convention.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Naming Convention
|
|
3
|
+
description: Naming Convention
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
| Item | Convention |
|
|
7
|
+
| ------------------------ | ----------------------- |
|
|
8
|
+
| Modules | kebab-case |
|
|
9
|
+
| Types/Traits/Enum | UpperCamelCase |
|
|
10
|
+
| Fields/Functions/Methods | lowerCamelCase |
|
|
11
|
+
| Local variables | snake_case |
|
|
12
|
+
| Constants | SCREAMING_SNAKE_CASE |
|
|
13
|
+
| Generics | single uppercase letter |
|
|
14
|
+
|
|
15
|
+
## Rules
|
|
16
|
+
|
|
17
|
+
- Function parameters are passed by value only. You cannot modify a parameter. The compiler will throw an error if you try to.
|
|
18
|
+
- Strict naming convention
|
|
19
|
+
- Only one way of doing things ex: loops, condition
|