let students =
Map.empty.
Add("ABC", "97").
Add("Jill", "98");;
printfn "Map - students: %A" students
This I know will create a map named students. I was thinking a way to create a copy of this map, with same key and value. I checked the documentation for f#, and didn't find any methods that clone/create a copy of map.
F# maps are immutable. This means that "changing" a map actually means creating a copy with some slight changes. The copy process uses tricks to make it much faster than a full copy. The new version can actually point back to the old version for most of the data. This is safe to do because the maps are immutable: they can never change.
This means that there is no possible reason to copy a map, except to warm up your CPU 😆
The same applies to List
and Set
in F#, as they are also immutable. It takes a while to get used to working with immutable data structures but life gets easier once you do.
By the way you can create the map with less code like this:
let students =
[ "ABC", "97"
"Jill", "98" ]
|> Map
Here's one way to do it.
let copy =
students
|> Seq.map (fun pair -> pair.Key, pair.Value)
|> Map
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.