Haskell 中 map 函数的 C# 等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2142935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the C# equivalent of map function in Haskell
提问by BM.
map function in Haskell has two input parameters. The first parameter is a function and second parameter is a list. The map function applies the function passed as input parameter to all the elements in the list and returns a new list.
Haskell 中的 map 函数有两个输入参数。第一个参数是一个函数,第二个参数是一个列表。map 函数将作为输入参数传递的函数应用于列表中的所有元素并返回一个新列表。
Is there a C# equivalent to this functionality?
是否有与此功能等效的 C#?
采纳答案by ChaosPandion
回答by Eric Lippert
And to answer a question you didn't ask, the Haskell equivalent of binding the "sequence" monad is called SelectMany in C#. See Wes Dyer's great article on this for details:
为了回答您没有问过的问题,在 C# 中,与绑定“序列”monad 的 Haskell 等效项称为 SelectMany。有关详细信息,请参阅 Wes Dyer 的精彩文章:
http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx
http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx
回答by C. A. McCann
Since Select
and SelectMany
were already mentioned, I'll answer an additional question you didn't ask: fold
is found as Aggregate.
由于Select
和SelectMany
已经被提及,我将回答您没有问的另一个问题:fold
被发现为Aggregate。
Now everyone reading this should be fully equipped to go be That Guy who writes Language X using Language Y idioms... so for the sake of your fellow C# programmers, don't get toocarried away.
现在每个人都读这应该是完全有能力去成为那个人用语言Y成语谁写的语言X ...所以你的同胞C#程序员的缘故,不要太忘乎所以。
回答by Annie Lagang
Another alternative to Select
and SelectMany
is to write your own extension method.
另一个备选方案Select
,并SelectMany
为编写自己的扩展方法。
public static IEnumerable<U> Map<T, U>(this IEnumerable<T> s, Func<T, U> f)
{
foreach (var item in s)
yield return f(item);
}
Thanks Wes Dyer for this sweet extension method! :) See postfor more details.
感谢 Wes Dyer 这种甜蜜的扩展方法!:) 有关更多详细信息,请参阅帖子。
回答by Adway Lele
How about ConvertAll? It looks like Closest to Map.
ConvertAll怎么样?它看起来最接近地图。