Skip to content

Commit fbc8b93

Browse files
committed
CHUTIL: expand API for traceresult
1 parent 7d5372d commit fbc8b93

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

CodeHawk/CH/chutil/cHTraceResult.ml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,26 @@ let tmap ?(msg="") (f: 'a -> 'c) (r: 'a traceresult) =
4848
| Error e -> Error (msg :: e)
4949

5050

51+
let tmap2
52+
?(msg1="")
53+
?(msg2="")
54+
(f: 'a -> 'b -> 'c)
55+
(r1: 'a traceresult)
56+
(r2: 'b traceresult): 'c traceresult =
57+
match r1, r2 with
58+
| Ok v1, Ok v2 -> Ok (f v1 v2)
59+
| Error e1, Ok _ -> Error (msg1 :: e1)
60+
| Ok _, Error e2 -> Error (msg2 :: e2)
61+
| Error e1, Error e2 -> Error (msg1 :: msg2 :: (e1 @ e2))
62+
63+
5164
let tbind ?(msg="") (f: 'a -> 'c traceresult) (r: 'a traceresult) =
5265
match r with
5366
| Ok v -> f v
5467
| Error e when msg = "" -> Error e
5568
| Error e -> Error (msg :: e)
5669

70+
5771
let tfold ~(ok:'a -> 'c) ~(error:string list -> 'c) (r: 'a traceresult): 'c =
5872
match r with
5973
| Ok v -> ok v
@@ -96,6 +110,19 @@ let tfold_list_default
96110
| Error _ -> err acc) initacc rl
97111

98112

113+
let tfold_list_fail
114+
(ok: 'c -> 'a -> 'c)
115+
(initacc: 'c traceresult)
116+
(rl: ('a traceresult) list): 'c traceresult =
117+
List.fold_left (fun acc r ->
118+
match acc with
119+
| Error e -> Error e
120+
| Ok accv ->
121+
match r with
122+
| Error e -> Error e
123+
| Ok v -> Ok (ok accv v)) initacc rl
124+
125+
99126
let to_bool (f: 'a -> bool) (r: 'a traceresult) =
100127
match r with
101128
| Ok v -> f v

CodeHawk/CH/chutil/cHTraceResult.mli

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ val tvalue: 'a traceresult -> default:'a -> 'a
5353
val tmap: ?msg:string -> ('a -> 'c) -> ('a traceresult) -> 'c traceresult
5454

5555

56+
(** [tmap2 f r1 r2] is [Ok (f v1 v2)] if [r1] is [Ok v1] and [r2] is [Ok v2];
57+
otherwise it returns an [Error] appending the messages corresponding to
58+
the error value as appropriate.*)
59+
val tmap2:
60+
?msg1: string
61+
-> ?msg2: string
62+
-> ('a -> 'b -> 'c)
63+
-> 'a traceresult
64+
-> 'b traceresult
65+
-> 'c traceresult
66+
67+
5668
(** [tfold ~ok ~error r] is [ok v] if [r] is [Ok v] and [error e] if [r] is
5769
[Error e].*)
5870
val tfold: ok:('a -> 'c) -> error:(string list -> 'c) -> 'a traceresult -> 'c
@@ -84,7 +96,7 @@ val tfold_list: ok:('c -> 'a -> 'c) -> 'c -> ('a traceresult) list -> 'c
8496

8597

8698
(** [tfold_list_default ~ok ~err init rl] folds [Ok] values left to right,
87-
startint from [init], using a default accumulator [err] for [Error]
99+
starting from [init], using a default accumulator [err] for [Error]
88100
values.
89101
90102
This function differs from [tfold_list] in that it enables making the
@@ -93,6 +105,13 @@ val tfold_list_default:
93105
ok:('c -> 'a -> 'c) -> err:('c -> 'c) -> 'c -> ('a traceresult) list -> 'c
94106

95107

108+
(** [tfold_list_fail f init rl] folds [Ok] values left to right starting
109+
from [init], failing on the first value in [rl] that has an error value.
110+
*)
111+
val tfold_list_fail:
112+
('c -> 'a -> 'c) -> 'c traceresult -> ('a traceresult) list -> 'c traceresult
113+
114+
96115
(** [to_bool f r] is [f v] if [r] is [Ok v] and [false] otherwise.*)
97116
val to_bool: ('a -> bool) -> 'a traceresult -> bool
98117

0 commit comments

Comments
 (0)