Belt.Map.Dict: This module separates data from function which is more verbose but slightly more efficient
A immutable sorted map module which allows customize compare behavior.
The implementation uses balanced binary trees, and therefore searching and insertion take time logarithmic in the size of the map.
For more info on this module's usage of identity, `make` and others, please see the top level documentation of Belt, A special encoding for collection safety.
Example usage:
module PairComparator = Belt.Id.MakeComparable(struct
type t = int * int
let cmp (a0, a1) (b0, b1) =
match Pervasives.compare a0 b0 with
| 0 -> Pervasives.compare a1 b1
| c -> c
end)
let myMap = Belt.Map.make ~id:(module PairComparator)
let myMap2 = Belt.Map.set myMap (1, 2) "myValue"
The API documentation below will assume a predeclared comparator module for integers, IntCmp
module Int = Belt_MapInt
Specalized when key type is int, more efficient than the generic type, its compare behavior is fixed using the built-in comparison
This module seprate identity from data, it is a bit more verboe but slightly more efficient due to the fact that there is no need to pack identity and data back after each operation
type('key, 'value, 'identity) t
('key, 'identity) t
'key is the field type
'value is the element type
'identity the identity of the collection
type('key, 'id) id =
(moduleBelt__.Belt_Id.Comparablewithtypeidentity = 'idandtypet = 'key)
has (fromArray [|1,"1"|] ~id:(module IntCmp)) 1 = true
val cmpU : ('k, 'v, 'id)t->('k, 'v, 'id)t->('v->'v-> int)-> int
val cmp : ('k, 'v, 'id)t->('k, 'v, 'id)t->('v->'v-> int)-> int
cmp m0 m1 vcmp
Total ordering of map given total ordering of value function.
It will compare size first and each element following the order one by one.
val eqU : ('k, 'v, 'id)t->('k, 'v, 'id)t->('v->'v-> bool)-> bool
val eq : ('k, 'v, 'id)t->('k, 'v, 'id)t->('v->'v-> bool)-> bool
eq m1 m2 veq tests whether the maps m1 and m2 are equal, that is, contain equal keys and associate them with equal data. veq is the equality predicate used to compare the data associated with the keys.
val findFirstByU : ('k, 'v, 'id)t->('k->'v-> bool)->('k * 'v) option
val findFirstBy : ('k, 'v, 'id)t->('k->'v-> bool)->('k * 'v) option
findFirstBy m p uses funcion f to find the first key value pair to match predicate p.
let s0 = fromArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
findFirstBy s0 (fun k v -> k = 4 ) = option (4, "4");;
val forEachU : ('k, 'v, 'id)t->('k->'v-> unit)-> unit
val forEach : ('k, 'v, 'id)t->('k->'v-> unit)-> unit
forEach m f applies f to all bindings in map m. f receives the 'k as first argument, and the associated value as second argument. The bindings are passed to f in increasing order with respect to the ordering over the type of the keys.
let s0 = fromArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
let acc = ref [] ;;
forEach s0 (fun k v -> acc := (k,v) :: !acc);;
!acc = [4,"4"; 3,"3"; 2,"2"; 1,"1"]
val reduceU : ('k, 'v, 'id)t->'acc->('acc->'k->'v->'acc)->'acc
val reduce : ('k, 'v, 'id)t->'acc->('acc->'k->'v->'acc)->'acc
reduce m a f computes (f kN dN ... (f k1 d1 a)...), where k1 ... kN are the keys of all bindings in m (in increasing order), and d1 ... dN are the associated data.
let s0 = fromArray ~id:(module IntCmp) [|4,"4";1,"1";2,"2,"3""|];;
reduce s0 [] (fun acc k v -> (k,v) acc ) = [4,"4";3,"3";2,"2";1,"1"];;
val everyU : ('k, 'v, 'id)t->('k->'v-> bool)-> bool
val every : ('k, 'v, 'id)t->('k->'v-> bool)-> bool
every m p checks if all the bindings of the map satisfy the predicate p. Order unspecified
val someU : ('k, 'v, 'id)t->('k->'v-> bool)-> bool
set m x y returns a map containing the same bindings as m, with a new binding of x to y. If x was already bound in m, its previous binding disappears.
let s0 = (fromArray [2,"2"; 1,"1"; 3,"3"] ~id:(module IntCmp));;
let s1 = set s0 2 "3";;
valuesToArray s1 = ["1";"3";"3"];;
val updateU :
('k, 'v, 'id)t->'k->('v option->'v option)->('k, 'v, 'id)t
val update :
('k, 'v, 'id)t->'k->('v option->'v option)->('k, 'v, 'id)t
update m x f returns a map containing the same bindings as m, except for the binding of x. Depending on the value of y where y is f (get x m), the binding of x is added, removed or updated. If y is None, the binding is removed if it exists; otherwise, if y is Some z then x is associated to z in the resulting map.
merge m1 m2 f computes a map whose keys is a subset of keys of m1 and of m2. The presence of each such binding, and the corresponding value, is determined with the function f.
val keepU : ('k, 'v, 'id)t->('k->'v-> bool)->('k, 'v, 'id)t
val keep : ('k, 'v, 'id)t->('k->'v-> bool)->('k, 'v, 'id)t
keep m p returns the map with all the bindings in m that satisfy predicate p.
partition m p returns a pair of maps (m1, m2), where m1 contains all the bindings of s that satisfy the predicate p, and m2 is the map with all the bindings of s that do not satisfy p.
split x m returns a tuple (l r), data, where l is the map with all the bindings of m whose 'k is strictly less than x; r is the map with all the bindings of m whose 'k is strictly greater than x; data is None if m contains no binding for x, or Some v if m binds v to x.
val mapU : ('k, 'v, 'id)t->('v->'v2)->('k, 'v2, 'id)t
val map : ('k, 'v, 'id)t->('v->'v2)->('k, 'v2, 'id)t
map m f returns a map with same domain as m, where the associated value a of all bindings of m has been replaced by the result of the application of f to a. The bindings are passed to f in increasing order with respect to the ordering over the type of the keys.
val mapWithKeyU : ('k, 'v, 'id)t->('k->'v->'v2)->('k, 'v2, 'id)t
val mapWithKey : ('k, 'v, 'id)t->('k->'v->'v2)->('k, 'v2, 'id)t
mapWithKey m f
The same as map except that f is supplied with one more argument: the key
val getData : ('k, 'v, 'id)t->('k, 'v, 'id)Belt__.Belt_MapDict.t
getData s0
Advanced usage only
returns
the raw data (detached from comparator), but its type is still manifested, so that user can pass identity directly without boxing