5 people like it.

Dict utilities to use Dictionary in more F#:y way

Utility module to more naturally use mutable Dictionary type from Collections library

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
module Dict
 
open System.Collections.Generic

// Converts seq of key - value pairs to mutable Dictionary
let ofSeq (src:seq<'a * 'b>) = 
    let d = new Dictionary<'a, 'b>()
    for (k,v) in src do
        d.Add(k,v)
    d

// get a seq of key-value pairs for easy iteration with for (k,v) in d do...
let pairs (d:Dictionary<'a, 'b>) =
    seq {
        for kv in d do
            yield (kv.Key, kv.Value)
    }
module Dict
namespace System
namespace System.Collections
namespace System.Collections.Generic
val ofSeq : src:seq<'a * 'b> -> Dictionary<'a,'b> (requires equality)

Full name: Dict.ofSeq
val src : seq<'a * 'b> (requires equality)
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val d : Dictionary<'a,'b> (requires equality)
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Item : 'TKey -> 'TValue with get, set
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

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

--------------------
Dictionary() : unit
Dictionary(capacity: int) : unit
Dictionary(comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : unit
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : unit
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : unit
val k : 'a (requires equality)
val v : 'b
Dictionary.Add(key: 'a, value: 'b) : unit
val pairs : d:Dictionary<'a,'b> -> seq<'a * 'b>

Full name: Dict.pairs
val d : Dictionary<'a,'b>
val kv : KeyValuePair<'a,'b>
property KeyValuePair.Key: 'a
property KeyValuePair.Value: 'b
Raw view Test code New version

More information

Link:http://fssnip.net/qL
Posted:9 years ago
Author:Ville Vainio
Tags: collections , dictionary