3 people like it.

Manage Variable Length Dictionary Entries

Populate and displaythe contents of a dictionary with a variable number of nested records - useful if you want to store and retrieve an arbitrary set of data from a database or other data source

 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: 
open System
open System.Collections.Generic

// Dictionary Demo Script for a set of Nested Data Records
// In this example the dictionary value is defined to contain a 
// variable number of records of the specified type.
// (In this example the record is just an integer value)  
 
// Define Record Types 
type  tinfo = { 
    num:int;
}

let populate_dict = 
    // Populate a dictionary with a nested set of records and display contents
    // Define a dictionary to hold nested record
    let mydict = new Dictionary<string, tinfo list>()
    
    // Add some arbitrary data 
    mydict.Add("bb", [{num=2;}; {num=3;}])
    mydict.Add("a", [{num=1;}])
    mydict.Add("ccc", [{num=4;}; {num=5;}; {num = 6;}])
    mydict


let print_dict (mydict)= 
    // Print Contents ( Keys and Values) in dictionary
    mydict 
    // Sort by key
    |> Seq.sortBy(fun ( KeyValue(k,v)) -> k)
    // Iterate over dict entries
    |> Seq.iter (fun  (KeyValue(k,v)) ->  
                    printfn "Dict Key: %s" k; 
                    v |>
                    // Now iterate over record list
                    Seq.iter (fun v -> 
                        printfn "Dict Values: %A;" v) )
    

// Main Entry Point
let main() =

    printfn "\n>>>>>> F# Dictionary Demo  <<<<< \n" 

    let d = populate_dict
    print_dict(d)

    printfn "\n>>>>>> Done  <<<<< \n" 

    printfn "\n>>>>>> Press Any Key to Exit! \n"
    System.Console.ReadLine() |> ignore

main()
namespace System
namespace System.Collections
namespace System.Collections.Generic
type tinfo =
  {num: int;}

Full name: Script.tinfo
tinfo.num: int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val populate_dict : Dictionary<string,tinfo list>

Full name: Script.populate_dict
val mydict : Dictionary<string,tinfo list>
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
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Dictionary.Add(key: string, value: tinfo list) : unit
val print_dict : mydict:seq<KeyValuePair<string,#seq<'b>>> -> unit

Full name: Script.print_dict
val mydict : seq<KeyValuePair<string,#seq<'b>>>
module Seq

from Microsoft.FSharp.Collections
val sortBy : projection:('T -> 'Key) -> source:seq<'T> -> seq<'T> (requires comparison)

Full name: Microsoft.FSharp.Collections.Seq.sortBy
active recognizer KeyValue: KeyValuePair<'Key,'Value> -> 'Key * 'Value

Full name: Microsoft.FSharp.Core.Operators.( |KeyValue| )
val k : string
val v : #seq<'b>
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val v : 'b
val main : unit -> unit

Full name: Script.main
val d : Dictionary<string,tinfo list>
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
Console.ReadLine() : string
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
Raw view New version

More information

Link:http://fssnip.net/3R
Posted:9 years ago
Author:Brendan Campbell
Tags: dictionary , nested , data