Here are a couple ways:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 

open System.Collections.Generic 

let Clone1(dic : Dictionary<_,_>) =
    let ret = new Dictionary<_,_>()
    for KeyValue(k,v) in dic do
        ret.[k] <- v
    ret

let Clone2(dic : Dictionary<_,_>) =
    let ret = new Dictionary<_,_>()
    for kvp in dic do
        ret.[kvp.Key] <- kvp.Value 
    ret    

The first uses the 'KeyValue' active pattern, and the second does things more straightforwardly. Does that help?

By on 12/17/2009 6:04 AM ()

This should also work:

1
2
3
open System.Collections.Generic 

let Clone (d:Dictionary<_,_>) = new Dictionary<_,_>(d)
By on 12/18/2009 5:20 AM ()

How about:

1
2
3
4
5
6
7
open System.Collections.Generic

let CloneDict (dict: Dictionary<_,_>) =
            let dict' = new Dictionary<_,_>()

            for x in dict do
                dict'.[x.Key] <- x.Value

The enumerator for the KeyValuePair is directly on the dictionary class, no need to call a method or property.

You could also consider using F#'s immutable maps if your going to be doing a lot of cloning of dicitionaries.

Cheers,
Rob

By on 12/17/2009 6:03 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper