Module Hashtbloper


module Hashtbloper: sig .. end
Hash table convenience operators

These are operators to help make your work with hash tables easier. For other utitilies, see also the Hashtblutil.

Unlike the functions in Hashtblutil, all of these operators are non-destructive. That is, they do not modify the hash tables on which they operate, but rather return a new hash table that represents the result. This leads to the most natural behavior for an operator.


val (/>) : ('a, 'b) Hashtbl.t -> 'a -> 'b
Shorcut to get an element from a hash. The following:
  hash /> 5 

is the same as:

  Hashtbl.find hash 5 

This can be combined to form more powerful constructs. Consider this example:

  let sections = Hashtbl.create 5;;
  let options = Hashtbl.create 5;;
  Hashtbl.replace options "option1" "value1";;
  Hashtbl.replace sections "section1" options;;
  sections /> "section1" /> "option1";;
     returns "value1" 

val (/+) : ('a, 'b) Hashtbl.t -> ('a, 'b) Hashtbl.t -> ('a, 'b) Hashtbl.t
Add (merge) two hashes. For instance, this:
   hash1 /+ hash2 

Returns a new hash (hash1 and hash2 are unmodified). It will contain all key/value pairs in either hash. If any pairs are duplicated, the values in hash2 take precedence.

val (//) : ('a, 'b) Hashtbl.t -> 'a * 'b -> ('a, 'b) Hashtbl.t
Returns a new hash that has the given (key, value) pair added to the hash passed in. If a duplicate key is given, the new pair takes precedence.

For example:

  let newhash = hash // ("key", "value");; 

This will return a new hash that has the elements of hash plus one more.