plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
c0bab65
— Peter John
2026-09-04T20:45:40+05:30
remove test
- test/LibC.plum +0 -11
- test/add.plum +0 -88
- test/aoc_2020_1.plum +0 -28
- test/aoc_2020_2.plum +0 -62
- test/aoc_2020_3.plum +0 -62
- test/aoc_2020_4.plum +0 -41
- test/import_fixtures/helper.plum +0 -4
- test/import_fixtures/main.plum +0 -6
- test/main.go +0 -39
- test/main.java +0 -9
- test/main.js +0 -11
- test/main.kt +0 -8
- test/main.py +0 -25
- test/main.rs +0 -18
- test/sample.plum +0 -274
- test/simple_add.plum +0 -2
test/LibC.plum
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
module LibC
|
|
2
|
-
|
|
3
|
-
include "include/libs/libc"
|
|
4
|
-
|
|
5
|
-
import std.(C)
|
|
6
|
-
import std.(List, Map, Math)
|
|
7
|
-
import std/encoding.(Base64)
|
|
8
|
-
|
|
9
|
-
extern sqrt(x: C.Double) -> C.Double
|
|
10
|
-
extern pow(x: C.Double, y: C.Double) -> C.Double
|
|
11
|
-
extern sqrt(x: C.Double) -> C.Double
|
test/add.plum
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
module test
|
|
2
|
-
|
|
3
|
-
import fs
|
|
4
|
-
|
|
5
|
-
fun add(a: Int, b: Int) -> Int =
|
|
6
|
-
a + b
|
|
7
|
-
|
|
8
|
-
fun give42() -> Int =
|
|
9
|
-
42
|
|
10
|
-
|
|
11
|
-
fun main() -> Int =
|
|
12
|
-
x1 = 2 * branch(4)
|
|
13
|
-
x2 = 3 * branch(9)
|
|
14
|
-
x3 = 5 * branch(11)
|
|
15
|
-
x1 + x2 + x3
|
|
16
|
-
|
|
17
|
-
branch(x: Int) -> Int
|
|
18
|
-
if x < 5
|
|
19
|
-
0
|
|
20
|
-
else if x < 10
|
|
21
|
-
1
|
|
22
|
-
else
|
|
23
|
-
2
|
|
24
|
-
|
|
25
|
-
fun main() -> Int =
|
|
26
|
-
result = 0
|
|
27
|
-
for i in 0...10
|
|
28
|
-
result += i
|
|
29
|
-
if i == 4
|
|
30
|
-
continue
|
|
31
|
-
else i == 7
|
|
32
|
-
break
|
|
33
|
-
|
|
34
|
-
# Recursive
|
|
35
|
-
fun factorial(x: Int) -> Int =
|
|
36
|
-
if x < 2 then
|
|
37
|
-
x
|
|
38
|
-
else
|
|
39
|
-
x * factorial(x - 1)
|
|
40
|
-
|
|
41
|
-
# While
|
|
42
|
-
fun factorial(x: Int) -> Int =
|
|
43
|
-
result = 1
|
|
44
|
-
i = n
|
|
45
|
-
while i == 0
|
|
46
|
-
result *= i
|
|
47
|
-
i -= 1
|
|
48
|
-
result
|
|
49
|
-
|
|
50
|
-
# For
|
|
51
|
-
factorial(num: Int): Int =
|
|
52
|
-
result = 1
|
|
53
|
-
for i in 2..num
|
|
54
|
-
result *= i
|
|
55
|
-
result
|
|
56
|
-
|
|
57
|
-
# Reduce
|
|
58
|
-
fun factorial(num: Int) -> Int =
|
|
59
|
-
(1..num).reduce(\a, b -> a * b)
|
|
60
|
-
|
|
61
|
-
main() -> Bool
|
|
62
|
-
5.squared() == 25
|
|
63
|
-
|
|
64
|
-
extend Int
|
|
65
|
-
fun squared() -> Int =
|
|
66
|
-
this * this
|
|
67
|
-
|
|
68
|
-
fun main() -> Unit =
|
|
69
|
-
input = fs::readFile("./examples/test/demos/aoc2023/1.txt")
|
|
70
|
-
calibration_sum = 0
|
|
71
|
-
first_digit = 0
|
|
72
|
-
last_digit = 0
|
|
73
|
-
for i in 0..input.len()
|
|
74
|
-
c = input.charAt(i)
|
|
75
|
-
if c >= '0' && c <= '9'
|
|
76
|
-
if first_digit == 0
|
|
77
|
-
first_digit = Int(c - '0')
|
|
78
|
-
last_digit = Int(c - '0')
|
|
79
|
-
else
|
|
80
|
-
last_digit = Int(c - '0')
|
|
81
|
-
continue
|
|
82
|
-
if c == '\n'
|
|
83
|
-
calibration_value = first_digit * 10 + last_digit
|
|
84
|
-
calibration_sum += calibration_value
|
|
85
|
-
first_digit = 0
|
|
86
|
-
last_digit = 0
|
|
87
|
-
continue
|
|
88
|
-
printLn(calibration_sum)
|
test/aoc_2020_1.plum
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
module aoc2020_1
|
|
2
|
-
|
|
3
|
-
import std/fs
|
|
4
|
-
import std/int
|
|
5
|
-
|
|
6
|
-
fun main() -> Unit =
|
|
7
|
-
input = fs.readFile!("./examples/test/demos/aoc2020/1.txt")
|
|
8
|
-
numbers = parseNumbers(input)
|
|
9
|
-
for i in 0...numbers.size
|
|
10
|
-
a = numbers.get!<u32>(i)
|
|
11
|
-
for j in 0...numbers.size
|
|
12
|
-
b = numbers.get!<u32>(j)
|
|
13
|
-
if a + b == 2020
|
|
14
|
-
printLn(a * b)
|
|
15
|
-
return
|
|
16
|
-
|
|
17
|
-
fun parseNumbers(input: Str) -> List<Int> =
|
|
18
|
-
numbers = List<Int>()
|
|
19
|
-
current_number = 0
|
|
20
|
-
for i in 0..input.length()
|
|
21
|
-
c = input.charAt(i)
|
|
22
|
-
if c >= '0' && c <= '9'
|
|
23
|
-
current_number *= 10
|
|
24
|
-
current_number += Int(c - '0')
|
|
25
|
-
if c == '\n'
|
|
26
|
-
numbers.push!<u32>(current_number)
|
|
27
|
-
current_number = 0
|
|
28
|
-
numbers
|
test/aoc_2020_2.plum
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
enum Step(n: Int) =
|
|
2
|
-
| READ_MIN_OCCURANCES(0)
|
|
3
|
-
| READ_MAX_OCCURANCES(1)
|
|
4
|
-
| READ_CHAR_TO_COUNT(2)
|
|
5
|
-
| COUNT_OCCURANCES(3)
|
|
6
|
-
|
|
7
|
-
fun toNumber(self) =
|
|
8
|
-
match self
|
|
9
|
-
| READ_MIN_OCCURANCES => 0
|
|
10
|
-
| READ_MAX_OCCURANCES => 1
|
|
11
|
-
| READ_CHAR_TO_COUNT => 2
|
|
12
|
-
| COUNT_OCCURANCES => 3
|
|
13
|
-
|
|
14
|
-
type PasswordCheckState
|
|
15
|
-
step: Step
|
|
16
|
-
min_occurances: Int
|
|
17
|
-
max_occurances: Int
|
|
18
|
-
current_occurances: Int
|
|
19
|
-
char_to_count: Int
|
|
20
|
-
|
|
21
|
-
fun create() -> PasswordCheckState =
|
|
22
|
-
PasswordCheckState(
|
|
23
|
-
step: READ_MIN_OCCURANCES,
|
|
24
|
-
min_occurances: 0,
|
|
25
|
-
max_occurances: 0,
|
|
26
|
-
current_occurances: 0,
|
|
27
|
-
char_to_count: '\0',
|
|
28
|
-
)
|
|
29
|
-
|
|
30
|
-
fun main() -> Unit =
|
|
31
|
-
input = fs.readFile!("./examples/test/demos/aoc2020/2.txt")
|
|
32
|
-
valid_passwords_count = 0
|
|
33
|
-
state = PasswordCheckState.create()
|
|
34
|
-
for i in 0..input.len()
|
|
35
|
-
c = input.charAt(i)
|
|
36
|
-
if state.step == READ_MIN_OCCURANCES
|
|
37
|
-
if c == '-'
|
|
38
|
-
state.step = READ_MAX_OCCURANCES
|
|
39
|
-
continue
|
|
40
|
-
state.min_occurances *= 10
|
|
41
|
-
state.min_occurances += (c - '0') as Int
|
|
42
|
-
else if state.step == READ_MAX_OCCURANCES
|
|
43
|
-
if c == ' '
|
|
44
|
-
state.step = READ_CHAR_TO_COUNT
|
|
45
|
-
continue
|
|
46
|
-
state.max_occurances *= 10
|
|
47
|
-
state.max_occurances += (c - '0') as Int
|
|
48
|
-
else if state.step == READ_CHAR_TO_COUNT
|
|
49
|
-
state.char_to_count = c
|
|
50
|
-
# skip `${c}: `, (+1 will be skipped by loop)
|
|
51
|
-
i += 2
|
|
52
|
-
state.step = COUNT_OCCURANCES
|
|
53
|
-
continue
|
|
54
|
-
else if state.step == COUNT_OCCURANCES
|
|
55
|
-
if c == '\n'
|
|
56
|
-
if state.current_occurances >= state.min_occurances && state.current_occurances <= state.max_occurances
|
|
57
|
-
valid_passwords_count += 1
|
|
58
|
-
state = PasswordCheckState.create()
|
|
59
|
-
continue
|
|
60
|
-
if c == state.char_to_count
|
|
61
|
-
state.current_occurances += 1
|
|
62
|
-
writeLn(valid_passwords_count)
|
test/aoc_2020_3.plum
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
module aoc_2020_3
|
|
2
|
-
|
|
3
|
-
import cli
|
|
4
|
-
import fs
|
|
5
|
-
|
|
6
|
-
Tile =
|
|
7
|
-
| TREE = True
|
|
8
|
-
| EMPTY = False
|
|
9
|
-
|
|
10
|
-
fun slopeI() = 1
|
|
11
|
-
fun slopeJ() = 3
|
|
12
|
-
|
|
13
|
-
fun main() -> Unit =
|
|
14
|
-
let input = fs::read_file!("./examples/test/demos/aoc2020/3.txt");
|
|
15
|
-
defer input.free();
|
|
16
|
-
let map = Map::parse(input);
|
|
17
|
-
defer map.free();
|
|
18
|
-
let tree_count = 0;
|
|
19
|
-
let i = 0;
|
|
20
|
-
let j = 0;
|
|
21
|
-
loop {
|
|
22
|
-
i += slopeI();
|
|
23
|
-
j += slopeJ();
|
|
24
|
-
if i == map.rows.size {
|
|
25
|
-
break;
|
|
26
|
-
};
|
|
27
|
-
if map.get(i, j) == Tile::TREE {
|
|
28
|
-
tree_count += 1;
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
print_u32(tree_count);
|
|
32
|
-
puts("\n");
|
|
33
|
-
|
|
34
|
-
Map = (
|
|
35
|
-
rows: List(List(Tile))
|
|
36
|
-
)
|
|
37
|
-
|
|
38
|
-
Map\parse(input: String) -> Map =
|
|
39
|
-
rows = listOf(List<Tile>())
|
|
40
|
-
current_row = listOf<Tile>()
|
|
41
|
-
for i in 0..input.len()
|
|
42
|
-
c = input.char_at(i);
|
|
43
|
-
if c == '#'
|
|
44
|
-
current_row.push!(TREE)
|
|
45
|
-
if c == '.'
|
|
46
|
-
current_row.push!(EMPTY)
|
|
47
|
-
if c == '\n'
|
|
48
|
-
rows.push!(current_row)
|
|
49
|
-
current_row = listOf<Tile>()
|
|
50
|
-
Map(
|
|
51
|
-
rows: rows,
|
|
52
|
-
)
|
|
53
|
-
|
|
54
|
-
Map\get(i: u32, j: u32) -> Tile =
|
|
55
|
-
row = rows.get!(i);
|
|
56
|
-
*(row.at(j % row.size));
|
|
57
|
-
|
|
58
|
-
Map\free() -> Unit =
|
|
59
|
-
for i in 0..rows.size
|
|
60
|
-
row = rows.get!(i);
|
|
61
|
-
row.free();
|
|
62
|
-
rows.free();
|
test/aoc_2020_4.plum
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
module aoc2020_4
|
|
2
|
-
|
|
3
|
-
fun main() -> Result =
|
|
4
|
-
input = fs::readFile!("./examples/test/demos/aoc2020/4.txt")
|
|
5
|
-
valid_passwords = 0
|
|
6
|
-
passwords = StrCutter(input.toStr())
|
|
7
|
-
while passwords.rest.size > 0
|
|
8
|
-
has_byr = False
|
|
9
|
-
has_iyr = False
|
|
10
|
-
has_eyr = False
|
|
11
|
-
has_hgt = False
|
|
12
|
-
has_hcl = False
|
|
13
|
-
has_ecl = False
|
|
14
|
-
has_pid = False
|
|
15
|
-
passwords = passwords.cut_at("\n\n")
|
|
16
|
-
lines = StrCutter(passwords.piece)
|
|
17
|
-
while lines.rest.size > 0
|
|
18
|
-
lines = lines.cut_at("\n")
|
|
19
|
-
fields = StrCutter(lines.piece)
|
|
20
|
-
while fields.rest.size > 0
|
|
21
|
-
fields = fields.cut_at(" ")
|
|
22
|
-
field_parts = StrCutter(fields.piece).cut_at(":")
|
|
23
|
-
field_name = field_parts.piece;
|
|
24
|
-
field_value = field_parts.rest;
|
|
25
|
-
if field_name.equals("byr")
|
|
26
|
-
has_byr = True
|
|
27
|
-
else if field_name.equals("iyr")
|
|
28
|
-
has_iyr = True
|
|
29
|
-
else if field_name.equals("eyr")
|
|
30
|
-
has_eyr = True
|
|
31
|
-
else if field_name.equals("hgt")
|
|
32
|
-
has_hgt = True
|
|
33
|
-
else if field_name.equals("hcl")
|
|
34
|
-
has_hcl = True
|
|
35
|
-
else if field_name.equals("ecl")
|
|
36
|
-
has_ecl = true
|
|
37
|
-
else if field_name.equals("pid")
|
|
38
|
-
has_pid = True
|
|
39
|
-
if has_byr && has_iyr && has_eyr && has_hgt && has_hcl && has_ecl && has_pid
|
|
40
|
-
valid_passwords += 1
|
|
41
|
-
printLn(valid_passwords)
|
test/import_fixtures/helper.plum
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
module fixtures
|
|
2
|
-
|
|
3
|
-
fun helperValue() -> Int =
|
|
4
|
-
42
|
test/import_fixtures/main.plum
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
module fixtures
|
|
2
|
-
|
|
3
|
-
import helper
|
|
4
|
-
|
|
5
|
-
fun main() -> Int =
|
|
6
|
-
helperValue()
|
test/main.go
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
package main
|
|
2
|
-
|
|
3
|
-
const MIN_VALUE = -0x8000_0000_0000_0000 // Lowest value of Int
|
|
4
|
-
const MAX_VALUE = 0x7FFF_FFFF_FFFF_FFFF // Highest value of Int
|
|
5
|
-
const LARGE = 268435456 # 2**28
|
|
6
|
-
|
|
7
|
-
import (
|
|
8
|
-
"123"
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
type Game struct {
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
func random() Int {
|
|
16
|
-
a := 123
|
|
17
|
-
println("Hellow")
|
|
18
|
-
panic("TODO")
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
func fromStr() Result(Int, Err) {
|
|
22
|
-
return Ok(0)
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
func (i Int) toFloat() Float {
|
|
26
|
-
return FloatfromInt(i)
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
func (i Int) add(b: Int) Int {
|
|
30
|
-
return i + b
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
func (i Int) sub(b: Int) Int {
|
|
34
|
-
return i - b
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
func (i Int) abs() Int {
|
|
38
|
-
return i < 0 ? -i : i
|
|
39
|
-
}
|
test/main.java
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
class Data<T> {
|
|
2
|
-
static Float PI = 3.14;
|
|
3
|
-
|
|
4
|
-
public void printLn() {
|
|
5
|
-
this.name
|
|
6
|
-
final 123 =
|
|
7
|
-
return "\(123)";
|
|
8
|
-
}
|
|
9
|
-
}
|
test/main.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
class Data {
|
|
4
|
-
toStr() {
|
|
5
|
-
return `${this.name}`
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function toStr() {
|
|
10
|
-
return `Hello ${this.name}`
|
|
11
|
-
}
|
test/main.kt
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
class Data {
|
|
2
|
-
val Float PI = 3.14;
|
|
3
|
-
|
|
4
|
-
fun printLn() {
|
|
5
|
-
val name = "${name}123"
|
|
6
|
-
return "${name}";
|
|
7
|
-
}
|
|
8
|
-
}
|
test/main.py
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
class ToStr:
|
|
2
|
-
pass
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def main(data: str):
|
|
6
|
-
pass
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
class Dog:
|
|
10
|
-
|
|
11
|
-
def bark():
|
|
12
|
-
print("barking dog")
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
class Int(ToStr):
|
|
16
|
-
def __init__():
|
|
17
|
-
format("%d")
|
|
18
|
-
|
|
19
|
-
def toStr(self, name: Int):
|
|
20
|
-
return f"Hello {self.name}"
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
raw_string = r"Python\nis\easy\to\learn"
|
|
24
|
-
v = Dog(a="123")
|
|
25
|
-
v.bark()
|
test/main.rs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
mod 123
|
|
2
|
-
|
|
3
|
-
static data = ""
|
|
4
|
-
|
|
5
|
-
struct Row {
|
|
6
|
-
pub id: String
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
impl Row {
|
|
10
|
-
fn add(&self, other: Row) -> Row {
|
|
11
|
-
Row {}
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
fn main() {
|
|
16
|
-
println("1231")
|
|
17
|
-
Row("123")
|
|
18
|
-
}
|
test/sample.plum
DELETED
|
@@ -1,274 +0,0 @@
|
|
|
1
|
-
module std/http
|
|
2
|
-
|
|
3
|
-
import std/path
|
|
4
|
-
import std/os
|
|
5
|
-
import std/http/content_type
|
|
6
|
-
import std/libc
|
|
7
|
-
|
|
8
|
-
trait Writer =
|
|
9
|
-
write(p: Str | Buffer) -> Result(Int, WriteError)
|
|
10
|
-
|
|
11
|
-
enum ReadError =
|
|
12
|
-
| Eof
|
|
13
|
-
| Closed
|
|
14
|
-
|
|
15
|
-
enum WriteError =
|
|
16
|
-
| Eof
|
|
17
|
-
| Closed
|
|
18
|
-
|
|
19
|
-
trait Reader =
|
|
20
|
-
read(p: Str | Buffer) -> Result(Int, ReadError)
|
|
21
|
-
|
|
22
|
-
trait IO(Reader, Writer)
|
|
23
|
-
|
|
24
|
-
type Cat(ToStr) =
|
|
25
|
-
name: Str
|
|
26
|
-
age: Int
|
|
27
|
-
|
|
28
|
-
fun parseCat<Json>() -> Result(Cat, Err) =
|
|
29
|
-
v = try Json.parse(self) as Map(Str, Json)
|
|
30
|
-
Cat::create()
|
|
31
|
-
.name(v.get("name").asStr())
|
|
32
|
-
.age(v.get("age").asInt())
|
|
33
|
-
|
|
34
|
-
fun withName<Cat>(name: Str) -> Cat =
|
|
35
|
-
Cat(name: name, age: self.age)
|
|
36
|
-
|
|
37
|
-
fun withAge<Cat>(age: Int) -> Cat =
|
|
38
|
-
Cat(name: self.name, age: age)
|
|
39
|
-
|
|
40
|
-
fun toStr<Cat>() -> Str =
|
|
41
|
-
"Cat({self.name}, {self.age})"
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
2.sqrt().sin().cos()
|
|
45
|
-
|
|
46
|
-
type User(ToStr) =
|
|
47
|
-
name: Str
|
|
48
|
-
age: Int
|
|
49
|
-
todos: List(Todo)
|
|
50
|
-
|
|
51
|
-
fun parseUser<Json>() -> Result(Self, Err) =
|
|
52
|
-
v = try Json.parse(self) as Map(Str, Json)
|
|
53
|
-
User.build()
|
|
54
|
-
.name(v.get("name").asStr())
|
|
55
|
-
.email(v.get("email").asStr())
|
|
56
|
-
.todos(v.get("todos").asList().map(|t|
|
|
57
|
-
Todo::create().title(t.get("title").asStr()))
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
fun isAuthorized<User>() -> Bool =
|
|
61
|
-
False
|
|
62
|
-
|
|
63
|
-
fun map<User>(cb: (a: User) -> b) -> b =
|
|
64
|
-
cb(u)
|
|
65
|
-
|
|
66
|
-
fun toStr<User>() -> Str =
|
|
67
|
-
"User({self.name}, {self.age})"
|
|
68
|
-
|
|
69
|
-
u = User.build()
|
|
70
|
-
.name("John Doe")
|
|
71
|
-
.email("[email protected]")
|
|
72
|
-
.todos(Todo.create().title("Make pizza"))
|
|
73
|
-
.todos(Todo.create().title("Finish Toasty"))
|
|
74
|
-
.todos(Todo.create().title("Sleep"))
|
|
75
|
-
|
|
76
|
-
u2 = try User.fromJson(`{
|
|
77
|
-
"name": "John Doe",
|
|
78
|
-
"email": "[email protected]",
|
|
79
|
-
"todos": [
|
|
80
|
-
{"title": "Make pizza"},
|
|
81
|
-
{"title": "Finish Toasty"},
|
|
82
|
-
{"title": "Sleep"}
|
|
83
|
-
]
|
|
84
|
-
}`)
|
|
85
|
-
|
|
86
|
-
fun stoplightColor(something: Int) -> Color =
|
|
87
|
-
if something > 0
|
|
88
|
-
Red
|
|
89
|
-
else if something == 0
|
|
90
|
-
Yellow
|
|
91
|
-
else
|
|
92
|
-
Green
|
|
93
|
-
|
|
94
|
-
fun test("stoplightColor") =
|
|
95
|
-
doc: "Get the color of something"
|
|
96
|
-
assert stoplightColor(1) == Red
|
|
97
|
-
assert stoplightColor(0) == Yellow
|
|
98
|
-
assert stoplightColor(-1) == Green
|
|
99
|
-
|
|
100
|
-
fun toCelsius(f: Float) -> Float =
|
|
101
|
-
doc: "Convert fahrenheit temperature reading to celsius"
|
|
102
|
-
return {f - 32} * {5 / 9}
|
|
103
|
-
check:
|
|
104
|
-
fun toCelsius(0) == 32
|
|
105
|
-
fun toCelsius(100) == 212
|
|
106
|
-
fun toCelsius(100) == 392
|
|
107
|
-
|
|
108
|
-
fun empty() -> Response =
|
|
109
|
-
Response::create()
|
|
110
|
-
.body(Buffer())
|
|
111
|
-
.headers(Map())
|
|
112
|
-
.status(0)
|
|
113
|
-
|
|
114
|
-
fun createFileResponse(path: Str) -> Result(Response, IOError) =
|
|
115
|
-
content_type = try Mime::fromExt(path.ext())
|
|
116
|
-
body = try os.readFile(file)
|
|
117
|
-
Response::create()
|
|
118
|
-
.header("Content-Type", content_type)
|
|
119
|
-
.body(body)
|
|
120
|
-
.status(200)
|
|
121
|
-
|
|
122
|
-
fun serveFile(file: Str) -> Result(Response, IOError) =
|
|
123
|
-
ext = try Path::fromExt(file)
|
|
124
|
-
content_type = try Mime::fromExt(ext)
|
|
125
|
-
body = os.readFile(file)
|
|
126
|
-
Response::create()
|
|
127
|
-
.header("Content-Type", content_type)
|
|
128
|
-
.body(body)
|
|
129
|
-
.status(200)
|
|
130
|
-
|
|
131
|
-
fun index() -> Result(Response, IOError) =
|
|
132
|
-
createFileResponse("index.html")
|
|
133
|
-
|
|
134
|
-
type Response(Reader, Writer) =
|
|
135
|
-
body: Buffer = Buffer()
|
|
136
|
-
headers: Map(Str, Str) = Map()
|
|
137
|
-
status: Int = 0
|
|
138
|
-
|
|
139
|
-
g = Greeter(name: "abc")
|
|
140
|
-
g = Greeter(...g, name: "123")
|
|
141
|
-
|
|
142
|
-
fun readFile() -> Result(String, Err) =
|
|
143
|
-
return Err("123")
|
|
144
|
-
|
|
145
|
-
fun main() -> Unit =
|
|
146
|
-
printLn("123")
|
|
147
|
-
createFileResponse("./src/sample.plum")
|
|
148
|
-
index()
|
|
149
|
-
serveFile("./src/sample.plum")
|
|
150
|
-
content = try readFile()
|
|
151
|
-
|
|
152
|
-
if Ok(d) = readFile()
|
|
153
|
-
println(d)
|
|
154
|
-
|
|
155
|
-
match readFile()
|
|
156
|
-
| Ok(d) =>
|
|
157
|
-
printLn(d)
|
|
158
|
-
| Err(e) =>
|
|
159
|
-
printErr(e)
|
|
160
|
-
|
|
161
|
-
fun sub(arg1: Int, arg2: Int) -> Int =
|
|
162
|
-
local1 = arg1
|
|
163
|
-
local2 = arg2
|
|
164
|
-
local1 - local2
|
|
165
|
-
|
|
166
|
-
fun addAndStringify(num1: Int, num2: Int) -> Str =
|
|
167
|
-
return (num1 + num2).toStr()
|
|
168
|
-
|
|
169
|
-
fun factorial(x: Int) -> Int =
|
|
170
|
-
if x < 2
|
|
171
|
-
x
|
|
172
|
-
else
|
|
173
|
-
x * factorial(x - 1)
|
|
174
|
-
|
|
175
|
-
birds = 3
|
|
176
|
-
iguanas = 2
|
|
177
|
-
total = addAndStringify(birds, iguanas)
|
|
178
|
-
|
|
179
|
-
fun pluralize(singular: Str, plural: Str, count: Int) -> Str =
|
|
180
|
-
countstr = Num.toStr(count)
|
|
181
|
-
if count == 1 then
|
|
182
|
-
"$(countstr) $(singular)"
|
|
183
|
-
else
|
|
184
|
-
"$(countstr) $(plural)"
|
|
185
|
-
|
|
186
|
-
pluralize_test(t: Test) -> Unit =
|
|
187
|
-
assert pluralize("cactus", "cacti", 1) == "1 cactus"
|
|
188
|
-
assert pluralize("cactus", "cacti", 2) == "2 cacti"
|
|
189
|
-
|
|
190
|
-
fun name(d: Option(Str)) -> Str =
|
|
191
|
-
if d == Some(v) then
|
|
192
|
-
v.subString(0, 3)
|
|
193
|
-
else if a == b then
|
|
194
|
-
"123"
|
|
195
|
-
else
|
|
196
|
-
"1231"
|
|
197
|
-
|
|
198
|
-
fun delta(d: Option(Str)) -> Str =
|
|
199
|
-
if d == Some(v)
|
|
200
|
-
v.subString(0, 3)
|
|
201
|
-
else if a == b
|
|
202
|
-
"123"
|
|
203
|
-
else
|
|
204
|
-
if a < 100
|
|
205
|
-
"Data"
|
|
206
|
-
else
|
|
207
|
-
"No Data"
|
|
208
|
-
|
|
209
|
-
MIN_VALUE = -0x8000_0000_0000_0000 # Lowest value of Int
|
|
210
|
-
MAX_VALUE = 0x7FFF_FFFF_FFFF_FFFF # Highest value of Int
|
|
211
|
-
LARGE = 268435456 # 2**28
|
|
212
|
-
|
|
213
|
-
fun random() -> Float = # generate random number
|
|
214
|
-
panic("TODO")
|
|
215
|
-
|
|
216
|
-
fun fromStr() -> Result(Int, Err) = # convert Str to Int
|
|
217
|
-
Ok(0)
|
|
218
|
-
|
|
219
|
-
type Int(Comparable, ToStr)
|
|
220
|
-
|
|
221
|
-
fun toFloat<Int>() -> Float = # convert Int to Float
|
|
222
|
-
Float(self)
|
|
223
|
-
|
|
224
|
-
fun add<Int>(other: Int) -> Int =
|
|
225
|
-
self + other
|
|
226
|
-
|
|
227
|
-
fun sub<Int>(other: Int) -> Int =
|
|
228
|
-
self - other
|
|
229
|
-
|
|
230
|
-
fun abs<Int>() -> Int =
|
|
231
|
-
self < 0 ? -self : self
|
|
232
|
-
|
|
233
|
-
fun main() =
|
|
234
|
-
user = User(name: "John Doe", age: 30)
|
|
235
|
-
if user.isAuthorized()
|
|
236
|
-
println("IsAutho")
|
|
237
|
-
|
|
238
|
-
user.map({ it.name })
|
|
239
|
-
|
|
240
|
-
params = Map("one" => 1, "two" => 2, "three" => 3)
|
|
241
|
-
arr = List(1, 2, 3, 4)
|
|
242
|
-
|
|
243
|
-
d.price?.takeIf(\a -> a != "")
|
|
244
|
-
(1 + 2).mod(3).pow(2).sqrt()
|
|
245
|
-
|
|
246
|
-
if v == None
|
|
247
|
-
error.Error
|
|
248
|
-
else if Some(c) = v
|
|
249
|
-
c * 20
|
|
250
|
-
|
|
251
|
-
match v
|
|
252
|
-
None -> error.Error
|
|
253
|
-
Some(c) -> c * 20
|
|
254
|
-
|
|
255
|
-
names = List("Sam", "Lee", "Ari")
|
|
256
|
-
names.append("Jess")
|
|
257
|
-
names
|
|
258
|
-
.filter({ it == "Sam" })
|
|
259
|
-
.map({ it * 2 })
|
|
260
|
-
|
|
261
|
-
fun main() =
|
|
262
|
-
sum = 1 + {{2 * 3} / 4}
|
|
263
|
-
enabled = !False
|
|
264
|
-
open = {count > 10} && {enabled == True} || {debug == False}
|
|
265
|
-
countries_list = listOf("US", "INDIA", "CANADA")
|
|
266
|
-
list_ints = listOf(1, 2, 3)
|
|
267
|
-
nested_list_ints = listOf(listOf(1), listOf(2), listOf(3))
|
|
268
|
-
country_codes = mapOf(
|
|
269
|
-
"in" => "INDIA",
|
|
270
|
-
"us" => "United States",
|
|
271
|
-
"ca" => "Canada"
|
|
272
|
-
)
|
|
273
|
-
map_ints = mapOf("a" => 1, "b" => 2)
|
|
274
|
-
nested_map_ints = mapOf("a" => 1, "b" => mapOf("c" => 3, "d" => 4))
|
test/simple_add.plum
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
fun add(a: Int, b: Int) -> Int =
|
|
2
|
-
a + b
|