Skip to content

Commit f89819b

Browse files
committed
Added Gleam Triple
1 parent 68aaf15 commit f89819b

File tree

4 files changed

+71
-3
lines changed

4 files changed

+71
-3
lines changed

gleam/src/triple.gleam

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import gleam/list
2+
import gleam/int
3+
import gleam/io
4+
import gleam/string
5+
6+
pub fn main() {
7+
let print_triple = fn(triple: List(Int)) {
8+
triple
9+
|> list.map(int.to_string)
10+
|> string.join(", ")
11+
|> io.println
12+
}
13+
14+
list.range(1, 40) |> list.each( fn(c) {
15+
list.range(1, c) |> list.each( fn(b) {
16+
list.range(1, b) |> list.each( fn(a) {
17+
case {a * a + b * b == c * c} {
18+
True -> print_triple([a, b, c])
19+
False -> Nil
20+
}
21+
})
22+
})
23+
})
24+
}

gleam/src/triple_pipelines.gleam

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import gleam/float
2+
import gleam/list
3+
import gleam/int
4+
import gleam/io
5+
import gleam/result
6+
import gleam/string
7+
8+
pub fn main() {
9+
10+
let valid_triple = fn(triple: List(Int)) {
11+
let target = triple
12+
|> list.last
13+
|> result.unwrap(0)
14+
|> int.to_float
15+
16+
triple
17+
|> list.map(fn(n) { n * n })
18+
|> int.sum
19+
|> int.to_float
20+
|> float.divide(by: 2.0)
21+
|> result.unwrap(0.0)
22+
|> float.square_root
23+
|> result.unwrap(0.0)
24+
|> float.loosely_equals(with: target, tolerating: 0.0)
25+
}
26+
27+
let print_triple = fn(triple: List(Int)) {
28+
triple
29+
|> list.map(int.to_string)
30+
|> string.join(", ")
31+
|> io.println
32+
}
33+
34+
list.range(1, 40)
35+
|> list.combinations(3)
36+
|> list.filter(valid_triple)
37+
|> list.each(print_triple)
38+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import gleam/io
1+
import gleam/io.{println}
22

33
pub fn main() {
4-
io.println("Hello Sandra!")
5-
io.println("Hello to you too, Bob!")
4+
println("Hello Sandra!")
5+
println("Hello to you too, Bob!")
66
}

gleam/test.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ gleam run -m factorial &&
2020
gleam run -m generics &&
2121
gleam run -m hello &&
2222
gleam run -m pipelines &&
23+
gleam run -m triple_pipelines |
24+
Compare-Object (Get-Content "$PSScriptRoot\..\test\triple_expected") |
25+
Assert-MatchTests &&
26+
gleam run -m triple |
27+
Compare-Object (Get-Content "$PSScriptRoot\..\test\triple_expected") |
28+
Assert-MatchTests &&
2329
gleam run -m unqualified_imports &&
2430
ForEach-Object 'foo';
2531

0 commit comments

Comments
 (0)