// `<>` is `Semigroup`'s `append` (ADR 0027): Strings, Lists, Bytes and // Order in the prelude, generic functions, and a user impl. import std/bytes import std/io import std/list type Name { Name(first: String, last: String) } // Order breaks ties: by last name, then first name. fn by_name :: a: Name b: Name -> Order { compare a.last b.last <> compare a.first b.first } // Inferred: `fn(a) -> a where Semigroup`. fn twice :: x { x <> x } fn join_all :: xs: List empty: a -> a where Semigroup { list.fold xs empty fn acc x { acc <> x } } type Path { Path(List) } impl Semigroup { fn append :: a b { match (a, b) { (Path(x), Path(y)) -> Path(x <> y) } } } fn order_text :: o: Order -> String { match o { Lt -> "Lt", Eq -> "Eq", Gt -> "Gt" } } pub fn main :: -> () / { io.println ("a" <> "b" <> "c") io.println (show ([1, 2] <> [3] <> [])) io.println (show ([] <> [4])) io.println (debug (bytes.from_string "ab" <> bytes.from_string "cd")) io.println (twice "na" <> " " <> show (twice [1])) io.println (join_all ["x", "y", "z"] "") io.println (show (join_all [[1], [2, 3]] [])) io.println (order_text (by_name Name("Ada", "Lovelace") Name("Ada", "Byron"))) io.println (order_text (by_name Name("Ada", "Byron") Name("Bo", "Byron"))) io.println (order_text (Eq <> Eq)) match Path(["usr"]) <> Path(["lib"]) { Path(p) -> io.println (show p) } // The prelude's `append` is the method; `list.append` is unaffected. io.println (show (append [1] [2] == list.append [1] [2])) } //> abc //> [1, 2, 3] //> [4] //> b"abcd" //> nana [1, 1] //> xyz //> [1, 2, 3] //> Gt //> Lt //> Eq //> [usr, lib] //> True