mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2026-01-28 03:44:51 -05:00
add CCFun.finally{1,2}, convenience around finally
This commit is contained in:
parent
49991717c1
commit
7dbf3f983b
2 changed files with 32 additions and 4 deletions
|
|
@ -64,10 +64,28 @@ let lexicographic f1 f2 x y =
|
||||||
let finally ~h ~f =
|
let finally ~h ~f =
|
||||||
try
|
try
|
||||||
let x = f () in
|
let x = f () in
|
||||||
h ();
|
ignore (h ());
|
||||||
x
|
x
|
||||||
with e ->
|
with e ->
|
||||||
h ();
|
ignore (h ());
|
||||||
|
raise e
|
||||||
|
|
||||||
|
let finally1 ~h f x =
|
||||||
|
try
|
||||||
|
let res = f x in
|
||||||
|
ignore (h ());
|
||||||
|
res
|
||||||
|
with e ->
|
||||||
|
ignore (h ());
|
||||||
|
raise e
|
||||||
|
|
||||||
|
let finally2 ~h f x y =
|
||||||
|
try
|
||||||
|
let res = f x y in
|
||||||
|
ignore (h ());
|
||||||
|
res
|
||||||
|
with e ->
|
||||||
|
ignore (h ());
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
module Monad(X : sig type t end) = struct
|
module Monad(X : sig type t end) = struct
|
||||||
|
|
|
||||||
|
|
@ -57,7 +57,7 @@ val curry : ('a * 'b -> 'c) -> 'a -> 'b -> 'c
|
||||||
|
|
||||||
val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c
|
val uncurry : ('a -> 'b -> 'c) -> ('a * 'b) -> 'c
|
||||||
|
|
||||||
val tap : ('a -> 'b) -> 'a -> 'a
|
val tap : ('a -> _) -> 'a -> 'a
|
||||||
(** [tap f x] evaluates [f x], discards it, then returns [x]. Useful
|
(** [tap f x] evaluates [f x], discards it, then returns [x]. Useful
|
||||||
in a pipeline, for instance:
|
in a pipeline, for instance:
|
||||||
{[CCArray.(1 -- 10)
|
{[CCArray.(1 -- 10)
|
||||||
|
|
@ -72,11 +72,21 @@ val (%) : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c
|
||||||
val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int
|
val lexicographic : ('a -> 'a -> int) -> ('a -> 'a -> int) -> 'a -> 'a -> int
|
||||||
(** Lexicographic combination of comparison functions *)
|
(** Lexicographic combination of comparison functions *)
|
||||||
|
|
||||||
val finally : h:(unit -> unit) -> f:(unit -> 'a) -> 'a
|
val finally : h:(unit -> _) -> f:(unit -> 'a) -> 'a
|
||||||
(** [finally h f] calls [f ()] and returns its result. If it raises, the
|
(** [finally h f] calls [f ()] and returns its result. If it raises, the
|
||||||
same exception is raised; in {b any} case, [h ()] is called after
|
same exception is raised; in {b any} case, [h ()] is called after
|
||||||
[f ()] terminates. *)
|
[f ()] terminates. *)
|
||||||
|
|
||||||
|
val finally1 : h:(unit -> _) -> ('a -> 'b) -> 'a -> 'b
|
||||||
|
(** [finally1 ~h f x] is the same as [f x], but after the computation,
|
||||||
|
[h ()] is called whether [f x] rose an exception or not.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
|
val finally2 : h:(unit -> _) -> ('a -> 'b -> 'c) -> 'a -> 'b -> 'c
|
||||||
|
(** [finally2 ~h f x y] is the same as [f x y], but after the computation,
|
||||||
|
[h ()] is called whether [f x y] rose an exception or not.
|
||||||
|
@since NEXT_RELEASE *)
|
||||||
|
|
||||||
(** {2 Monad}
|
(** {2 Monad}
|
||||||
|
|
||||||
Functions with a fixed domain are monads in their codomain *)
|
Functions with a fixed domain are monads in their codomain *)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue