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.

99 lignes · 2221 octetsBrut · Historique
1// A `map` left in the middle (ADR 0039): the part of the result built so
2// far is released, whether a panic, a `final ctl` or a kill leaves it
3// (natively: 0 live blocks at the end).
4
5import std/io
6import std/list
7import std/process.{type proc, Down}
8
9effect stop
10{
11 final ctl stop :: at: Int -> Never
12}
13
14/// `xs` mapped until an element is 500: then `stop` leaves the map.
15fn until_500 :: xs: List<Int> -> Int
16{
17 handle
18 {
19 list.length
20 ( list.map
21 xs
22 fn x
23 {
24 if x == 500
25 {
26 stop x
27 }
28 else
29 {
30 [x, x]
31 }
32 }
33 )
34 }
35 with
36 { stop at -> -at }
37}
38
39pub fn main :: -> () / <io, proc>
40{
41 let xs = list.range 1 1000
42 io.println "{until_500 xs} {until_500 (list.range 1 10)}"
43
44 // A panic in the mapper crashes the actor; its context is released.
45 let (_, down) =
46 process.spawn_monitor
47 fn
48 {
49 let mapped =
50 list.map
51 (list.range 1 1000)
52 fn x
53 {
54 if x == 700
55 {
56 panic "mapper gave up at {x}"
57 }
58 else
59 {
60 "{x}"
61 }
62 }
63 io.println "never printed {list.length mapped}"
64 }
65 match process.receive down 1000
66 { Some(Down(_, reason)) -> io.println "{reason}"
67 , None -> io.println "no answer"
68 }
69
70 // Killed while mapping: the actor waits in the mapper, at element 300.
71 let started = process.subject ()
72 let (pid, killed) =
73 process.spawn_monitor
74 fn
75 {
76 let mapped =
77 list.map
78 (list.range 1 1000)
79 fn x
80 {
81 if x == 300
82 {
83 process.send started ()
84 let _ = process.receive (process.subject ()) (-1)
85 }
86 x * 2
87 }
88 io.println "never printed {list.length mapped}"
89 }
90 let _ = process.receive started 1000
91 process.kill pid
92 match process.receive killed 1000
93 { Some(Down(_, reason)) -> io.println "{reason}"
94 , None -> io.println "no answer"
95 }
96}
97//> -500 10
98//> Crashed(mapper gave up at 700)
99//> Killed