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.

52 lignes · 1069 octetsBrut · Historique
1import std/io
2import std/list
3import std/result
4
5fn adder :: n
6{
7 fn x { x + n }
8}
9
10fn compose :: f g
11{
12 fn x { f (g x) }
13}
14
15fn parse_digit :: s
16{
17 match s
18 { "0" -> Ok(0), "1" -> Ok(1), "2" -> Ok(2), _ -> Error("not a digit: " <> s) }
19}
20
21fn add_digits :: a b
22{
23 use x <- result.try (parse_digit a)
24 use y <- result.try (parse_digit b)
25 Ok(x + y)
26}
27
28fn render :: r
29{
30 match r { Ok(v) -> "ok " <> show v, Error(e) -> "error " <> e }
31}
32
33pub fn main :: -> () / <io>
34{
35 let add5 = adder 5
36 let inc_then_double = compose fn x { x * 2 } (adder 1)
37 io.println (show (add5 10) <> " " <> show (inc_then_double 4))
38 // A capture chain through nested lambdas.
39 let base = 100
40 let nested = fn a { fn b { fn c { base + a + b + c } } }
41 io.println (show (((nested 1) 2) 3))
42 io.println
43 (render (add_digits "1" "2") <> ", " <> render (add_digits "1" "x"))
44 let names = ["ada", "grace", "edsger"] |> list.filter fn n { n != "grace" }
45 use n <- list.each names
46 io.println ("hi " <> n)
47}
48//> 15 10
49//> 106
50//> ok 3, error not a digit: x
51//> hi ada
52//> hi edsger