~repos /plum
git clone https://pyrossh.dev/repos/plum.git
A statically typed, imperative programming language inspired by rust, python
7b112e2a
—
pyrossh 1 year ago
improve fn example
- example/fn.palm +86 -58
example/fn.palm
CHANGED
|
@@ -1,74 +1,69 @@
|
|
|
1
|
-
|
|
1
|
+
module lambda
|
|
2
|
-
|
|
2
|
+
|
|
3
|
-
op_ne(x: bool, y: bool) = x != y
|
|
4
|
-
op_and(x: bool, y: bool) = x && y
|
|
5
|
-
op_or(x: bool, y: bool) = x || y
|
|
6
|
-
op_xor(x: bool, y: bool) = x <> y
|
|
7
|
-
op_not(x: bool,): bool = !x
|
|
8
|
-
str(x: bool) = x.str()
|
|
9
|
-
|
|
10
|
-
list = (first | link(v: a, rest: list))
|
|
11
|
-
|
|
3
|
+
alias MapCallback = fn(v: a): v
|
|
12
|
-
match l
|
|
13
|
-
first => first
|
|
14
|
-
link(v, rest) =>
|
|
15
|
-
cb(v)
|
|
16
|
-
each(rest, cb)
|
|
17
4
|
|
|
5
|
+
enum bool(true | false) =
|
|
18
|
-
|
|
6
|
+
fn op_eq(x: bool, y: bool) = x == y
|
|
19
|
-
match l
|
|
20
|
-
|
|
7
|
+
fn op_ne(x: bool, y: bool) = x != y
|
|
21
|
-
|
|
8
|
+
fn op_and(x: bool, y: bool) = x && y
|
|
9
|
+
fn op_or(x: bool, y: bool) = x || y
|
|
10
|
+
fn op_xor(x: bool, y: bool) = x <> y
|
|
11
|
+
fn op_not(x: bool,): bool = !x
|
|
12
|
+
fn str(x: bool) = x ? "true" : "false"
|
|
22
13
|
|
|
23
|
-
enum List<a>
|
|
24
|
-
first
|
|
25
|
-
link(v: a, rest: List)
|
|
26
14
|
|
|
15
|
+
enum List<a>(First | Link(v: a, rest: List)) =
|
|
27
|
-
fn each(
|
|
16
|
+
fn each(self, cb: fn(v: a)) =
|
|
28
|
-
match
|
|
17
|
+
match self
|
|
29
|
-
|
|
18
|
+
List::First -> List::First
|
|
30
|
-
link(v, rest)
|
|
19
|
+
List::link(v, rest) ->
|
|
31
20
|
cb(v)
|
|
32
21
|
each(rest, cb)
|
|
33
22
|
|
|
34
|
-
fn map(
|
|
23
|
+
fn map(self, cb: fn(v: a): v) =
|
|
35
|
-
match
|
|
24
|
+
match self
|
|
36
|
-
|
|
25
|
+
First -> First
|
|
37
|
-
|
|
26
|
+
Link(v, rest) -> List::Link(cb(v), map(rest, cb))
|
|
38
27
|
|
|
39
|
-
|
|
28
|
+
enum Temperature(Celsius(float) | Fahrenheit(float)) =
|
|
40
|
-
fullname(c: Cat) =
|
|
41
|
-
c.name + c.age.str()
|
|
42
|
-
|
|
29
|
+
fn str(self) =
|
|
30
|
+
match self
|
|
31
|
+
Temperature::Celsius(t) && t > 30 -> "${t}C is above 30 Celsius"
|
|
43
|
-
|
|
32
|
+
Temperature::Celsius(t) -> "${t}C is below 30 Celsius"
|
|
33
|
+
Temperature::Fahrenheit(t) && t > 86 -> "${t}F is above 86 Fahrenheit"
|
|
34
|
+
Temperature::Fahrenheit(t) -> "${t}F is below 86 Fahrenheit"
|
|
44
35
|
|
|
45
|
-
struct Cat(
|
|
36
|
+
struct Cat(name: str, age: int) =
|
|
46
|
-
name: str
|
|
47
|
-
age: int
|
|
48
|
-
fullname(
|
|
37
|
+
fn fullname() =
|
|
49
|
-
|
|
38
|
+
name + age.str()
|
|
50
|
-
talk(c: Cat) =
|
|
51
|
-
println("cat ${c.name} says meow")
|
|
52
39
|
|
|
53
|
-
c = Cat(name = "123", age = 1)
|
|
54
|
-
|
|
40
|
+
fn talk() =
|
|
55
|
-
|
|
41
|
+
println("cat ${name} says meow")
|
|
56
42
|
|
|
57
|
-
|
|
43
|
+
test "Cat" =
|
|
58
|
-
fib(n: int = 1) = 1
|
|
59
|
-
|
|
44
|
+
c = Cat(name = "123", age = 1)
|
|
45
|
+
c.talk()
|
|
46
|
+
Cat("rabby", 21).fullname() == "rabby21"
|
|
60
47
|
|
|
61
|
-
|
|
48
|
+
fn fib(n: int): int =
|
|
49
|
+
match n
|
|
50
|
+
0 | 1 -> n
|
|
62
|
-
|
|
51
|
+
else -> fib(n - 1) + fib(n - 2)
|
|
63
52
|
|
|
53
|
+
fn factorial(n: int): int =
|
|
54
|
+
match n
|
|
55
|
+
1 -> 1
|
|
64
|
-
|
|
56
|
+
_ -> n * factorial(n - 1)
|
|
65
57
|
|
|
58
|
+
fn to-celsius(f: float) =
|
|
59
|
+
(f - 32) * (5 / 9)
|
|
60
|
+
|
|
66
|
-
repeat(n: int, cb: fn(int)) =
|
|
61
|
+
fn repeat(n: int, cb: fn(int)) =
|
|
67
62
|
if n != 0:
|
|
68
63
|
cb(n)
|
|
69
64
|
repeat(n-1, cb)
|
|
70
65
|
|
|
71
|
-
range(start: int, e: int, cb: fn(int)) =
|
|
66
|
+
fn range(start: int, e: int, cb: fn(int)) =
|
|
72
67
|
if (start < end):
|
|
73
68
|
cb(start)
|
|
74
69
|
range(start + 1, end, cb)
|
|
@@ -76,8 +71,41 @@ range(start: int, e: int, cb: fn(int)) =
|
|
|
76
71
|
cb(start)
|
|
77
72
|
range(start - 1, end, cb)
|
|
78
73
|
|
|
79
|
-
main() =
|
|
74
|
+
fn main() =
|
|
80
|
-
repeat(10) |i|
|
|
75
|
+
repeat(10) |i|
|
|
76
|
+
println(i)
|
|
77
|
+
range(10, 20) |i|
|
|
81
78
|
println(i)
|
|
79
|
+
|
|
80
|
+
/// Http similar to rocket
|
|
81
|
+
/// GET,DELETE Request -> fn params are got from query
|
|
82
|
+
/// POST,PUT,PATCH Request -> fn params got from multi part form body
|
|
83
|
+
/// DI at compiler level
|
|
84
|
+
/// Controller similar to IHP
|
|
85
|
+
|
|
86
|
+
#[path("/counters")]
|
|
87
|
+
@path("/counters")
|
|
88
|
+
struct CounterController(db: DB)
|
|
89
|
+
static path = "/counters"
|
|
90
|
+
|
|
91
|
+
fn all(): HttpResponse =
|
|
92
|
+
counters := db.query(Counter).fetchAll()
|
|
82
|
-
|
|
93
|
+
render(CounterList(counters))
|
|
94
|
+
|
|
83
|
-
|
|
95
|
+
fn view() =
|
|
96
|
+
counter := fetchOne(Counter) ?? createOne(Counter)
|
|
97
|
+
if counter := fetchOne(Counter)
|
|
98
|
+
render CounterView(counter)
|
|
99
|
+
else
|
|
100
|
+
counter <- newRecord @Counter |> set #count 0 |> createRecord
|
|
101
|
+
render CounterView(counter)
|
|
102
|
+
|
|
103
|
+
fn increment(id: str) =
|
|
104
|
+
counter <- fetch counterId
|
|
105
|
+
updatedCounter <- counter |> incrementField #count |> updateRecord
|
|
106
|
+
respondHtml $ counterView updatedCounter
|
|
107
|
+
|
|
108
|
+
fn decrement(id: str) =
|
|
109
|
+
counter <- fetch counterId
|
|
110
|
+
updatedCounter <- counter |> decrementField #count |> updateRecord
|
|
111
|
+
respondHtml $ counterView updatedCounter
|