ocaml: phase 6 Hashtbl.iter / Hashtbl.fold (+2 tests, 494 total)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 35s

New host primitive _hashtbl_to_list returns the entries as a list of
OCaml tuples — ('tuple' k v) form, matching the AST representation
that the pattern-match VM (:ptuple) expects. Without that exact
shape, '(k, v) :: rest' patterns fail to match.

Hashtbl.iter / Hashtbl.fold in runtime walk that list with the user
fn. This closes a long-standing gap: previously Hashtbl was opaque
once values were written (we could only find_opt one key at a time).

  let t = Hashtbl.create 4 in
  Hashtbl.add t "a" 1; Hashtbl.add t "b" 2; Hashtbl.add t "c" 3;
  Hashtbl.fold (fun _ v acc -> acc + v) t 0   = 6
This commit is contained in:
2026-05-09 00:53:32 +00:00
parent 1b38f89055
commit 207dfc60ad
4 changed files with 51 additions and 1 deletions

View File

@@ -530,6 +530,24 @@
| Some v -> v
let mem t k = _hashtbl_mem t k
let length t = _hashtbl_length t
(* iter / fold over (k, v) pairs. Keys come back as their string
representation since the host coerces all keys via `str`. *)
let iter f t =
let rec go xs =
match xs with
| [] -> ()
| (k, v) :: rest -> f k v; go rest
in
go (_hashtbl_to_list t)
let fold f t acc =
let rec go xs a =
match xs with
| [] -> a
| (k, v) :: rest -> go rest (f k v a)
in
go (_hashtbl_to_list t) acc
end ;;
module Map = struct