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#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // `std/list`: sort and sort_by (stability, a custom and an effectful |
| 2 | // comparator), take, drop and find, on their edge cases. |
| 3 | |
| 4 | import std/io |
| 5 | import std/list |
| 6 | import std/string |
| 7 | |
| 8 | pub fn main :: -> () / <io> |
| 9 | { |
| 10 | let xs = [3, 1, 4, 1, 5, 9, 2, 6] |
| 11 | let empty: List<Int> = [] |
| 12 | io.println (show (list.sort xs)) |
| 13 | io.println (show (list.sort ["pear", "fig", "apple"])) |
| 14 | io.println (show (list.sort empty)) |
| 15 | io.println (show (list.sort [2])) |
| 16 | // Descending, with a custom comparator. |
| 17 | io.println (show (list.sort_by xs fn a b { compare b a })) |
| 18 | // Stable: the ties on the key keep their original relative order. |
| 19 | let pairs = [(1, "a"), (2, "b"), (1, "c"), (3, "d"), (2, "e"), (1, "f")] |
| 20 | io.println (show (list.sort_by pairs fn x y { compare x.0 y.0 })) |
| 21 | // The comparator may perform effects. |
| 22 | let traced = |
| 23 | list.sort_by |
| 24 | [2, 1, 3] |
| 25 | fn a b |
| 26 | { |
| 27 | io.println ("cmp " <> show a <> " " <> show b) |
| 28 | compare a b |
| 29 | } |
| 30 | io.println (show traced) |
| 31 | // take and drop: 0, negative, past the end, the empty list. |
| 32 | io.println |
| 33 | (show [list.take xs 0, list.take xs (-2), list.take xs 3, list.take xs 100]) |
| 34 | io.println (show (list.take empty 3)) |
| 35 | io.println |
| 36 | ( show |
| 37 | [ list.drop xs 0 |
| 38 | , list.drop xs (-1) |
| 39 | , list.drop xs 6 |
| 40 | , list.drop xs 8 |
| 41 | , list.drop xs 9 |
| 42 | ] |
| 43 | ) |
| 44 | io.println (show (list.drop empty 2)) |
| 45 | io.println (show (list.take xs 3 <> list.drop xs 3)) |
| 46 | // find: a hit, a miss, the empty list. |
| 47 | io.println (show (list.find xs fn x { x % 2 == 0 })) |
| 48 | io.println (show (list.find [1, 3] fn x { x % 2 == 0 })) |
| 49 | io.println (show (list.find empty fn x { x == 0 })) |
| 50 | io.println (show (list.find ["", "a", "bc"] fn s { string.length s > 1 })) |
| 51 | } |
| 52 | //> [1, 1, 2, 3, 4, 5, 6, 9] |
| 53 | //> [apple, fig, pear] |
| 54 | //> [] |
| 55 | //> [2] |
| 56 | //> [9, 6, 5, 4, 3, 2, 1, 1] |
| 57 | //> [(1, a), (1, c), (1, f), (2, b), (2, e), (3, d)] |
| 58 | //> cmp 1 2 |
| 59 | //> cmp 3 2 |
| 60 | //> [1, 2, 3] |
| 61 | //> [[], [], [3, 1, 4], [3, 1, 4, 1, 5, 9, 2, 6]] |
| 62 | //> [] |
| 63 | //> [[3, 1, 4, 1, 5, 9, 2, 6], [3, 1, 4, 1, 5, 9, 2, 6], [2, 6], [], []] |
| 64 | //> [] |
| 65 | //> [3, 1, 4, 1, 5, 9, 2, 6] |
| 66 | //> Some(4) |
| 67 | //> None |
| 68 | //> None |
| 69 | //> Some(bc) |