How do I iterate over a hashtable in F#? -
let dic = environment.getenvironmentvariables() dic |> seq.filter( fun k -> k.contains("comntools"))
fails compile. i've tried using array.filter, seq.filter, list.filter
i've tried getting dic.keys
iterate on f# doesn't seem want me coerce keycollection
ienumerable
.
i've tried upcasting hashtable ienumerable<keyvaluepair<string,string>>
how walk hashtable returned environment.getenvironmentvariables()
?
since environment.getenvironmentvariables()
returns non-generic idictionary
, stores key/value pairs in dictionaryentry
, have use seq.cast
first:
let dic = environment.getenvironmentvariables() dic |> seq.cast<dictionaryentry> |> seq.filter(fun entry -> entry.key.tostring().contains("comntools"))
see relevant docs @ https://msdn.microsoft.com/en-us/library/system.collections.idictionary(v=vs.110).aspx. notice entry.key
of type obj
, 1 has convert string before checking string containment.
instead of using high-order functions, sequence expression might handy:
let dic = environment.getenvironmentvariables() seq { entry in seq.cast<dictionaryentry> dic -> (string entry.key), (string entry.value) } |> seq.filter(fun (k, _) -> k.contains("comntools"))
Comments
Post a Comment