collectionsf#

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


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


Solution

  • Define the following function:

    let join (p:Map<'a,'b>) (q:Map<'a,'b>) = 
        Map(Seq.concat [ (Map.toSeq p) ; (Map.toSeq q) ])
    

    example:

    let a = Map([1,11;2,21;3,31;])
    
    let b = Map([3,32; 4,41;5,51;6,61;])
    
    let c = join a b
    

    and the result:

    val c : Map<int,int> =
      map [(1, 11); (2, 21); (3, 32); (4, 41); (5, 51); (6, 61)]