Google offre un sacco di esempi di aggiunta e cancellazione di voci in un dizionario F # (o altra raccolta). Ma non vedo esempi per l'equivalente diCome si dichiarano i valori di una voce del dizionario come mutabili?
myDict["Key"] = MyValue;
Ho provato
myDict.["Key"] <- MyValue
ho anche tentato di dichiarare il dizionario come
Dictionary<string, mutable string>
così diverse varianti su Questo. Tuttavia, non ho ancora trovato la combinazione corretta ... se è è effettivamente possibile in F #.
Edit: il codice incriminato è:
type Config(?fileName : string) =
let fileName = defaultArg fileName @"C:\path\myConfigs.ini"
static let settings =
dict[ "Setting1", "1";
"Setting2", "2";
"Debug", "0";
"State", "Disarray";]
let settingRegex = new Regex(@"\s*(?<key>([^;#=]*[^;#= ]))\s*=\s*(?<value>([^;#]*[^;# ]))")
do File.ReadAllLines(fileName)
|> Seq.map(fun line -> settingRegex.Match(line))
|> Seq.filter(fun mtch -> mtch.Success)
|> Seq.iter(fun mtch -> settings.[mtch.Groups.Item("key").Value] <- mtch.Groups.Item("value").Value)
L'errore che sto ottenendo è:
System.NotSupportedException: This value may not be mutated
at [email protected]_Item(K key, V value)
at <StartupCode$FSI_0036>[email protected](Match mtch)
at Microsoft.FSharp.Collections.SeqModule.iter[T](FastFunc`2 action, IEnumerable`1 sequence)
at FSI_0036.Utilities.Config..ctor(Option`1 fileName)
at <StartupCode$FSI_0041>[email protected]()
stopped due to error
il tempo di attività dei siti di ricerca è davvero piuttosto scadente. ecco il link alla cache di google http: http:// 0909.229.132/search?q=cache:GGIlRD9kBnYJ:research.microsoft.com/en-us/um/cambridge/projects/fsharp/manual/fsharp.core/microsoft.fsharp. collections.map.html + f% 23 + microsoft.fsharp.collections.map & cd = 1 & hl = it & ct = clnk & gl = uk – ShuggyCoUk
Si noti che la ricerca in un albero binario è O (log n), non O (n log n) come indicato. – kvb
oops - typo thanks – ShuggyCoUk