Skip to content

Instantly share code, notes, and snippets.

@savaged
Last active February 4, 2023 08:45
Show Gist options
  • Save savaged/69b2e42c41b03d1fd2aadd4e78080640 to your computer and use it in GitHub Desktop.
Save savaged/69b2e42c41b03d1fd2aadd4e78080640 to your computer and use it in GitHub Desktop.
Demonstrating that the equivalent to the Haskell map function in C# is the Select in Linq
/*
* Trying to emulate the following Haskell, with the aim of demonstrating
* that the map function in C# is the Select in Linq...
*
* main = do
* print (reverseEach ["map", "select"])
*
* reverseEach l = map reverse l
*
*/
Console.WriteLine(ReverseEach(new List<string> { "map", "select" }));
string ReverseEach(IEnumerable<string> l) =>
l.Select(s => s.Reverse()).Aggregate((a, b) => $"{a} {b}");
static class StringEx
{
public static string Reverse(this string s) => new string(s.ToCharArray().Reverse().ToArray());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment