<p>I am writing a few extensions to mimic the map and reduce functions in Lisp.</p>

<pre><code>public delegate R ReduceFunction&lt;T,R&gt;(T t, R previous);
public delegate void TransformFunction&lt;T&gt;(T t, params object[] args);

public static R Reduce&lt;T,R&gt;(this List&lt;T&gt; list, ReduceFunction&lt;T,R&gt; r, R initial)
{
     var aggregate = initial;
     foreach(var t in list)
         aggregate = r(t,aggregate);

     return aggregate;
}
public static void Transform&lt;T&gt;(this List&lt;T&gt; list, TransformFunction&lt;T&gt; f, params object [] args)
{
    foreach(var t in list)
         f(t,args);
}
</code></pre>

<p>The transform function will cut down on cruft like:</p>

<pre><code>foreach(var t in list)
    if(conditions &amp;&amp; moreconditions)
        //do work etc
</code></pre>

<p>Does this make sense? Could it be better?</p>

[{'answer_id': 40084, 'author': 'Jake Pearson', 'author_id': 632, 'author_profile': 'https://Stackoverflow.com/users/632', 'pm_score': 2, 'selected': False, 'text': '<p>I would use the built in Func delegates instead.  This same code would work on any IEnumerable.  Your code would turn into:</p>\n\n<pre><code>public static R Reduce&lt;T,R&gt;(this IEnumerable&lt;T&gt; list, Func&lt;T,R&gt; r, R initial)\n{\n     var aggregate = initial;\n     foreach(var t in list)\n         aggregate = r(t,aggregate);\n\n     return aggregate;\n}\npublic static void Transform&lt;T&gt;(this IEnumerable&lt;T&gt; list, Func&lt;T&gt; f)\n{\n    foreach(var t in list)\n             f(t);\n}\n</code></pre>\n'}, {'answer_id': 40089, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 1, 'selected': False, 'text': '<p>You might want to add a way to do a map but return a new list, instead of working on the list passed in (and returning the list can prove useful to chain other operations)... perhaps an overloaded version with a boolean that indicates if you want to return a new list or not, as such:</p>\n\n<pre><code>public static List&lt;T&gt; Transform&lt;T&gt;(this List&lt;T&gt; list, TransformFunction&lt;T&gt; f,\n        params object [] args)\n{\n    return Transform(list, f, false, args);\n}\n\npublic static List&lt;T&gt; Transform&lt;T&gt;(this List&lt;T&gt; list, TransformFunction&lt;T&gt; f,\n        bool create, params object [] args)\n{\n    // Add code to create if create is true (sorry,\n    // too lazy to actually code this up)\n    foreach(var t in list)\n         f(t,args);\n    return list;\n}\n</code></pre>\n'}, {'answer_id': 40148, 'author': 'Keith', 'author_id': 905, 'author_profile': 'https://Stackoverflow.com/users/905', 'pm_score': 6, 'selected': True, 'text': "<p>These look very similar to extensions in Linq already:</p>\n\n<pre><code>//takes a function that matches the Func&lt;T,R&gt; delegate\nlistInstance.Aggregate( \n    startingValue, \n    (x, y) =&gt; /* aggregate two subsequent values */ );\n\n//takes a function that matches the Action&lt;T&gt; delegate\nlistInstance.ForEach( \n    x =&gt; /* do something with x */);\n</code></pre>\n\n<p>Why is the 2nd example called Transform? Do you intend to change the values in the list somehow? If that's the case you may be better off using <code>ConvertAll&lt;T&gt;</code> or <code>Select&lt;T&gt;</code>.</p>\n"}, {'answer_id': 278287, 'author': 'Ray', 'author_id': 4872, 'author_profile': 'https://Stackoverflow.com/users/4872', 'pm_score': 6, 'selected': False, 'text': '<p>According to this link <a href="http://www.25hoursaday.com/weblog/2008/06/16/FunctionalProgrammingInC30HowMapReduceFilterCanRockYourWorld.aspx" rel="noreferrer">Functional Programming in C# 3.0: How Map/Reduce/Filter can Rock your World</a> the following are the equivalent in C# under the System.Linq namespace:</p>\n\n<ul>\n<li>map    --> <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.select.aspx" rel="noreferrer">Enumerable.Select</a></li>\n<li>reduce --> <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.aggregate.aspx" rel="noreferrer">Enumerable.Aggregate</a></li>\n<li>filter --> <a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.where.aspx" rel="noreferrer">Enumerable.Where</a></li>\n</ul>\n'}, {'answer_id': 54801689, 'author': 'CsUtil.com', 'author_id': 10808596, 'author_profile': 'https://Stackoverflow.com/users/10808596', 'pm_score': 1, 'selected': False, 'text': '<p>I would recommend to create extension methods that internally use LinQ <a href="https://github.com/cs-util-com/cscore/blob/master/CsCore/PlainNetClassLib/src/Plugins/CsCore/com/csutil/collections/IEnumerableExtensions.cs" rel="nofollow noreferrer">like this</a>:</p>\n\n<pre><code>public static IEnumerable&lt;R&gt; Map&lt;T, R&gt;(this IEnumerable&lt;T&gt; self, Func&lt;T, R&gt; selector) {\n    return self.Select(selector);\n}\n\npublic static T Reduce&lt;T&gt;(this IEnumerable&lt;T&gt; self, Func&lt;T, T, T&gt; func) {\n    return self.Aggregate(func);\n}\n\npublic static IEnumerable&lt;T&gt; Filter&lt;T&gt;(this IEnumerable&lt;T&gt; self, Func&lt;T, bool&gt; predicate) {\n    return self.Where(predicate);\n}\n</code></pre>\n\n<p>Here some example usages:</p>\n\n<pre><code>IEnumerable&lt;string&gt; myStrings = new List&lt;string&gt;() { "1", "2", "3", "4", "5" };\nIEnumerable&lt;int&gt; convertedToInts = myStrings.Map(s =&gt; int.Parse(s));\nIEnumerable&lt;int&gt; filteredInts = convertedToInts.Filter(i =&gt; i &lt;= 3); // Keep 1,2,3\nint sumOfAllInts = filteredInts.Reduce((sum, i) =&gt; sum + i); // Sum up all ints\nAssert.Equal(6, sumOfAllInts); // 1+2+3 is 6\n</code></pre>\n\n<p>(See <a href="https://github.com/cs-util-com/cscore#ienumerable-extensions" rel="nofollow noreferrer">https://github.com/cs-util-com/cscore#ienumerable-extensions</a> for more examples)</p>\n'}]
['https://Stackoverflow.com/questions/40075', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1455/']