~repos /plum
git clone https://pyrossh.dev/repos/plum.git
A statically typed, imperative programming language inspired by rust, python
68c4568f
—
pyrossh 1 year ago
docs
- readme.md +84 -2
- std/list.mi +83 -37
readme.md
CHANGED
|
@@ -8,6 +8,15 @@ Here is some sample code, please enjoy.
|
|
|
8
8
|
```rs
|
|
9
9
|
module lambda
|
|
10
10
|
|
|
11
|
+
import pacos/list
|
|
12
|
+
import pacos/math
|
|
13
|
+
import pacos/http
|
|
14
|
+
|
|
15
|
+
const start_year = 2101
|
|
16
|
+
const end_year = 2111
|
|
17
|
+
const name: String = "Gleam"
|
|
18
|
+
const size: Int = 100
|
|
19
|
+
|
|
11
20
|
fn sum(a: int, b: int): int = a + b
|
|
12
21
|
|
|
13
22
|
fn sum_all(series: list[int]): int =
|
|
@@ -46,14 +55,42 @@ fn first_item(l: list[int]): int? =
|
|
|
46
55
|
fn to-celsius(f: float): float =
|
|
47
56
|
(f - 32) * (5 / 9)
|
|
48
57
|
|
|
58
|
+
class Cat[A: Comparable & Stringable] is Stringable:
|
|
59
|
+
name: str
|
|
60
|
+
age: int
|
|
61
|
+
|
|
62
|
+
fn Cat():
|
|
63
|
+
pass
|
|
64
|
+
|
|
65
|
+
fn ~Cat():
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
fn Cat.with_name(name: str):
|
|
69
|
+
Cat(name: name, age: 0)
|
|
70
|
+
|
|
71
|
+
fn fullname() -> str:
|
|
72
|
+
name + age.to_str()
|
|
73
|
+
|
|
74
|
+
fn talk():
|
|
75
|
+
println("cat ${name} says meow")
|
|
76
|
+
|
|
77
|
+
fn to_str() -> str:
|
|
78
|
+
"Cat<{fullname()}, ${age}>"
|
|
79
|
+
|
|
49
|
-
record Cat(
|
|
80
|
+
record Cat[A](
|
|
50
81
|
name: str
|
|
51
82
|
age: int
|
|
52
|
-
)
|
|
83
|
+
) where A is Comparable & Stringable
|
|
53
84
|
|
|
54
85
|
fn Cat.with_name(name: str) =
|
|
55
86
|
Cat(name: name, age: 0)
|
|
56
87
|
|
|
88
|
+
fn (c: Cat) init(): str =
|
|
89
|
+
c.name + c.age.to_str()
|
|
90
|
+
|
|
91
|
+
fn (c: Cat) deinit(): str =
|
|
92
|
+
c.name + c.age.to_str()
|
|
93
|
+
|
|
57
94
|
fn (c: Cat) fullname(): str =
|
|
58
95
|
c.name + c.age.to_str()
|
|
59
96
|
|
|
@@ -300,6 +337,51 @@ when
|
|
|
300
337
|
cmp > 0 -> low = mid + 1
|
|
301
338
|
cmp < 0 -> high = mid
|
|
302
339
|
cmp == 0 -> return mid, true
|
|
340
|
+
|
|
341
|
+
when x, y
|
|
342
|
+
1, 1 -> "both are 1"
|
|
343
|
+
1, _ -> "x is 1"
|
|
344
|
+
_, 1 -> "y is 1"
|
|
345
|
+
_, _ -> "neither is 1"
|
|
346
|
+
|
|
347
|
+
when number
|
|
348
|
+
2 | 4 | 6 | 8 -> "This is an even number"
|
|
349
|
+
1 | 3 | 5 | 7 -> "This is an odd number"
|
|
350
|
+
_ -> "I'm not sure"
|
|
351
|
+
|
|
352
|
+
match xs
|
|
353
|
+
[] -> "This list is empty"
|
|
354
|
+
[a] -> "This list has 1 element"
|
|
355
|
+
[a, b] -> "This list has 2 elements"
|
|
356
|
+
_ -> "This list has more than 2 elements"
|
|
357
|
+
|
|
358
|
+
import palm/list
|
|
359
|
+
|
|
360
|
+
list.new("Krabs")
|
|
361
|
+
|> list.add("Spongebob")
|
|
362
|
+
|> list.length() // ==> 3
|
|
363
|
+
|> list.contains("Krabs") // ==> true
|
|
364
|
+
|> list.get(0) // => Some("Krabs")
|
|
365
|
+
|> list.get(5) // => None
|
|
366
|
+
|
|
367
|
+
let x = list.new(2, 3)
|
|
368
|
+
let y = list.new(1, ..x)
|
|
369
|
+
|
|
370
|
+
import palm/map
|
|
371
|
+
|
|
372
|
+
let nums = map.new(:one => 1, :two => 2) // => Map(k, v)
|
|
373
|
+
nums |> map.get(:one) // => Some(1)
|
|
374
|
+
nums |> map.get(:unknown) // => None
|
|
375
|
+
|
|
376
|
+
import palm/io.{println}
|
|
377
|
+
import palm/result.{Ok, Error}
|
|
378
|
+
|
|
379
|
+
let connect_res = Error("Connection failed")
|
|
380
|
+
|
|
381
|
+
case connect_res {
|
|
382
|
+
Ok(a) -> println("Connection succeeded")
|
|
383
|
+
Err(a) -> println("Error occurred: {a}")
|
|
384
|
+
}
|
|
303
385
|
```
|
|
304
386
|
|
|
305
387
|
Arithmetic (+, -, /, *, @divFloor, @sqrt, @ceil, @log, etc.)
|
std/list.mi
CHANGED
|
@@ -1,39 +1,85 @@
|
|
|
1
1
|
module std
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
record list<V>(
|
|
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
|
-
|
|
3
|
+
`A list is a data structure describing a contiguous section of an array stored separately from the slice variable itself.
|
|
4
|
+
`It contains the pointers to the start and end nodes (head, tail) and maintains the size as well
|
|
5
|
+
record list<V>(
|
|
6
|
+
head: node?
|
|
7
|
+
tail: node?
|
|
8
|
+
size: int
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
`A node captures the data value in a list and contains pointers to the previous and next sibling nodes
|
|
12
|
+
record node[V](
|
|
13
|
+
value: V
|
|
14
|
+
prev: node[V]?
|
|
15
|
+
next: node[V]?
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
fn (l: list) get(i: int): A =
|
|
19
|
+
`gets the element at i'th index of the list
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
fn (l: list) set(i: int, v: V): A =
|
|
23
|
+
`sets the element at i'th index of the list
|
|
24
|
+
pass
|
|
25
|
+
|
|
26
|
+
fn (l: list) add(c: V) =
|
|
27
|
+
`adds the specified elements to the start of the list
|
|
28
|
+
l.head = node(value: v, prev: nil, next: l.head)
|
|
29
|
+
l.head.next.prev = l.head
|
|
30
|
+
l.size += 1
|
|
31
|
+
|
|
32
|
+
fn (l: list) remove(i int) =
|
|
33
|
+
`removes the element at i'th index of the list
|
|
34
|
+
l.tail?.prev?.next = nil
|
|
35
|
+
`old tail node gets deallocated due to zero reference count
|
|
36
|
+
l.tail = list.tail?.prev
|
|
37
|
+
l.size -= 1
|
|
38
|
+
|
|
39
|
+
fn (l: list) some(predicate: fn(v: V): bool) =
|
|
40
|
+
`returns true if any element in the list satisfies the predicate
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
fn (l: list) reversed(v: fn(v: V): true) =
|
|
45
|
+
`returns a new list with the elements in reverse order.
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
fn (l: list) sort(v: fn(v: V): true) =
|
|
49
|
+
`returns a new list with the elements sorted in ascending order.
|
|
50
|
+
pass
|
|
51
|
+
|
|
52
|
+
fn (l: list) indexOf(v: V): int? =
|
|
53
|
+
`returns the index of an item in the list if present and comparable otherwise nil
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
fn (l: list) each(cb: fn(v: V)) =
|
|
57
|
+
current := l.head
|
|
58
|
+
while current != nil
|
|
59
|
+
cb(current.value)
|
|
60
|
+
current = current.next
|
|
61
|
+
|
|
62
|
+
fn (l: list) map(cb: fn(v: V): A): list[A] =
|
|
63
|
+
nl := []
|
|
64
|
+
current := l.head
|
|
65
|
+
while current != nil
|
|
66
|
+
item := cb(current.value)
|
|
67
|
+
nl.push(item)
|
|
68
|
+
nl
|
|
69
|
+
|
|
70
|
+
fn (l: list) retain(predicate: fn(v: V): A): list[A] =
|
|
71
|
+
`returns a new list with the elements that matched the predicate
|
|
72
|
+
pass
|
|
73
|
+
|
|
74
|
+
fn (l: list) reduce[B](acc: B, cb: fn(v: V): A): B =
|
|
75
|
+
`returns the accumulated value of all the elements in the list
|
|
76
|
+
pass
|
|
77
|
+
|
|
78
|
+
fn (l: list) to_str() =
|
|
79
|
+
res := Buffer()
|
|
80
|
+
l.each() |v|
|
|
81
|
+
if @HasTrait(V, ToStr)
|
|
82
|
+
res.write(v.to_str())
|
|
83
|
+
else
|
|
84
|
+
res.write(@TypeToString(v))
|
|
85
|
+
res.to_str()
|