1 type<U> Atom<Option<U>> 2 def as_safe_slice = case value of 3 Some x@ptr -> SafeSlice<U> x 1 version@ptr version 4 else -> SafeSlice<U>.empty 5 6 def option_bind_revert (f : U -> Unit) (g : U -> Unit) = 7 bind_revert { if _ ? Some v then f v } 8 { if _ ? Some v then g v } 9 10 def option_map_revert<V> (f : U -> V 11 g : V -> Unit) = 12 let atom = 13 let maybe_value = case value of 14 None -> None 15 Some x -> f x |> Some 16 17 Atom<Option<V>> maybe_value 18 19 let token = subscribe { _ maybe_x -> 20 let maybe_prev_value = atom.value 21 let maybe_value = case maybe_x of 22 None -> None 23 Some x -> f x |> Some 24 25 atom.set maybe_value 26 if maybe_prev_value ? Some prev_value then 27 g prev_value 28 29 @[weak atom] } 30 31 atom.push_token token 32 33 atom.as_readonly 34 35 def option_map_discard<V> (f : U -> V) where V : Discard = 36 option_map_revert<V> f { _.discard } 37 38 def option_map<V> (f : U -> V) = option_map_revert<V> f {} 39 40 def unwrap_or self (value : U) = 41 self.map { maybe_x -> case maybe_x of 42 Some x -> x 43 None -> value } 44