louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#121 · main · 16 min 23 s
0Étoiles
0Forks
40Issues
| 1 | // A tuple pattern inside a constructor pattern, when a later field of |
| 2 | // the same constructor is tested too (`[(tag, n)]`: the head's tuple, |
| 3 | // then the tail is `[]`): the simplifier sinks the projection of `tag` |
| 4 | // into the branch that knows the tail, and must keep the tuple's `let` |
| 5 | // it reads (issue #433). It was dropped as dead: the interpreter |
| 6 | // panicked reading an unbound variable, native code segfaulted. |
| 7 | |
| 8 | import std/io |
| 9 | import std/list |
| 10 | |
| 11 | type Pair { Pair(left: (String, Int), right: Bool) } |
| 12 | |
| 13 | fn only :: points: List<(String, Int)> -> Option<String> |
| 14 | { |
| 15 | match list.reverse points { [(tag, n)] -> Some(tag), _ -> None } |
| 16 | } |
| 17 | |
| 18 | fn leader :: points: List<(String, Int)> threshold: Int -> Option<String> |
| 19 | { |
| 20 | let best = |
| 21 | list.fold |
| 22 | points |
| 23 | 0 |
| 24 | fn m p |
| 25 | { |
| 26 | if p.1 > m |
| 27 | { |
| 28 | p.1 |
| 29 | } |
| 30 | else |
| 31 | { |
| 32 | m |
| 33 | } |
| 34 | } |
| 35 | match list.filter points fn p { p.1 == best } |
| 36 | { [(tag, n)] if n >= threshold -> Some(tag), _ -> None } |
| 37 | } |
| 38 | |
| 39 | fn post :: posts: List<(String, List<(String, String)>, String)> -> String |
| 40 | { |
| 41 | match posts |
| 42 | { [(url, headers, body)] -> |
| 43 | url <> " " <> body <> " " <> show (list.length headers) |
| 44 | , other -> "posts: " <> show (list.length other) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | fn inner :: p: Pair -> String |
| 49 | { |
| 50 | match p { Pair((a, b), True) -> a <> show b, _ -> "no" } |
| 51 | } |
| 52 | |
| 53 | pub fn main :: -> () / <io> |
| 54 | { |
| 55 | io.println (show (only [("MRD", 4)])) |
| 56 | //> Some(MRD) |
| 57 | io.println (show (only [("MRD", 4), ("ORE", 1)])) |
| 58 | //> None |
| 59 | io.println (show (leader [("MRD", 12), ("ORE", 4)] 10)) |
| 60 | //> Some(MRD) |
| 61 | io.println (show (leader [("MRD", 4), ("ORE", 1)] 10)) |
| 62 | //> None |
| 63 | io.println (post [("https://a", [("k", "v")], "body")]) |
| 64 | //> https://a body 1 |
| 65 | io.println (post []) |
| 66 | //> posts: 0 |
| 67 | io.println (inner (Pair(("x", 2), True))) |
| 68 | //> x2 |
| 69 | io.println (inner (Pair(("x", 2), False))) |
| 70 | //> no |
| 71 | } |