You are currently looking at the < v8.2.0 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)
HashMapString
Specalized when key type is string
, more efficient than the generic type
key
REtype key = string;
Type of the Belt.HashMap.String
key.
t
REtype t('b);
Type of the Belt.HashMap.String
.
make
RElet make: (~hintSize: int) => t('b);
make(~hintSize=10)
creates a new hash map by taking the hintSize
.
RE
let hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "key1", "a");
clear
RElet clear: t('b) => unit;
Clears a hash table.
RElet hMap = Belt.HashMap.String.fromArray([|("1", "1")|])
Belt.HashMap.String.clear(hMap)
Belt.HashMap.String.isEmpty(hMap) == true;
isEmpty
RElet isEmpty: t('a) => bool;
isEmpty(m)
checks whether a hash map is empty.
RElet hMap = Belt.HashMap.String.fromArray([|("1", "1")|])
Belt.HashMap.String.isEmpty(hMap) == false;
set
RElet set: (t('a), key, 'a) => unit;
set(tbl, k, v)
if k
does not exist, add the binding k,v
, otherwise, update the old value with the new v
.
RE
let hMap = Belt.HashMap.String.fromArray([|("2", "2")|]);
Belt.HashMap.String.set(hMap, "1", "1");
Belt.HashMap.String.valuesToArray(hMap) == [|"1", "2"|];
copy
RElet copy: t('a) => t('a);
Creates copy of a hash map.
RElet hMap1 = Belt.HashMap.String.fromArray([|("1", "1"), ("2", "2")|]);
let hMap2 = Belt.HashMap.String.copy(hMap1)
Belt.HashMap.String.set(hMap2, "2", "3");
Belt.HashMap.String.get(hMap1, "2") != Belt.HashMap.String.get(hMap2, "2")
get
RElet get: (t('a), key) => option('a);
Returns value bound under specific key. If values not exist returns None
.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.get(hMap, "1") == Some("value1");
Belt.HashMap.String.get(hMap, "2") == None;
has
RElet has: (t('b), key) => bool;
Checks if x
is bound in tbl
.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.has(hMap, "1") == true;
Belt.HashMap.String.has(hMap, "2") == false;
remove
RElet remove: (t('a), key) => unit;
If bound exists, removes it from the hash map.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.remove(hMap, "1");
Belt.HashMap.String.has(hMap, "1") == false;
forEachU
RElet forEachU: (t('b), [@bs] ((key, 'b) => unit)) => unit;
Same as forEach but takes uncurried functon.
forEach
RElet forEach: (t('b), (key, 'b) => 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
.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.forEach(hMap, (key, value) => Js.log2(key, value));
// prints ("1", "value1")
reduceU
RElet reduceU: (t('b), 'c, [@bs] (('c, key, 'b) => 'c)) => 'c;
Same as reduce but takes uncurried functon.
reduce
RElet reduce: (t('b), 'c, ('c, key, 'b) => '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.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.reduce(hMap, "", (acc, key, value) => {
acc ++ ", " ++ value
}) == "value1, value2";
keepMapInPlaceU
RElet keepMapInPlaceU: (t('a), [@bs] ((key, 'a) => option('a))) => unit;
Same as keepMapInPlace but takes uncurried functon.
keepMapInPlace
RElet keepMapInPlace: (t('a), (key, 'a) => option('a)) => unit;
Filters out values for which function f
returned None
.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.keepMapInPlace(hMap, (key, value) => {
key == "1" ? None : Some(value)
});
size
RElet size: t('a) => int;
size(tbl)
returns the number of bindings in tbl
. It takes constant time.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.size(hMap) == 2;
toArray
RElet toArray: t('a) => array((key, 'a));
Returns array of key value pairs.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.toArray(hMap) == [|("1", "value1"), ("2", "value2")|];
keysToArray
RElet keysToArray: t('a) => array(key);
Returns array of keys.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.keysToArray(hMap) == [|"1", "2"|];
valuesToArray
RElet valuesToArray: t('a) => array('a);
Returns array of values.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "value1");
Belt.HashMap.String.set(hMap, "2", "value2");
Belt.HashMap.String.valuesToArray(hMap) == [|"value1", "value2"|];
fromArray
RElet fromArray: array((key, 'a)) => t('a);
Creates new hash map from array of pairs.
Returns array of values.
RElet hMap = Belt.HashMap.String.fromArray([|("1", "value1"), ("2", "value2")|]);
Belt.HashMap.String.toArray(hMap) == [|("1", "value1"), ("2", "value2")|];
mergeMany
RElet mergeMany: (t('a), array((key, 'a))) => unit;
Merges many key value pairs into hash map.
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.mergeMany(hMap, [|("1", "value1"), ("2", "value2")|]);
getBucketHistogram
RElet getBucketHistogram: t('a) => array(int);
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "1");
Belt.HashMap.String.getBucketHistogram(hMap);
logStats
RElet logStats: t('a) => unit;
RElet hMap = Belt.HashMap.String.make(~hintSize=10);
Belt.HashMap.String.set(hMap, "1", "1");
Belt.HashMap.String.logStats(hMap);