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

@@ -171,6 +171,17 @@
(fn (t) (fn (k) (has-key? (nth t 0) (str k)))))
(list "_hashtbl_length"
(fn (t) (len (keys (nth t 0)))))
(list "_hashtbl_remove"
(fn (t) (fn (k)
(let ((d (nth t 0)))
(begin
(set-nth! t 0 (dissoc d (str k)))
nil)))))
(list "_hashtbl_clear"
(fn (t)
(begin
(set-nth! t 0 {})
nil)))
;; _lazy_force: evaluate the thunk on first force, cache result.
;; cell: one-elt list whose value is ("Thunk" expr env) or
;; ("Forced" v).