Data records of some kind frequently have to be indexed by a unique key. Usually it looks something like this (I'm using C# because it's the language I'm most comfortable with, but this question isn't specific to it):
public class NamedRecord
{
public readonly string UniqueImmutableName;
...
}
public class UsesUsualDict
{
Dictionary<string, NamedRecord> myDict = new Dictionary<string, NamedRecord>();
void AddRecord(NamedRecord _NewRecord)
{
myDict[_NewRecord.UniqueImmutableName] = _NewRecord;
}
NamedRecord GetRecord(string _Key)
{
return myDict[_Key];
}
}
However, this seems a little redundant: keys in that dictionary should always be considered equal to NamedRecord.UniqueImmutableName
, but developer ends maintaining this relationship himself. Also, this kind of data duplication just doesn't feel right to me.
Sometimes I see a solution that is similar: data record doesn't even have the UniqueImmutableName as their member. For example, in his tutorial for D language, Andrei Alexandrescu uses data struct that depicts word statistics of character in a play:
struct PersonaData {
uint totalWordsSpoken;
uint[string] wordCount;
}
But the character's name isn't even in it — it's only available as a key to the dictionary these structs are saved in. Outside of that context, this data structure is almost useless.
What I want to do is something like that:
public class UsesLambdaDict
{
LambdaDictionary<string, NamedRecord> myDict = new LambdaDictionary<string, NamedRecord>(NamedRecord _Record => _Record.UniqueImmutableName);
void AddRecord(NamedRecord _NewRecord)
{
myDict.Add(_NewRecord);
}
NamedRecord GetRecord(string _Key)
{
return myDict[_Key];
}
}
It seems to me that this way of handling such data records is better, because the relationship between the NamedRecord's UniqueImmutableName member and the dictionary it's saved in is formalized at compile-time. The downside I see is that there's no way to ensure that given lambda will is a pure function, at least in C#. I don't really know D so well, but it seems that pure
keyword it has can't it guarantee neither.
So, I have several questions about this:
Dictionary is a built-in implementation of “Python Data Structure” which is a collection of “Key: Value” pairs. Dictionary is created using curly braces with key and value separated by semicolon {Key : Value}.
Dictionary keys must be of an immutable type. Strings and numbers are the two most commonly used data types as dictionary keys. We can also use tuples as keys but they must contain only strings, integers, or other tuples.
In addition to the curly braces, there are also colons ( : ) throughout the dictionary. The words to the left of the colons are the keys. Keys can be made up of any immutable data type.
only immutable type of objects like string, tuple or integer etc. can be used as keys in the dictionaries.
1.) I do not think so.
2.) Use structure with key and value, I do not think lambdas are necessary
3.) Performance issues (cache misses)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With