开发者

What is the C# equivalent of map function in Haskell

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.

Is there a开发者_StackOverflow中文版 C# equivalent to this functionality?


Select

MSDN Reference

See my question Why is the LINQ "apply-to-all" method named Select? (Only if you are curious as it is not directly related).


Another alternative to Select and SelectMany is to write your own extension method.

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 post for more details.


Since Select and SelectMany were already mentioned, I'll answer an additional question you didn't ask: fold is found as 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 too carried away.


How about ConvertAll? It looks like Closest to Map.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜