rfreytag
rfreytag

Reputation: 1097

Need F# Map.fold example

I can’t find a simple working example of the use of Map.fold anywhere. I have seen the F# for fun and profit and the MSFT documents on ‘fold’.

Can you provide a short working example of the use of Map.fold?

Upvotes: 2

Views: 788

Answers (1)

rfreytag
rfreytag

Reputation: 1097

let capitals =
    [("Australia", "Canberra"); ("Canada", "Ottawa"); ("China", "Beijing");
        ("Denmark", "Copenhagen"); ("Egypt", "Cairo"); ("Finland", "Helsinki");
        ("France", "Paris"); ("Germany", "Berlin"); ("India", "New Delhi");
        ("Japan", "Tokyo"); ("Mexico", "Mexico City"); ("Russia", "Moscow");
        ("Slovenia", "Ljubljana"); ("Spain", "Madrid"); ("Sweden", "Stockholm");
        ("Taiwan", "Taipei"); ("USA", "Washington D.C.")]
    |> Map.ofList

printfn "%A\n" capitals

printfn "%s" 
    (Map.fold 
        (fun acc key value -> (acc + "(" + key + "," + value + ")")) 
        "" 
        capitals)

Upvotes: 5

Related Questions