// A `map` left in the middle (ADR 0039): the part of the result built so // far is released, whether a panic, a `final ctl` or a kill leaves it // (natively: 0 live blocks at the end). import std/io import std/list import std/process.{type proc, Down} effect stop { final ctl stop :: at: Int -> Never } /// `xs` mapped until an element is 500: then `stop` leaves the map. fn until_500 :: xs: List -> Int { handle { list.length ( list.map xs fn x { if x == 500 { stop x } else { [x, x] } } ) } with { stop at -> -at } } pub fn main :: -> () / { let xs = list.range 1 1000 io.println "{until_500 xs} {until_500 (list.range 1 10)}" // A panic in the mapper crashes the actor; its context is released. let (_, down) = process.spawn_monitor fn { let mapped = list.map (list.range 1 1000) fn x { if x == 700 { panic "mapper gave up at {x}" } else { "{x}" } } io.println "never printed {list.length mapped}" } match process.receive down 1000 { Some(Down(_, reason)) -> io.println "{reason}" , None -> io.println "no answer" } // Killed while mapping: the actor waits in the mapper, at element 300. let started = process.subject () let (pid, killed) = process.spawn_monitor fn { let mapped = list.map (list.range 1 1000) fn x { if x == 300 { process.send started () let _ = process.receive (process.subject ()) (-1) } x * 2 } io.println "never printed {list.length mapped}" } let _ = process.receive started 1000 process.kill pid match process.receive killed 1000 { Some(Down(_, reason)) -> io.println "{reason}" , None -> io.println "no answer" } } //> -500 10 //> Crashed(mapper gave up at 700) //> Killed