5 people like it.

ImmutableDictionary module

Functional programming operators related to the BCL ImmutableDictionary type.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
module ImmutableDict =
   open System.Collections.Generic
   open System.Collections.Immutable

   let empty<'key,'T> = 
      ImmutableDictionary<'key,'T>.Empty :> IImmutableDictionary<'key,'T>

   let isEmpty (table:IImmutableDictionary<'key,'T>) =
      table.Count = 0

   let add key value (table:IImmutableDictionary<'key,'T>) = 
      table.Add(key,value)
   
   let remove key (table:IImmutableDictionary<'key,'T>) = 
      table.Remove(key)
      
   let containsKey key (table:IImmutableDictionary<'key,'T>) = 
      table.ContainsKey(key)
   
   let find key (table:IImmutableDictionary<'key,'T>) = 
      match table.TryGetKey(key) with
      | true, value -> value
      | false, _ -> raise (KeyNotFoundException())
   
   let tryFind key (table:IImmutableDictionary<'key,'T>) =
      match table.TryGetKey(key) with
      | true, value -> Some value
      | false, _ -> None

   let toArray (table:IImmutableDictionary<'key,'T>) =
      [|for pair in table -> pair.Key, pair.Value|]

   let ofSeq (elements:('key * 'T) seq) =
      seq { for (k,v) in elements -> KeyValuePair(k,v) }
      |> empty.AddRange
     
   let map (mapping:'key -> 'T -> 'U) (table:IImmutableDictionary<'key,'T>) =
      seq { for pair in table -> KeyValuePair<'key,'U>(pair.Key, mapping pair.Key pair.Value) }
      |> empty.AddRange
      
   let filter (predicate:'key -> 'T -> bool) (table:IImmutableDictionary<'key,'T>) =
      seq { for pair in table do if predicate pair.Key pair.Value then yield pair }
      |> empty.AddRange

   let exists (predicate:'key -> 'T -> bool) (table:IImmutableDictionary<'key,'T>) =
      table |> Seq.exists (fun pair -> predicate pair.Key pair.Value)

   let iter (action:'key -> 'T -> unit) (table:IImmutableDictionary<'key,'T>) =
      table |> Seq.iter (fun pair -> action pair.Key pair.Value)
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Collections.Immutable
val empty<'key,'T> : obj

Full name: Script.ImmutableDict.empty
val isEmpty : table:'a -> bool

Full name: Script.ImmutableDict.isEmpty
val table : 'a
val add : key:'a -> value:'b -> table:'c -> 'd

Full name: Script.ImmutableDict.add
val key : 'a
val value : 'b
val table : 'c
val remove : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.remove
val table : 'b
val containsKey : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.containsKey
val find : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.find
val value : 'c
val raise : exn:System.Exception -> 'T

Full name: Microsoft.FSharp.Core.Operators.raise
Multiple items
type KeyNotFoundException =
  inherit SystemException
  new : unit -> KeyNotFoundException + 2 overloads

Full name: System.Collections.Generic.KeyNotFoundException

--------------------
KeyNotFoundException() : unit
KeyNotFoundException(message: string) : unit
KeyNotFoundException(message: string, innerException: exn) : unit
val tryFind : key:'a -> table:'b -> 'c option

Full name: Script.ImmutableDict.tryFind
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
val toArray : table:seq<'a> -> ('b * 'c) []

Full name: Script.ImmutableDict.toArray
val table : seq<'a>
val pair : 'a
val ofSeq : elements:seq<'key * 'T> -> 'a

Full name: Script.ImmutableDict.ofSeq
val elements : seq<'key * 'T>
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 k : 'key
val v : 'T
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
val map : mapping:('key -> 'T -> 'U) -> table:seq<'a> -> 'b

Full name: Script.ImmutableDict.map
val mapping : ('key -> 'T -> 'U)
val filter : predicate:('key -> 'T -> bool) -> table:seq<'a> -> 'b

Full name: Script.ImmutableDict.filter
val predicate : ('key -> 'T -> bool)
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
val exists : predicate:('key -> 'T -> bool) -> table:seq<'a> -> bool

Full name: Script.ImmutableDict.exists
module Seq

from Microsoft.FSharp.Collections
val exists : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.exists
val iter : action:('key -> 'T -> unit) -> table:seq<'a> -> unit

Full name: Script.ImmutableDict.iter
val action : ('key -> 'T -> unit)
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/r3
Posted:9 years ago
Author:Phillip Trelford
Tags: bcl , dictionary