API / Belt / HashMap

You are currently looking at the v6.0 - v8.2 docs (Reason v3.6 syntax edition). You can find the latest API docs here.

(These docs cover all versions between v3 to v8 and are equivalent to the old BuckleScript docs before the rebrand)

HashMap

The top level provides generic mutable hash map operations.

t

RE
type t('key, 'value, 'id);

The type of hash tables from type 'key to type 'value.

id

RE
type id('a, 'id) = BeltId.hashable('a, 'id);

The identity needed for making an empty hash map.

make

RE
let make: (~hintSize: int, ~id: id('key, 'id)) => t('key, 'value, 'id);

make(~hintSize=10, ~id) creates a new map by taking in the comparator and hintSize.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let hMap = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(hMap, 0, "a");

clear

RE
let clear: t('key, 'value, 'id) => unit;

Clears a hash table.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let hMap = Belt.HashMap.fromArray([|(1, "1")|], ~id=(module IntHash)) Belt.HashMap.clear(hMap) Belt.HashMap.isEmpty(hMap) == true;

isEmpty

RE
let isEmpty: t('a, 'b, 'c) => bool;

isEmpty(m) checks whether a hash map is empty.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); Belt.HashMap.isEmpty(Belt.HashMap.fromArray([|(1, "1")|], ~id=(module IntHash))) == false;

set

RE
let set: (t('key, 'value, 'id), 'key, 'value) => unit;

set(hMap, k, v) if k does not exist, add the binding k,v, otherwise, update the old value with the new v.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.fromArray([|(2, "2"), (1, "1"), (3, "3")|], ~id=(module IntHash)); Belt.HashMap.set(s0, 2, "3"); Belt.HashMap.valuesToArray(s0) == [|"1", "3", "3"|];

copy

RE
let copy: t('key, 'value, 'id) => t('key, 'value, 'id);

Creates copy of a hash map.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.fromArray([|(2, "2"), (1, "1"), (3, "3")|], ~id=(module IntHash)); let s1 = Belt.HashMap.copy(s0) Belt.HashMap.set(s0, 2, "3"); Belt.HashMap.get(s0, 2) != Belt.HashMap.get(s1, 2)

get

RE
let get: (t('key, 'value, 'id), 'key) => option('value);

Returns value bound under specific key. If values not exist returns None.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.get(s0, 1) == Some("value1"); Belt.HashMap.get(s0, 2) == None;

has

RE
let has: (t('key, 'value, 'id), 'key) => bool;

Checks if x is bound in tbl.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.has(s0, 1) == true; Belt.HashMap.has(s0, 2) == false;

remove

RE
let remove: (t('key, 'value, 'id), 'key) => unit;

If bound exists, removes it from the hash map.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.remove(s0, 1); Belt.HashMap.has(s0, 1) == false;

forEachU

RE
let forEachU: (t('key, 'value, 'id), [@bs] (('key, 'value) => unit)) => unit;

Same as forEach but takes uncurried functon.

forEach

RE
let forEach: (t('key, 'value, 'id), ('key, 'value) => unit) => unit;

forEach(tbl, f) applies f to all bindings in table tbl. f receives the key as first argument, and the associated value as second argument. Each binding is presented exactly once to f.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.forEach(s0, (key, value) => Js.log2(key, value)); // prints (1, "value1")

reduceU

RE
let reduceU: (t('key, 'value, 'id), 'c, [@bs] (('c, 'key, 'value) => 'c)) => 'c;

Same as reduce but takes uncurried functon.

reduce

RE
let reduce: (t('key, 'value, 'id), 'c, ('c, 'key, 'value) => 'c) => 'c;

reduce(tbl, init, f) computes (f(kN, dN) ... (f(k1, d1, init))...), where k1 ... kN are the keys of all bindings in tbl, and d1 ... dN are the associated values. Each binding is presented exactly once to f.

The order in which the bindings are passed to f is unspecified. However, if the table contains several bindings for the same key, they are passed to f in reverse order of introduction, that is, the most recent binding is passed first.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.reduce(s0, "", (acc, key, value) => { acc ++ ", " ++ value }) == "value1, value2";

keepMapInPlaceU

RE
let keepMapInPlaceU: (t('key, 'value, 'id), [@bs] (('key, 'value) => option('value))) => unit;

Same as keepMapInPlace but takes uncurried functon.

keepMapInPlace

RE
let keepMapInPlace: (t('key, 'value, 'id), ('key, 'value) => option('value)) => unit;

Filters out values for which function f returned None.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.keepMapInPlace(s0, (key, value) => { key == 1 ? None : Some(value) });

size

RE
let size: t('a, 'b, 'c) => int;

size(tbl) returns the number of bindings in tbl. It takes constant time.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.size(s0) == 2;

toArray

RE
let toArray: t('key, 'value, 'id) => array(('key, 'value));

Returns array of key value pairs.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.toArray(s0) == [|(1, "value1"), (2, "value2")|];

keysToArray

RE
let keysToArray: t('key, 'a, 'b) => array('key);

Returns array of keys.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.keysToArray(s0) == [|1, 2|];

valuesToArray

RE
let valuesToArray: t('a, 'value, 'b) => array('value);

Returns array of values.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(s0, 1, "value1"); Belt.HashMap.set(s0, 2, "value2"); Belt.HashMap.valuesToArray(s0) == [|"value1", "value2"|];

fromArray

RE
let fromArray: (array(('key, 'value)), ~id: id('key, 'id)) => t('key, 'value, 'id);

Creates new hash map from array of pairs.

Returns array of values.

RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let s0 = Belt.HashMap.fromArray([|(1, "value1"), (2, "value2")|], ~id=(module IntHash)); Belt.HashMap.toArray(s0) == [|(1, "value1"), (2, "value2")|];

mergeMany

RE
let mergeMany: (t('key, 'value, 'id), array(('key, 'value))) => unit;
RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let hMap = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.mergeMany(hMap, [|(1, "1"), (2, "2")|])

getBucketHistogram

RE
let getBucketHistogram: t('a, 'b, 'c) => array(int);
RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let hMap = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(hMap, 1, "1"); Belt.HashMap.getBucketHistogram(hMap);

logStats

RE
let logStats: t('a, 'b, 'c) => unit;
RE
module IntHash = Belt.Id.MakeHashable({ type t = int; let hash = a => a; let eq = (a, b) => a == b; }); let hMap = Belt.HashMap.make(~hintSize=10, ~id=(module IntHash)); Belt.HashMap.set(hMap, 1, "1"); Belt.HashMap.logStats(hMap);