I have a NameValueCollection which I need to convert to a Map and I just can’t work it out. I tried:
let headerMap (m : MailMessage) = m.Headers |> Map.map (fun k v -> v.[k])
Do I need to use Seq.map instead?
Basically the point of this is that I want to serialize the headers in a System.Net.MailMessage to JSON.
Daniel’s answer will work just fine, but I thought I’d offer some additional alternatives:
Array.fold — This should be faster than Daniel’s version since it avoids the overhead of the iterators.
Array.fold with sets of values — Similar to the code above, but returns the value as a
Set<string>which may be useful if you want to determine if some value is in the returned set of values.Recursive loop — Creates the map item-by-item with a recursive loop. I wouldn’t use this in practice because the
Array.foldversion would be easier and faster. However, this approach could be faster if the specific collection class you’re using (derived fromNameValueCollection) overrides theAllKeysproperty and has some weird internal behavior which takes a long time to return the property value.Imperative loop — Creates the map item-by-item with an imperative loop. As with the recursive loop, I’d prefer to use
Array.foldin practice unless there was some special reason not to.