tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

127 lignes · 3055 octetsBrut · Historique
1// The concrete pieces of Functor, Applicative and Alternative in std
2// (issue #95, ADR 0026): list.flat_map, try_map, zip, unzip, zip_with;
3// option and result or, lazy_or, map2. Split into functions: one long
4// `main` overflows the debug compiler's stack (see results.lse).
5
6import std/io
7import std/list
8import std/option
9import std/result
10import std/string
11
12fn halve :: n: Int -> Result<Int, String>
13{
14 if n % 2 == 0
15 {
16 Ok(n / 2)
17 }
18 else
19 {
20 Error(show n <> " is odd")
21 }
22}
23
24fn pairs :: ps: List<(Int, String)> -> String
25{
26 show (list.map ps fn p { "(" <> show p.0 <> ", " <> p.1 <> ")" })
27}
28
29fn lists :: -> () / <io>
30{
31 io.println (show (list.flat_map [1, 2, 3] fn n { list.range 1 n }))
32 io.println (show (list.flat_map [] fn n: Int { [n] }))
33 io.println (show (list.try_map [2, 4, 6] halve))
34 // Stops at the first error: 4 is never visited.
35 let visited =
36 list.try_map
37 [2, 3, 4]
38 fn n
39 {
40 io.println ("visiting " <> show n)
41 halve n
42 }
43 io.println (show visited)
44 io.println (show (list.try_map [] halve))
45 io.println (pairs (list.zip [1, 2, 3] ["a", "b"]))
46 let (numbers, words) = list.unzip [(1, "a"), (2, "b")]
47 io.println (show numbers <> " " <> show words)
48 io.println (show (list.zip_with [1, 2, 3] [10, 20, 30, 40] fn a b { a + b }))
49}
50
51fn options :: -> () / <io>
52{
53 let none: Option<Int> = None
54 io.println (show (option.or Some(1) Some(2)))
55 io.println (show (option.or none Some(2)))
56 io.println (show (option.or none none))
57 // `lazy_or` calls the fallback only for `None`.
58 let fallback = fn
59 {
60 io.println "fallback"
61 Some(9)
62 }
63 io.println (show (option.lazy_or Some(1) fallback))
64 io.println (show (option.lazy_or none fallback))
65 io.println (show (option.map2 Some(2) Some(3) fn a b { a * b }))
66 io.println (show (option.map2 Some(2) none fn a b { a * b }))
67}
68
69fn results :: -> () / <io>
70{
71 io.println (show (result.or (halve 3) (halve 4)))
72 io.println (show (result.or (halve 3) (halve 5)))
73 io.println (show (result.or (halve 2) (halve 5)))
74 io.println
75 ( show
76 ( result.lazy_or
77 (halve 2)
78 fn
79 {
80 io.println "not called"
81 halve 4
82 }
83 )
84 )
85 io.println (show (result.lazy_or (halve 1) fn { halve 4 }))
86 // Two fields validated together: both values, or the first error.
87 let both = fn a: String b: String
88 {
89 result.map2 (string.to_int a) (string.to_int b) fn x y { x + y }
90 }
91 io.println (show (both "1" "2"))
92 io.println (show (both "x" "y"))
93 io.println (show (both "1" "y"))
94}
95
96pub fn main :: -> () / <io>
97{
98 lists ()
99 options ()
100 results ()
101}
102//> [1, 1, 2, 1, 2, 3]
103//> []
104//> Ok([1, 2, 3])
105//> visiting 2
106//> visiting 3
107//> Error(3 is odd)
108//> Ok([])
109//> [(1, a), (2, b)]
110//> [1, 2] [a, b]
111//> [11, 22, 33]
112//> Some(1)
113//> Some(2)
114//> None
115//> Some(1)
116//> fallback
117//> Some(9)
118//> Some(6)
119//> None
120//> Ok(2)
121//> Error(5 is odd)
122//> Ok(1)
123//> Ok(1)
124//> Ok(2)
125//> Ok(3)
126//> Error(not an integer: "x")
127//> Error(not an integer: "y")