~repos /plum

#treesitter#compiler#wasm

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
Files changed (1) hide show
  1. example/fn.palm +86 -58
example/fn.palm CHANGED
@@ -1,74 +1,69 @@
1
- bool = (true | false)
1
+ module lambda
2
- op_eq(x: bool, y: bool) = x == y
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
- each(l: list, cb: (v: a)) =
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
- map(l: list, cb: (v: a): v) =
6
+ fn op_eq(x: bool, y: bool) = x == y
19
- match l
20
- first => first
7
+ fn op_ne(x: bool, y: bool) = x != y
21
- link(v, rest) => link(cb(v), map(rest, cb))
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(l: list, cb: fn(v: a)) =
16
+ fn each(self, cb: fn(v: a)) =
28
- match l
17
+ match self
29
- first => first
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(l: list, cb: fn(v: a): v) =
23
+ fn map(self, cb: fn(v: a): v) =
35
- match l
24
+ match self
36
- first => first
25
+ First -> First
37
- link(v, rest) => link(cb(v), map(rest, cb))
26
+ Link(v, rest) -> List::Link(cb(v), map(rest, cb))
38
27
 
39
- Cat = (name: str, age: int)
28
+ enum Temperature(Celsius(float) | Fahrenheit(float)) =
40
- fullname(c: Cat) =
41
- c.name + c.age.str()
42
- talk(c: Cat) =
29
+ fn str(self) =
30
+ match self
31
+ Temperature::Celsius(t) && t > 30 -> "${t}C is above 30 Celsius"
43
- println("cat ${c.name} says meow")
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(a, b)
36
+ struct Cat(name: str, age: int) =
46
- name: str
47
- age: int
48
- fullname(c: Cat) =
37
+ fn fullname() =
49
- c.name + c.age.str()
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
- c |> Cat.talk()
40
+ fn talk() =
55
- [1, 2, 3] |> list.each(println)
41
+ println("cat ${name} says meow")
56
42
 
57
- fib(n: int = 0) = 0
43
+ test "Cat" =
58
- fib(n: int = 1) = 1
59
- fib(n: int) = fib(n - 1) + fib(n - 2)
44
+ c = Cat(name = "123", age = 1)
45
+ c.talk()
46
+ Cat("rabby", 21).fullname() == "rabby21"
60
47
 
61
- factorial(n: int) = 1
48
+ fn fib(n: int): int =
49
+ match n
50
+ 0 | 1 -> n
62
- factorial(n: int) = n * factorial(n - 1)
51
+ else -> fib(n - 1) + fib(n - 2)
63
52
 
53
+ fn factorial(n: int): int =
54
+ match n
55
+ 1 -> 1
64
- to-celsius(f: float) = (f - 32) * (5 / 9)
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
- range(10, 20) |i|:
93
+ render(CounterList(counters))
94
+
83
- println(i)
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