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: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
88: 
module ImmutableDict =
   open System.Collections.Generic
   open System.Collections.Immutable

   /// The empty dictionary.
   let empty<'Key,'T> = 
      ImmutableDictionary<'Key,'T>.Empty :> IImmutableDictionary<'Key,'T>

   /// Tests whether the collection has any elements.
   let isEmpty (table:IImmutableDictionary<'Key,'T>) =
      table.Count = 0

   /// Returns a new dictionary with the binding added to the given dictionary.
   let add key value (table:IImmutableDictionary<'Key,'T>) = 
      table.Add(key,value)
   
   /// Removes an element from the collection.
   let remove key (table:IImmutableDictionary<'Key,'T>) = 
      table.Remove(key)
      
   /// Tests if an element is in collection.
   let containsKey key (table:IImmutableDictionary<'Key,'T>) = 
      table.ContainsKey(key)
   
   /// Looks up an element in the collection.
   let find key (table:IImmutableDictionary<'Key,'T>) = 
      match table.TryGetKey(key) with
      | true, value -> value
      | false, _ -> raise (KeyNotFoundException())
   
   /// Looks up an element in the collection, returning 
   /// a Some value if the element is in the domain of the map, 
   /// or None if not.
   let tryFind key (table:IImmutableDictionary<'Key,'T>) =
      match table.TryGetKey(key) with
      | true, value -> Some value
      | false, _ -> None

   /// Returns an array of all key/value pairs in the mapping. 
   let toArray (table:IImmutableDictionary<'Key,'T>) =
      [|for pair in table -> pair.Key, pair.Value|]

   /// Returns a new collection made from the given bindings.
   let ofSeq (elements:('Key * 'T) seq) =
      seq { for (k,v) in elements -> KeyValuePair(k,v) }
      |> empty.AddRange
     
   /// Creates a new collection whose elements are the results of applying 
   /// the given function to each of the elements of the collection. 
   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
   
   /// Creates a new collection containing only the bindings for which the given predicate returns true.
   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

   /// Returns true if the given predicate returns true for one of the bindings in the collection.
   let exists (predicate:'Key -> 'T -> bool) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.exists (fun pair -> predicate pair.Key pair.Value)

   /// Returns true if the given predicate returns true for all of the bindings in the collection.
   let forall (predicate:'Key -> 'T -> bool) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.forall (fun pair -> predicate pair.Key pair.Value)

   /// Evaluates the function on each mapping in the collection. 
   /// Returns the key for the first mapping where the function returns true.
   let findKey (predicate:'Key -> 'T -> bool) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.find (fun pair -> predicate pair.Key pair.Value) |> fun pair -> pair.Key

   /// Evaluates the function on each mapping in the collection. 
   /// Returns the key for the first mapping where the function returns true.
   let pick (chooser:'Key -> 'T -> 'U option) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.pick (fun pair -> chooser pair.Key pair.Value)

   /// Searches the collection looking for the first element where the given function 
   /// returns a Some value.
   let tryPick (chooser:'Key -> 'T -> 'U option) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.tryPick (fun pair -> chooser pair.Key pair.Value)

   /// Applies the given function to each binding in the collection
   let iter (action:'Key -> 'T -> unit) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.iter (fun pair -> action pair.Key pair.Value)
  
   /// Folds over the bindings in the collection
   let fold (folder:'State -> 'Key -> 'T -> 'State) (state:'State) (table:IImmutableDictionary<'Key,'T>) =
      table |> Seq.fold (fun acc pair -> folder acc pair.Key pair.Value) state
namespace System
namespace System.Collections
namespace System.Collections.Generic
namespace System.Collections.Immutable
val empty<'Key,'T> : obj

Full name: Script.ImmutableDict.empty


 The empty dictionary.
val isEmpty : table:'a -> bool

Full name: Script.ImmutableDict.isEmpty


 Tests whether the collection has any elements.
val table : 'a
val add : key:'a -> value:'b -> table:'c -> 'd

Full name: Script.ImmutableDict.add


 Returns a new dictionary with the binding added to the given dictionary.
val key : 'a
val value : 'b
val table : 'c
val remove : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.remove


 Removes an element from the collection.
val table : 'b
val containsKey : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.containsKey


 Tests if an element is in collection.
val find : key:'a -> table:'b -> 'c

Full name: Script.ImmutableDict.find


 Looks up an element in the collection.
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


 Looks up an element in the collection, returning
 a Some value if the element is in the domain of the map,
 or None if not.
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


 Returns an array of all key/value pairs in the mapping.
val table : seq<'a>
val pair : 'a
val ofSeq : elements:seq<'Key * 'T> -> 'a

Full name: Script.ImmutableDict.ofSeq


 Returns a new collection made from the given bindings.
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


 Creates a new collection whose elements are the results of applying
 the given function to each of the elements of the collection.
val mapping : ('Key -> 'T -> 'U)
val filter : predicate:('Key -> 'T -> bool) -> table:seq<'a> -> 'b

Full name: Script.ImmutableDict.filter


 Creates a new collection containing only the bindings for which the given predicate returns true.
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


 Returns true if the given predicate returns true for one of the bindings in the collection.
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.exists
val forall : predicate:('Key -> 'T -> bool) -> table:seq<'a> -> bool

Full name: Script.ImmutableDict.forall


 Returns true if the given predicate returns true for all of the bindings in the collection.
val forall : predicate:('T -> bool) -> source:seq<'T> -> bool

Full name: Microsoft.FSharp.Collections.Seq.forall
val findKey : predicate:('Key -> 'T -> bool) -> table:seq<'a> -> 'b

Full name: Script.ImmutableDict.findKey


 Evaluates the function on each mapping in the collection.
 Returns the key for the first mapping where the function returns true.
val find : predicate:('T -> bool) -> source:seq<'T> -> 'T

Full name: Microsoft.FSharp.Collections.Seq.find
val pick : chooser:('Key -> 'T -> 'U option) -> table:seq<'a> -> 'U

Full name: Script.ImmutableDict.pick


 Evaluates the function on each mapping in the collection.
 Returns the key for the first mapping where the function returns true.
val chooser : ('Key -> 'T -> 'U option)
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
val pick : chooser:('T -> 'U option) -> source:seq<'T> -> 'U

Full name: Microsoft.FSharp.Collections.Seq.pick
val tryPick : chooser:('Key -> 'T -> 'U option) -> table:seq<'a> -> 'U option

Full name: Script.ImmutableDict.tryPick


 Searches the collection looking for the first element where the given function
 returns a Some value.
val tryPick : chooser:('T -> 'U option) -> source:seq<'T> -> 'U option

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

Full name: Script.ImmutableDict.iter


 Applies the given function to each binding in the collection
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
val fold : folder:('State -> 'Key -> 'T -> 'State) -> state:'State -> table:seq<'a> -> 'State

Full name: Script.ImmutableDict.fold


 Folds over the bindings in the collection
val folder : ('State -> 'Key -> 'T -> 'State)
val state : 'State
val fold : folder:('State -> 'T -> 'State) -> state:'State -> source:seq<'T> -> 'State

Full name: Microsoft.FSharp.Collections.Seq.fold
val acc : 'State

More information

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