ocaml: phase 6 Hashtbl.keys/values/bindings/remove/clear (+4 tests, 545 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s

Two new host primitives:
  _hashtbl_remove t k   -> dissoc the key from the underlying dict
  _hashtbl_clear  t     -> reset the cell to {}

Eight new OCaml-syntax helpers in runtime.sx Hashtbl module:
  bindings t            = _hashtbl_to_list t
  keys t                = List.map (fun (k, _) -> k) (...)
  values t              = List.map (fun (_, v) -> v) (...)
  to_seq t              = bindings t
  to_seq_keys / to_seq_values
  remove / clear / reset

The keys/values implementations use a 'fun pair -> match pair with
(k, _) -> k' indirection because parse-fun does not currently allow
tuple patterns directly on parameters. Same restriction we worked
around in iteration 98's let-pattern desugaring.

Also: a detour attempting to add top-level 'let (a, b) = expr'
support was started but reverted — parse-decl-let in the outer
ocaml-parse-program scope does not have access to parse-pattern
(which is local to ocaml-parse). Will need a slice + re-parse trick
later.
This commit is contained in:
2026-05-09 03:59:20 +00:00
parent dab8718289
commit 41190c6d23
4 changed files with 58 additions and 0 deletions

View File

@@ -774,6 +774,27 @@
| (k, v) :: rest -> go rest (f k v a)
in
go (_hashtbl_to_list t) acc
(* OCaml's Hashtbl doesn't expose `keys` directly — it offers
`to_seq_keys` etc. We provide both: a plain list snapshot and
the keys/values projections that are commonly useful. *)
let bindings t = _hashtbl_to_list t
let keys t =
List.map (fun pair -> match pair with (k, _) -> k)
(_hashtbl_to_list t)
let values t =
List.map (fun pair -> match pair with (_, v) -> v)
(_hashtbl_to_list t)
let to_seq t = _hashtbl_to_list t
let to_seq_keys = keys
let to_seq_values = values
let remove t k = _hashtbl_remove t k
let reset t = _hashtbl_clear t
let clear t = _hashtbl_clear t
end ;;
module Map = struct