I am trying to merge two Maps, but there is no built in method for joining Collections. So how do you do it?
-
you should look here (stackoverflow.com/questions/590991/merging-two-ienumerablets), basically map1.Union(map2) should work – dvhh Oct 20 '10 at 4:44
-
Slight quibble: using linq's Union operator you'd end up with a IEnumerable of tuples, not quite the same as a map. – Robert Oct 20 '10 at 4:53
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)]
-
1Be aware that this reconstructs both maps from scratch which can be very slow. A better solution is Robert's if
map1
is large andmap2
is small. – Tim Rogers Jan 23 '14 at 15:06
You can implement this using Map.fold and Map.add, since add is actually add/replace:
let map1 = Map.ofList [ 1, "one"; 2, "two"; 3, "three" ]
let map2 = Map.ofList [ 2, "two"; 3, "oranges"; 4, "four" ]
let newMap = Map.fold (fun acc key value -> Map.add key value acc) map1 map2
printfn "%A" newMap
Probably the reason merge isn't provided out of the box is that you need to deal with key conflicts. In this simple merge algorithm we simple take the key value pair from the second map, this may not be the behaviour you want.
An alternative way is this:
let merge (a : Map<'a, 'b>) (b : Map<'a, 'b>) (f : 'a -> 'b * 'b -> 'b) =
Map.fold (fun s k v ->
match Map.tryFind k s with
| Some v' -> Map.add k (f k (v, v')) s
| None -> Map.add k v s) a b
It lets you decide on what value you want if there is duplicate keys.
Example:
let a = Map([1,11;2,21;3,31;])
let b = Map([3,32; 4,41;5,51;6,61;])
merge a b (fun k (v, v') -> v + v');;
//Result
val it : Map<int,int> =
map [(1, 11); (2, 21); (3, 63); (4, 41); (5, 51); (6, 61)]
Notice that the key 3
is different.
-
Note: you don't actually need to specify the types on your input parameters. – Benjol Feb 5 '13 at 10:38