Ton*_*ony 253
Linq等同于Map和Reduce:如果你有幸拥有linq,那么你不需要编写自己的地图并减少功能.C#3.5和Linq已经拥有它,虽然名称不同.
Enumerable.Range(1, 10).Select(x => x + 2);
Run Code Online (Sandbox Code Playgroud)
https://www.justinshield.com/2011/06/mapreduce-in-c/
Joe*_*nez 18
非常适合mapreduce风格解决方案的问题类别是聚合问题.从数据集中提取数据.在C#中,可以利用LINQ以这种方式编程.
来自以下文章:http: //codecube.net/2009/02/mapreduce-in-c-using-linq/
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);
Run Code Online (Sandbox Code Playgroud)
对于分布式部分,您可以查看DryadLINQ:http: //research.microsoft.com/en-us/projects/dryadlinq/default.aspx
小智 7
因为我从来不记得是LINQ调用它Where
,Select
并Aggregate
代替Filter
,Map
并Reduce
让我创建了一些扩展方法,你可以使用:
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
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
来自https://github.com/cs-util-com/cscore#ienumerable-extensions 的更多细节:
归档时间: |
|
查看次数: |
93891 次 |
最近记录: |