I didn't find any native function to do the conversion from Dict into Map and viceversa. (Something like Map.ofDict or Dict.ofMap.)
Main reason being maps are immutable while dictionaries are not.
Have I missed something?
There isn't a convenient function to go from dictionary to map but there is a built in dict function which converts from a sequence of tuples to an IDictionary<'a,'b>, see: https://msdn.microsoft.com/en-us/library/ee353774.aspx.
The simplest way I know to do the dictionary to map conversion is:
let dictToMap (dic : System.Collections.Generic.IDictionary<_,_>) =
dic
|> Seq.map (|KeyValue|)
|> Map.ofSeq
The reverse is easy with the dict function:
let mapToDict map =
map
|> Map.toSeq
|> dict
Note that the above function returns an IDictionary, the implementation of which is both internal to the FSharp.Core library and not mutable.
If you want to get to a standard .NET mutable dictionary, you use the constructor which accepts an IDictionary, Map implements IDictionary so this step is very straightforward.
let mapToMutDict map =
map :> System.Collections.Generic.IDictionary<_,_>
|> System.Collections.Generic.Dictionary
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With