Skip to content

Commit bc39951

Browse files
committed
Added pattern matching and type enum
1 parent 520d800 commit bc39951

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

gleam/src/pattern_matching.gleam

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import gleam/int
2+
3+
// Currently unicode character is not supported in grammar :(
4+
pub type FrenchMonth {
5+
Janvier
6+
Fevrier
7+
Mars
8+
Avril
9+
Mai
10+
Juin
11+
Juillet
12+
Aout
13+
Septembre
14+
Octobre
15+
Novembre
16+
Decembre
17+
}
18+
19+
pub fn main() {
20+
let month1 = Aout
21+
let month2 = Novembre
22+
let month3 = Fevrier
23+
echo "This month most likely has " <> int.to_string(get_total_days(month1)) <> " days."
24+
echo "This month most likely has " <> int.to_string(get_total_days(month2)) <> " days."
25+
echo "This month most likely has " <> int.to_string(get_total_days(month3)) <> " days."
26+
}
27+
28+
pub fn get_total_days(month) {
29+
case month {
30+
Janvier | Mars | Mai | Juillet | Aout | Octobre | Decembre -> 31
31+
Avril | Juin | Septembre | Novembre -> 30
32+
Fevrier -> 28
33+
}
34+
}

gleam/src/type_enum.gleam

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import gleam/float
2+
import gleam/list
3+
import gleam/result
4+
5+
pub type Direction {
6+
North
7+
East
8+
South
9+
West
10+
}
11+
12+
pub fn main() {
13+
let directions = [East, South, South, South, West, North, West]
14+
let displacement = get_displacement(directions)
15+
echo "Your displacement is " <> float.to_string(displacement)
16+
}
17+
18+
pub fn get_displacement(directions) {
19+
let point = directions
20+
|> list.map ( fn(direction) {
21+
case direction {
22+
North -> #(0.0,1.0)
23+
East -> #(1.0,0.0)
24+
South -> #(0.0,-1.0)
25+
West -> #(-1.0,0.0)
26+
}
27+
})
28+
|> list.reduce( fn(p1, p2) {
29+
#(p1.0 +. p2.0, p1.1 +. p2.1)
30+
})
31+
|> result.unwrap(#(0.0,0.0))
32+
33+
float.square_root(point.0 *. point.0 +. point.1 *. point.1)
34+
|> result.unwrap(0.0)
35+
}

gleam/test.ps1

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ gleam run -m clockhands |
2525
gleam run -m factorial &&
2626
gleam run -m generics &&
2727
gleam run -m hello &&
28+
gleam run -m pattern_matching &&
2829
gleam run -m permutations I like carrots |
2930
Compare-Object (Get-Content "$PSScriptRoot\..\test\carrots_expected") |
3031
Assert-MatchTests &&
@@ -36,6 +37,7 @@ gleam run -m triple_pipelines |
3637
gleam run -m triple |
3738
Compare-Object (Get-Content "$PSScriptRoot\..\test\triple_expected") |
3839
Assert-MatchTests &&
40+
gleam run -m type_enum &&
3941
gleam run -m unqualified_imports &&
4042
ForEach-Object 'foo';
4143

0 commit comments

Comments
 (0)