Cri*_*onX 3 c# lambda type-inference
所以我想了解更多关于lambda表达式的知识.我在stackoverflow上阅读了这个问题,与所选答案一致,并尝试使用简单的LINQ表达式在C#中使用控制台应用程序来实现该算法.
我的问题是:如何将lambda表达式的"var result"转换为可用的对象,然后我可以打印结果?
我还要感谢在我宣布这一过程时对正在发生的事情的深入解释 outer => outer.Value.Frequency
(我已经读过很多关于lambda表达式的解释,但是额外的澄清会有所帮助)
C#
//Input : {5, 13, 6, 5, 13, 7, 8, 6, 5}
//Output : {5, 5, 5, 13, 13, 6, 6, 7, 8}
//The question is to arrange the numbers in the array in decreasing order of their frequency, preserving the order of their occurrence.
//If there is a tie, like in this example between 13 and 6, then the number occurring first in the input array would come first in the output array.
List<int> input = new List<int>();
input.Add(5);
input.Add(13);
input.Add(6);
input.Add(5);
input.Add(13);
input.Add(7);
input.Add(8);
input.Add(6);
input.Add(5);
Dictionary<int, FrequencyAndValue> dictionary = new Dictionary<int, FrequencyAndValue>();
foreach (int number in input)
{
if (!dictionary.ContainsKey(number))
{
dictionary.Add(number, new FrequencyAndValue(1, number) );
}
else
{
dictionary[number].Frequency++;
}
}
var result = dictionary.OrderByDescending(outer => outer.Value.Frequency);
// How to translate the result into something I can print??
Run Code Online (Sandbox Code Playgroud)
有关打印命令的答案,请参阅我的答案.
Eri*_*ert 27
如何将lambda表达式的"var result"转换为可用的对象,然后我可以打印结果?
首先,"lambda表达式"只是表达式的一部分a=>b.查询的其余部分只是一个以lambda作为参数的方法调用.
无论如何,如果我可以教人们关于LINQ的一件事,那就是:"结果"不是查询的结果,而是查询本身.
如果要查看结果,请询问每个结果的查询:
foreach(var item in result)
Console.WriteLine(item.ToString());
Run Code Online (Sandbox Code Playgroud)
我还要感谢我在声明outer => outer.Value.Frequency时对正在发生的事情的深入解释.
当然.我们首先计算所涉及的所有类型.我们看到lambda是一个函数,它接受KeyValuePair并返回一个int,因此我们生成一个方法
static private int MyLambda(KeyValuePair<int, FrequencyAndValue> outer)
{
return outer.Value.Frequency;
}
Run Code Online (Sandbox Code Playgroud)
接下来,我们采用该方法并从中创建委托:
var result = dictionary.OrderByDescending(
new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));
Run Code Online (Sandbox Code Playgroud)
并重写扩展方法调用:
var result = Enumerable.OrderByDescending<KeyValuePair<int, FrequencyAndValue>, int>(
dictionary,
new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));
Run Code Online (Sandbox Code Playgroud)
并重写var:
IOrderedEnumerable<KeyValuePair<int, FrequencyAndValue>> result =
Enumerable.OrderByDescending<KeyValuePair<int, FrequencyAndValue>, int>(
dictionary,
new Func<KeyValuePair<int, FrequencyAndValue>, int>(MyLambda));
Run Code Online (Sandbox Code Playgroud)
我希望你同意你输入的代码比这个混乱更具可读性.类型推断岩石.
结果是一个对象,它表示通过给定键对此字典进行排序的能力.仔细阅读:它代表了通过该键对字典进行排序的能力.在你要求结果之前,它实际上并没有这样做; 到目前为止,它只是一个对象,说"当被要求获得结果时,按此键对字典进行排序".
假设你要求一个结果.它如何计算排序列表?它询问每个元素的字典.然后它在每个元素上调用MyLambda,它返回一个整数,所以我们现在有一对字典键值对和整数.然后它构建一个按该整数排序的对列表.然后,当你要求时,它会一次分发一个列表中的元素.
我们看到lambda是一个函数,它接受一个KeyValuePair并返回一个int" - 你是如何确定的?我没有从方法返回值中看到它,也没有记录在OrderByDescending()中.
啊,我看到了混乱; 出于教学原因,我对语义分析的确切顺序略微提出了一点.
我们如何进行这种类型的推理是C#中更微妙和有趣的部分之一.
这是它的工作原理.
我们看到OrderByDescending被声明为:
static IOrderedEnumerable<T> OrderByDescending<T, K>(
this IEnumerable<T> sequence,
Func<T, K> keyExtractor)
Run Code Online (Sandbox Code Playgroud)
我们看到我们可能会调用此方法:
OrderByDescending(dictionary, o=>o.Value.Frequency)
Run Code Online (Sandbox Code Playgroud)
但我们不知道T和K是什么.所以我们首先看一下不是lambda的东西.你的字典实现IEnumerable<KeyValuePair<int, FrequencyOrValue>>所以我们开始说"T可能KeyValuePair<int, FrequencyOrValue>".
在这一点上,我们没有别的东西可以从不是lambdas的东西中推断出来,所以我们开始看看lambdas.我们看到我们有一个lambda o=>o.Value.Frequency,到目前为止我们已经确定keyExtractor的类型是Func<KeyValuePair<int, FrequencyOrValue>, K>,我们仍然在寻找K.所以我们假设lambda实际上是:
(KeyValuePair<int, FrequencyOrValue> o)=>{return o.Value.Frequency;}
Run Code Online (Sandbox Code Playgroud)
我们问它是否绑定?是!是的,它确实.我们可以成功地编译这个lambda而没有错误,当我们这样做时,我们看到它的所有返回语句都返回一个int.
因此我们推断出K是int,我们现在对整个事物进行完整的类型分析.
这是一个相当直接的推论; 他们可以变得更加怪异.如果此主题特别引起您的兴趣,请参阅我博客上的"类型推断"存档.
http://blogs.msdn.com/ericlippert/archive/tags/Type+Inference/default.aspx
特别是,这里有一段视频,我解释了上面的内容以及其他一些有趣的案例:
http://blogs.msdn.com/ericlippert/archive/2006/11/17/a-face-made-for-email-part-three.aspx
| 归档时间: |
|
| 查看次数: |
1124 次 |
| 最近记录: |