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 | // `<>` is `Semigroup`'s `append` (ADR 0027): Strings, Lists, Bytes and |
| 2 | // Order in the prelude, generic functions, and a user impl. |
| 3 | |
| 4 | import std/bytes |
| 5 | import std/io |
| 6 | import std/list |
| 7 | |
| 8 | type Name { Name(first: String, last: String) } |
| 9 | |
| 10 | // Order breaks ties: by last name, then first name. |
| 11 | fn by_name :: a: Name b: Name -> Order |
| 12 | { |
| 13 | compare a.last b.last <> compare a.first b.first |
| 14 | } |
| 15 | |
| 16 | // Inferred: `fn(a) -> a where Semigroup<a>`. |
| 17 | fn twice :: x |
| 18 | { |
| 19 | x <> x |
| 20 | } |
| 21 | |
| 22 | fn join_all :: xs: List<a> empty: a -> a where Semigroup<a> |
| 23 | { |
| 24 | list.fold xs empty fn acc x { acc <> x } |
| 25 | } |
| 26 | |
| 27 | type Path { Path(List<String>) } |
| 28 | |
| 29 | impl Semigroup<Path> |
| 30 | { |
| 31 | fn append :: a b |
| 32 | { |
| 33 | match (a, b) { (Path(x), Path(y)) -> Path(x <> y) } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn order_text :: o: Order -> String |
| 38 | { |
| 39 | match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" } |
| 40 | } |
| 41 | |
| 42 | pub fn main :: -> () / <io> |
| 43 | { |
| 44 | io.println ("a" <> "b" <> "c") |
| 45 | io.println (show ([1, 2] <> [3] <> [])) |
| 46 | io.println (show ([] <> [4])) |
| 47 | io.println (debug (bytes.from_string "ab" <> bytes.from_string "cd")) |
| 48 | io.println (twice "na" <> " " <> show (twice [1])) |
| 49 | io.println (join_all ["x", "y", "z"] "") |
| 50 | io.println (show (join_all [[1], [2, 3]] [])) |
| 51 | io.println (order_text (by_name Name("Ada", "Lovelace") Name("Ada", "Byron"))) |
| 52 | io.println (order_text (by_name Name("Ada", "Byron") Name("Bo", "Byron"))) |
| 53 | io.println (order_text (Eq <> Eq)) |
| 54 | match Path(["usr"]) <> Path(["lib"]) { Path(p) -> io.println (show p) } |
| 55 | // The prelude's `append` is the method; `list.append` is unaffected. |
| 56 | io.println (show (append [1] [2] == list.append [1] [2])) |
| 57 | } |
| 58 | //> abc |
| 59 | //> [1, 2, 3] |
| 60 | //> [4] |
| 61 | //> b"abcd" |
| 62 | //> nana [1, 1] |
| 63 | //> xyz |
| 64 | //> [1, 2, 3] |
| 65 | //> Gt |
| 66 | //> Lt |
| 67 | //> Eq |
| 68 | //> [usr, lib] |
| 69 | //> True |