Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print contents of a map in F#

Tags:

f#

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

like image 845
silvercoder Avatar asked Oct 20 '25 14:10

silvercoder


1 Answers

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
like image 150
Phillip Carter Avatar answered Oct 23 '25 04:10

Phillip Carter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!