tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

29 lignes · 809 octetsBrut · Historique
1// A map/filter/fold pipeline over a million integers, ten times (each
2// round a little longer, so that no compiler computes it only once).
3import gleam/int
4import gleam/io
5import gleam/list
6
7// `gleam/list` has no `range`: build `[from, …, to]` (tail-recursive).
8fn range(from: Int, to: Int, acc: List(Int)) -> List(Int) {
9 case from > to {
10 True -> acc
11 False -> range(from, to - 1, [to, ..acc])
12 }
13}
14
15fn round(k: Int, acc: Int) -> Int {
16 case k == 0 {
17 True -> acc
18 False -> {
19 let doubled = list.map(range(1, 1_000_000 + k, []), fn(x) { x * 2 })
20 let kept = list.filter(doubled, fn(x) { int.remainder(x, 3) == Ok(0) })
21 let sum = list.fold(kept, 0, fn(a, x) { a + x })
22 round(k - 1, acc + sum)
23 }
24 }
25}
26
27pub fn main() {
28 io.println(int.to_string(round(10, 0)))
29}