C# .NET 中的映射和缩减

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/428798/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:59:45  来源:igfitidea点击:

Map and Reduce in .NET

c#mapreduce

提问by Developer

What scenarios would warrant the use of the "Map and Reduce" algorithm?

哪些场景可以保证使用“ Map and Reduce”算法?


Is there a .NET implementation of this algorithm?


这个算法有 .NET 实现吗?

采纳答案by Tony

Linq equivalents of Map and Reduce: If you're lucky enough to have linq then you don't need to write your own map and reduce functions. C# 3.5 and Linq already has it albeit under different names.

Map 和 Reduce 的 Linq 等价物:如果您有幸拥有 linq,那么您无需编写自己的 map 和 reduce 函数。C# 3.5 和 Linq 已经有了它,尽管名称不同。

  • Map is Select:

    Enumerable.Range(1, 10).Select(x => x + 2);
    
  • Reduce is Aggregate:

    Enumerable.Range(1, 10).Aggregate(0, (acc, x) => acc + x);
    
  • Filter is Where:

    Enumerable.Range(1, 10).Where(x => x % 2 == 0);
    
  • 地图是Select

    Enumerable.Range(1, 10).Select(x => x + 2);
    
  • 减少是Aggregate

    Enumerable.Range(1, 10).Aggregate(0, (acc, x) => acc + x);
    
  • 过滤器是Where

    Enumerable.Range(1, 10).Where(x => x % 2 == 0);
    

https://www.justinshield.com/2011/06/mapreduce-in-c/

https://www.justinshield.com/2011/06/mapreduce-in-c/

回答by Joel Martinez

The classes of problem that are well suited for a mapreduce style solution are problems of aggregation. Of extracting data from a dataset. In C#, one could take advantage of LINQ to program in this style.

非常适合 mapreduce 风格解决方案的问题类别是聚合问题。从数据集中提取数据。在 C# 中,可以利用 LINQ 以这种方式进行编程。

From the following article: http://codecube.net/2009/02/mapreduce-in-c-using-linq/

来自以下文章:http: //codecube.net/2009/02/mapreduce-in-c-using-linq/

the GroupBy method is acting as the map, while the Select method does the job of reducing the intermediate results into the final list of results.

GroupBy 方法充当映射,而 Select 方法负责将中间结果减少到最终结果列表中。

var wordOccurrences = words
                .GroupBy(w => w)
                .Select(intermediate => new
                {
                    Word = intermediate.Key,
                    Frequency = intermediate.Sum(w => 1)
                })
                .Where(w => w.Frequency > 10)
                .OrderBy(w => w.Frequency);

For the distributed portion, you could check out DryadLINQ: http://research.microsoft.com/en-us/projects/dryadlinq/default.aspx

对于分布式部分,您可以查看 DryadLINQ:http://research.microsoft.com/en-us/projects/dryadlinq/default.aspx

回答by CsUtil.com

Since I never can remember that LINQ calls it Where, Selectand Aggregateinstead of Filter, Mapand Reduceso I created a few extension methods you can use:

因为我从来不记得是LINQ调用它WhereSelectAggregate代替FilterMapReduce让我创建了一些扩展方法,你可以使用:

IEnumerable<string> myStrings = new List<string>() { "1", "2", "3", "4", "5" };
IEnumerable<int> convertedToInts = myStrings.Map(s => int.Parse(s));
IEnumerable<int> filteredInts = convertedToInts.Filter(i => i <= 3); // Keep 1,2,3
int sumOfAllInts = filteredInts.Reduce((sum, i) => sum + i); // Sum up all ints
Assert.Equal(6, sumOfAllInts); // 1+2+3 is 6

Here are the 3 methods (from https://github.com/cs-util-com/cscore/blob/master/CsCore/PlainNetClassLib/src/Plugins/CsCore/com/csutil/collections/IEnumerableExtensions.cs):

以下是 3 种方法(来自https://github.com/cs-util-com/cscore/blob/master/CsCore/PlainNetClassLib/src/Plugins/CsCore/com/csutil/collections/IEnumerableExtensions.cs):

public static IEnumerable<R> Map<T, R>(this IEnumerable<T> self, Func<T, R> selector) {
    return self.Select(selector);
}

public static T Reduce<T>(this IEnumerable<T> self, Func<T, T, T> func) {
    return self.Aggregate(func);
}

public static IEnumerable<T> Filter<T>(this IEnumerable<T> self, Func<T, bool> predicate) {
    return self.Where(predicate);
}

Some more details from https://github.com/cs-util-com/cscore#ienumerable-extensions:

来自https://github.com/cs-util-com/cscore#ienumerable-extensions 的更多细节:

enter image description here

在此处输入图片说明