What is the C# equivalent of map function in Haskell

后端 未结 5 1002
礼貌的吻别
礼貌的吻别 2021-02-01 00:34

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 paramet

相关标签:
5条回答
  • 2021-02-01 00:38

    Select

    MSDN Reference

    See my question here (Only if you are curious as it is not directly related).

    0 讨论(0)
  • 2021-02-01 00:42

    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.

    0 讨论(0)
  • 2021-02-01 00:44

    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.

    0 讨论(0)
  • 2021-02-01 00:47

    How about ConvertAll? It looks like Closest to Map.

    0 讨论(0)
  • 2021-02-01 00:48

    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:

    http://blogs.msdn.com/wesdyer/archive/2008/01/11/the-marvels-of-monads.aspx

    0 讨论(0)
提交回复
热议问题