I am new to F# and trying to print the contents of a map. The code I have is as follows:
let ids = CSVReader.ReadCSVFile(id, true).AsEnumerable()
|> Seq.groupBy( fun (id, _, _) -> id )
|> Seq.map( fun (_, vseq) ->
let vseqr = vseq |> Seq.sortBy( fun (_, _, d) -> -d ) |> Seq.head
let first (x1:string, x2:string, x3:int) = x1
let second (x1:string, x2:string, x3:int) = x2
(first vseqr, second vseqr)
)
|> Map.ofSeq
I am looking to print the contents in Map.ofSeq
F# has a built-in pretty printer with %A
formatting.
So this:
let lst = [(1, "hello"); (2, "world")]
printfn "%A" (Map.ofList lst)
Yields:
map [(1, "hello"); (2, "world")]
If you want to print the contents differently, you'll need to write your own routine like so:
let printer (mp: Map<'a, 'b>) =
for kvp in mp do
printfn $"{kvp.Key}: {kvp.Value}"
printer (Map.ofList lst)
Which would yield:
1: hello
2: world
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