Hashtbl.Make(3)

🌸 Hashtbl.Make(3) β€” the OCaml library glow-up πŸ¦‹

okay so this little icon is from the OCaml library manual, page Hashtbl.Make(3) β€” and bestie, it's literally the functor that builds you a whole hashtable implementation. no cap, it's giving "build-a-bear but for data structures" πŸŽ€


✨ NAME ✨

Hashtbl.Make β€” a functor that builds an implementation of the hashtable structure. that's it, that's the vibe πŸ’…


🧚 Module 🧚

Module Hashtbl.Make

πŸ’œ Documentation πŸ’œ

Module Make
 : (H : HashedType) -> sig end

okay so here's the tea πŸ”₯ β€” Hashtbl.Make is a functor building an implementation of the hashtable structure.

The way that it works: the functor Hashtbl.Make returns a structure containing:

  • a type key of keys πŸ”‘
  • a type 'a t of hash tables associating data of type 'a to keys of type key

The operations perform similarly to those of the generic interface, but β€” and this is the main character detail β€” they use the hashing and equality functions specified in the functor argument H instead of generic equality and hashing. iykyk 🌈

ngl, one important thing: since the hash function is not seeded, the create operation of the result structure always returns non-randomized hash tables. no randomization, no surprises, just consistency ✨

Parameters:

  • "H" β€” which is a Hashtbl.HashedType πŸ’–

πŸ‘‘ Types πŸ‘‘

  • type key β€” the key type, slaying as always πŸ”‘
  • type !'a t β€” the hash table type itself, with that ! variance marker doing its thing (yes, we stan a constructor that's not covariant) πŸ’…

⭐ The functions ⭐

  • val create : int -> 'a t β€” makes you a fresh table, you give it an int and it gives you the 'a t πŸŽ€
  • val clear : 'a t -> unit β€” empties the table out. bye bestie πŸ‘‹
  • val reset : 'a t -> unit β€” resets it. Since 4.00 πŸ¦‹
  • val copy : 'a t -> 'a t β€” a whole duplicate, no sharing of your original's drama πŸ’–
  • val add : 'a t -> key -> 'a -> unit β€” put a key + value in there πŸ“
  • val remove : 'a t -> key -> unit β€” take a key out 🌸
  • val find_and_remove : 'a t -> key -> 'a option β€” finds it and removes it in one go, handing you back an 'a option. Since 5.5 πŸ”₯
  • val find : 'a t -> key -> 'a β€” find the value for a key, straight up πŸ’œ
  • val find_opt : 'a t -> key -> 'a option β€” same energy but safe, gives you an 'a option. Since 4.05 ✨
  • val find_all : 'a t -> key -> 'a list β€” all the values for that key, in a list πŸŽ€
  • val replace : 'a t -> key -> 'a -> unit β€” swap out the value 🧚
  • val find_and_replace : 'a t -> key -> 'a -> 'a option β€” swap it and give you the old value back as an 'a option. Since 5.5 🌟
  • val mem : 'a t -> key -> bool β€” is this key even in here?? bool says yes/no πŸ’…
  • val iter : (key -> 'a -> unit) -> 'a t -> unit β€” run your function over everything, nobody left out 🫢
  • val filter_map_inplace : (key -> 'a -> 'a option) -> 'a t -> unit β€” filters and maps in place, option deciding who stays. Since 4.03 🌈
  • val fold : (key -> 'a -> 'acc -> 'acc) -> 'a t -> 'acc -> 'acc β€” fold it all down into one accumulated 'acc ⭐
  • val length : 'a t -> int β€” how many things are in there, as an int πŸ’€
  • val stats : 'a t -> Hashtbl.statistics β€” the numbers on your table, all packaged as Hashtbl.statistics. Since 4.00 πŸ“Š
  • val to_seq : 'a t -> (key * 'a) Seq.t β€” turn it into a sequence of (key * 'a) pairs. Since 4.07 πŸ¦‹
  • val to_seq_keys : 'a t -> key Seq.t β€” just the keys, as a key Seq.t. Since 4.07 πŸ”‘
  • val to_seq_values : 'a t -> 'a Seq.t β€” just the values, as an 'a Seq.t. Since 4.07 πŸ’–
  • val add_seq : 'a t -> (key * 'a) Seq.t -> unit β€” pour a (key * 'a) Seq.t straight into your table. Since 4.07 πŸ“
  • val replace_seq : 'a t -> (key * 'a) Seq.t -> unit β€” same but replacing as it goes. Since 4.07 🌸
  • val of_seq : (key * 'a) Seq.t -> 'a t β€” build a whole fresh table out of a sequence. Since 4.07 πŸŽ€

fr fr that's every single val, every signature, every Since note β€” nothing skipped, nothing merged, we keep the facts and only change the vibe πŸ’…

okay bestie that's the whole page 🫢 this was rendered by OCamldoc, dated 2026-06-22, and it's the Hashtbl.Make(3) page from the OCaml library β€” periodt. go build your hashtable, you're gonna eat πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from