ocaml: phase 6 Either module + Hashtbl.copy (+4 tests, 602 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Either module (mirrors OCaml 4.12+ stdlib): left x / right x is_left / is_right find_left / find_right (return Option) map_left / map_right (single-side mappers) fold lf rf e (case dispatch) equal eq_l eq_r a b compare cmp_l cmp_r a b (Left < Right) Constructors are bare 'Left x' / 'Right x' (OCaml 4.12+ exposes them directly without an explicit type-decl). Hashtbl.copy: build a fresh cell with _hashtbl_create walk _hashtbl_to_list and re-add each (k, v) mutating one copy doesn't touch the other (Hashtbl.length t + Hashtbl.length t2 = 3 after fork-and-add verifies that adds to t2 don't appear in t)
This commit is contained in:
@@ -1030,6 +1030,48 @@
|
||||
let remove t k = _hashtbl_remove t k
|
||||
let reset t = _hashtbl_clear t
|
||||
let clear t = _hashtbl_clear t
|
||||
|
||||
let copy t =
|
||||
let t' = _hashtbl_create 8 in
|
||||
List.iter
|
||||
(fun (k, v) -> _hashtbl_add t' k v)
|
||||
(_hashtbl_to_list t);
|
||||
t'
|
||||
end ;;
|
||||
|
||||
module Either = struct
|
||||
let left x = Left x
|
||||
let right x = Right x
|
||||
|
||||
let is_left e = match e with Left _ -> true | Right _ -> false
|
||||
let is_right e = match e with Left _ -> false | Right _ -> true
|
||||
|
||||
let find_left e =
|
||||
match e with Left x -> Some x | Right _ -> None
|
||||
let find_right e =
|
||||
match e with Left _ -> None | Right x -> Some x
|
||||
|
||||
let map_left f e =
|
||||
match e with Left x -> Left (f x) | Right x -> Right x
|
||||
let map_right f e =
|
||||
match e with Left x -> Left x | Right x -> Right (f x)
|
||||
|
||||
let fold lf rf e =
|
||||
match e with Left x -> lf x | Right x -> rf x
|
||||
|
||||
let equal eq_l eq_r a b =
|
||||
match a with
|
||||
| Left x ->
|
||||
(match b with Left y -> eq_l x y | Right _ -> false)
|
||||
| Right x ->
|
||||
(match b with Left _ -> false | Right y -> eq_r x y)
|
||||
|
||||
let compare cmp_l cmp_r a b =
|
||||
match a with
|
||||
| Left x ->
|
||||
(match b with Left y -> cmp_l x y | Right _ -> -1)
|
||||
| Right x ->
|
||||
(match b with Left _ -> 1 | Right y -> cmp_r x y)
|
||||
end ;;
|
||||
|
||||
module Map = struct
|
||||
|
||||
Reference in New Issue
Block a user