feat(system/io): add finally combinator
This commit is contained in:
parent
1b8533130b
commit
c4aef89296
1 changed files with 8 additions and 0 deletions
|
|
@ -100,6 +100,14 @@ iterate () $ λ _, a >> return (some ())
|
|||
def catch {e₁ e₂ α} (a : io_core e₁ α) (b : e₁ → io_core e₂ α) : io_core e₂ α :=
|
||||
interface.catch e₁ e₂ α a b
|
||||
|
||||
def finally {α e} (a : io_core e α) (cleanup : io_core e unit) : io_core e α := do
|
||||
res ← catch (sum.inr <$> a) (return ∘ sum.inl),
|
||||
cleanup,
|
||||
match res with
|
||||
| sum.inr res := return res
|
||||
| sum.inl error := io.interface.fail _ _ error
|
||||
end
|
||||
|
||||
instance : alternative io :=
|
||||
{ interface.monad _ with
|
||||
orelse := λ _ a b, catch a (λ _, b),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue