in F#, How do you merge 2 Collections.Map instances?

Viewed 6748

I am trying to merge two Maps, but there is no built in method for joining Collections. So how do you do it?

4 Answers

If you prefer using function composition you could define a join function like this:

let join (m1:Map<'a,'b>) (m2:Map<'a,'b>) = 
    Map.foldBack Map.add m2 m1

Given that m1 and m2 are Maps you would then use it like this:

m3 = join m1 m2

Some caveats:

  • Keys in m2 will overwrite those in m1
  • As others have noted it's faster to let the smaller map be the second argument as it will result in fewer insertions
Related