I am not very familiar with Map module in Ocaml.
I have a simple map m: string Map.Make(Int).t from int to string, and I know that every binding (:string) is unique. I would like to write a funciton find_from_string_to_int: string -> string Map.Make(Int).t -> int, could anyone help? Thank you very much!
If this is an infrequent operation, the easiest way is to fold over all the (key,value) bindings of the map
SMap.fold:If this is a common operation, what you’re looking for is a bidirectional map (a map that can be queried in both “directions”). The easiest way is to carry around a pair of maps:
int IMap.t * string SMap.t(accesses in both direction are logarithmic, while thereverseabove is linear in the size of the map). You could also implement a dedicated data structure, but that’s probably not worth the added complexity.PS:
reversecould be optimized to quit the iteration early if the value is found, but if it’s an uncommon operation it’s not really important anyway.