tobogganSe connecter

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.

69 lignes · 1675 octetsBrut · Historique
1// `<>` is `Semigroup`'s `append` (ADR 0027): Strings, Lists, Bytes and
2// Order in the prelude, generic functions, and a user impl.
3
4import std/bytes
5import std/io
6import std/list
7
8type Name { Name(first: String, last: String) }
9
10// Order breaks ties: by last name, then first name.
11fn 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>`.
17fn twice :: x
18{
19 x <> x
20}
21
22fn join_all :: xs: List<a> empty: a -> a where Semigroup<a>
23{
24 list.fold xs empty fn acc x { acc <> x }
25}
26
27type Path { Path(List<String>) }
28
29impl Semigroup<Path>
30{
31 fn append :: a b
32 {
33 match (a, b) { (Path(x), Path(y)) -> Path(x <> y) }
34 }
35}
36
37fn order_text :: o: Order -> String
38{
39 match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" }
40}
41
42pub 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