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 = value.map f |> Atom 13 let token = subscribe { _ maybe_x -> 14 let maybe_prev_value = atom.value 15 let maybe_value = maybe_x.map f 16 atom.set maybe_value 17 if maybe_prev_value ? Some prev_value then 18 g prev_value 19 20 @[weak atom] } 21 22 atom.push_token token 23 24 atom.as_readonly 25 26 def option_map_discard<V> (f : U -> V) where V : Discard = 27 option_map_revert<V> f { _.discard } 28 29 def option_map<V> (f : U -> V) = option_map_revert<V> f {} 30 31 def unwrap_or self (value : U) = 32 self.map { maybe_x -> case maybe_x of 33 Some x -> x 34 None -> value } 35