2 people like it.

Implement concat for Maps

Concatenate all the input maps. Where there are duplicate input keys, the last mapping is preserved.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
module Map =

    module private Kvp =

        open System.Collections.Generic

        let toTuple (kvp : KeyValuePair<_,_>) =
            kvp.Key, kvp.Value

    /// Concatenate all the input maps. Where there are duplicate input keys, 
    /// the last mapping is preserved.
    let concat maps = 
        maps
        |> Seq.concat
        |> Seq.map Kvp.toTuple
        |> Map.ofSeq

let map1 = [1,'a'; 2,'b'] |> Map.ofSeq
let map2 = [2,'c'; 3,'d'] |> Map.ofSeq

//  map [(1, 'a'); (2, 'c'); (3, 'd')]
Map.concat [|map1; map2|]
Multiple items
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
namespace System
namespace System.Collections
namespace System.Collections.Generic
val private toTuple : kvp:KeyValuePair<'a,'b> -> 'a * 'b

Full name: Script.Map.Kvp.toTuple
val kvp : KeyValuePair<'a,'b>
Multiple items
type KeyValuePair<'TKey,'TValue> =
  struct
    new : key:'TKey * value:'TValue -> KeyValuePair<'TKey, 'TValue>
    member Key : 'TKey
    member ToString : unit -> string
    member Value : 'TValue
  end

Full name: System.Collections.Generic.KeyValuePair<_,_>

--------------------
KeyValuePair()
KeyValuePair(key: 'TKey, value: 'TValue) : unit
property KeyValuePair.Key: 'a
property KeyValuePair.Value: 'b
val concat : maps:seq<#seq<System.Collections.Generic.KeyValuePair<'b,'c>>> -> Map<'b,'c> (requires comparison)

Full name: Script.Map.concat


 Concatenate all the input maps. Where there are duplicate input keys,
 the last mapping is preserved.
val maps : seq<#seq<System.Collections.Generic.KeyValuePair<'b,'c>>> (requires comparison)
module Seq

from Microsoft.FSharp.Collections
val concat : sources:seq<#seq<'T>> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.concat
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
module Kvp

from Script.Map
val private toTuple : kvp:System.Collections.Generic.KeyValuePair<'a,'b> -> 'a * 'b

Full name: Script.Map.Kvp.toTuple
val ofSeq : elements:seq<'Key * 'T> -> Map<'Key,'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Map.ofSeq
val map1 : Map<int,char>

Full name: Script.map1
Multiple items
module Map

from Script

--------------------
module Map

from Microsoft.FSharp.Collections

--------------------
type Map<'Key,'Value (requires comparison)> =
  interface IEnumerable
  interface IComparable
  interface IEnumerable<KeyValuePair<'Key,'Value>>
  interface ICollection<KeyValuePair<'Key,'Value>>
  interface IDictionary<'Key,'Value>
  new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
  member Add : key:'Key * value:'Value -> Map<'Key,'Value>
  member ContainsKey : key:'Key -> bool
  override Equals : obj -> bool
  member Remove : key:'Key -> Map<'Key,'Value>
  ...

Full name: Microsoft.FSharp.Collections.Map<_,_>

--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val map2 : Map<int,char>

Full name: Script.map2
Raw view Test code New version

More information

Link:http://fssnip.net/7WY
Posted:4 years ago
Author:Kit Eason
Tags: maps