将列表聚合到字典中
Aggregate List into Dictionary
我正在尝试创建一个 Sprache 解析器,其中的一部分输入应该被解析到字典中
input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs
and=a;list=of;arbitrary=key;value=pairs
部分应该在 Dictionary 中结束。
为此我有
public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
public static Parser<KeyValuePair<string, string>> ParameterTuple =
from key in Key
from eq in Parse.Char('=')
from value in Value
select new KeyValuePair<string, string>(key, value);
和扩展方法
public static IEnumerable<T> Cons<T>(this T head, IEnumerable<T> rest)
{
yield return head;
foreach (var item in rest)
yield return item;
}
public static Parser<IEnumerable<T>> ListDelimitedBy<T>(this Parser<T> parser, char delimiter)
{
return
from head in parser
from tail in Parse.Char(delimiter).Then(_ => parser).Many()
select head.Cons(tail);
}
(从示例中复制)
然后我尝试了
public static Parser<IEnumerable<KVP>> KeyValuePairList = KVPair.ListDelimitedBy(';'); // KVP is just an alias for KeyValuePair<string,string>
现在我被困在如何做类似
的事情上
public static Parser<???> Configuration =
from fixedstuff in FixedStuff
from kvps in Parse.Char(';').Then(_ => KeyValuePairList)
select new ???(fixedstuff, MakeDictionaryFrom(kvps))
或类似的东西。
我如何将任意 key=value[;] 对解析为字典?
你是这个意思吗?
public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
public static Parser<KeyValuePair<string, string>> ParameterTuple =
from key in Key
from eq in Parse.Char('=')
from value in Value
select new KeyValuePair<string, string>(key, value);
public static Parser<string> FixedStuff = Parse.String("input=some/fixed/stuff").Text();
public static Parser<Config> Configuration =
from fixedStuff in FixedStuff
from _ in Parse.Char(';')
from kvps in ParameterTuple.DelimitedBy(Parse.Char(';'))
select new Config(fixedStuff, kvps.ToDictionary(x => x.Key, x => x.Value));
public class Config
{
public Config(string fixedStuff, IReadOnlyDictionary<string, string> dict)
{
FixedStuff = fixedStuff;
Dict = dict;
}
public string FixedStuff { get; }
public IReadOnlyDictionary<string, string> Dict { get; }
}
[Fact]
public void ParseIt()
{
Configuration.Parse("input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs");
}
从任何 IEnumerable 中创建字典实际上非常容易,只需确保包含 System.Linq。这实际上只是查询正确并理解它的问题,在您的情况下是实际的字符串解析,所以我将其留给您。下面的代码构建了一个字典,你只需要提供字符串和解析。
//If the parse is simple it can be done inline with the dictionary creation
private string GenerateKey(string fullString)
{
//parse key from original string and return
}
//If the parse is simple it can be done inline with the dictionary creation
private string GenerateValue(string fullString)
{
//Parse Values from your original string and return
}
private void UsageMethod(IEnumerable<fullString> sprachs)
{
var dictionary = sprachs.ToDictionary(
fString => GenerateKey(fString), //The Key
fString => GenerateValue(fString) //The Value
);
//Now you can use Dicitonary as it is a IDictionary<string,string>
// so it too can be overriden an extended if need be
}
我正在尝试创建一个 Sprache 解析器,其中的一部分输入应该被解析到字典中
input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs
and=a;list=of;arbitrary=key;value=pairs
部分应该在 Dictionary
为此我有
public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
public static Parser<KeyValuePair<string, string>> ParameterTuple =
from key in Key
from eq in Parse.Char('=')
from value in Value
select new KeyValuePair<string, string>(key, value);
和扩展方法
public static IEnumerable<T> Cons<T>(this T head, IEnumerable<T> rest)
{
yield return head;
foreach (var item in rest)
yield return item;
}
public static Parser<IEnumerable<T>> ListDelimitedBy<T>(this Parser<T> parser, char delimiter)
{
return
from head in parser
from tail in Parse.Char(delimiter).Then(_ => parser).Many()
select head.Cons(tail);
}
(从示例中复制)
然后我尝试了
public static Parser<IEnumerable<KVP>> KeyValuePairList = KVPair.ListDelimitedBy(';'); // KVP is just an alias for KeyValuePair<string,string>
现在我被困在如何做类似
的事情上public static Parser<???> Configuration =
from fixedstuff in FixedStuff
from kvps in Parse.Char(';').Then(_ => KeyValuePairList)
select new ???(fixedstuff, MakeDictionaryFrom(kvps))
或类似的东西。
我如何将任意 key=value[;] 对解析为字典?
你是这个意思吗?
public static Parser<string> Key = Parse.CharExcept('=').Many().Text();
public static Parser<string> Value = Parse.CharExcept(';').Many().Text();
public static Parser<KeyValuePair<string, string>> ParameterTuple =
from key in Key
from eq in Parse.Char('=')
from value in Value
select new KeyValuePair<string, string>(key, value);
public static Parser<string> FixedStuff = Parse.String("input=some/fixed/stuff").Text();
public static Parser<Config> Configuration =
from fixedStuff in FixedStuff
from _ in Parse.Char(';')
from kvps in ParameterTuple.DelimitedBy(Parse.Char(';'))
select new Config(fixedStuff, kvps.ToDictionary(x => x.Key, x => x.Value));
public class Config
{
public Config(string fixedStuff, IReadOnlyDictionary<string, string> dict)
{
FixedStuff = fixedStuff;
Dict = dict;
}
public string FixedStuff { get; }
public IReadOnlyDictionary<string, string> Dict { get; }
}
[Fact]
public void ParseIt()
{
Configuration.Parse("input=some/fixed/stuff;and=a;list=of;arbitrary=key;value=pairs");
}
从任何 IEnumerable 中创建字典实际上非常容易,只需确保包含 System.Linq。这实际上只是查询正确并理解它的问题,在您的情况下是实际的字符串解析,所以我将其留给您。下面的代码构建了一个字典,你只需要提供字符串和解析。
//If the parse is simple it can be done inline with the dictionary creation
private string GenerateKey(string fullString)
{
//parse key from original string and return
}
//If the parse is simple it can be done inline with the dictionary creation
private string GenerateValue(string fullString)
{
//Parse Values from your original string and return
}
private void UsageMethod(IEnumerable<fullString> sprachs)
{
var dictionary = sprachs.ToDictionary(
fString => GenerateKey(fString), //The Key
fString => GenerateValue(fString) //The Value
);
//Now you can use Dicitonary as it is a IDictionary<string,string>
// so it too can be overriden an extended if need be
}