Given a list kinda like (simplified):
type foo(n:string,m:string,l:string) =
member f.name=n
member f.val1=m
member f.val2=l
let dates = [
foo("a","aa","aaa")
foo("b","bb","bbb")
]
How can a a immutable dictionary-type structure (eg, Map, IDictionary...any others?) of the form key:foo.name,value:foo
be made?
My best guess was
let fooDict = for f in foo do yield f.name,f
But that for-comprehension syntax can only be used to make a list, array, or seq?
With Java 8, you can convert a List to Map in one line using the stream() and Collectors. toMap() utility methods. The Collectors. toMap() method collects a stream as a Map and uses its arguments to decide what key/value to use.
The list of all declared fields can be obtained using the java. lang. Class. getDeclaredFields() method as it returns an array of field objects.
To create an immutable dictionary (the interface is mutable, but will throw an exception if you try to modify it)
[ for f in dates -> f.name,f ] |> dict
or
dates |> Seq.map (fun f -> f.name, f) |> dict
To create an immutable Map:
[ for f in dates -> f.name,f ] |> Map.ofSeq
or
dates |> Seq.map (fun f -> f.name, f) |> Map.ofSeq
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