如果不知道MapReduce是怎麼工作的,請看這裡,如果不知道MapReduce是什麼,請google之!
今天“閒”來無事,忽想起C#裡沒有MapReduce的方法,構思之,coding之:
#region IEnumerable<T>.MapReduce public static Dictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>( this IEnumerable<TInput> list, Func<TInput, IEnumerable<KeyValuePair<TKey, TValue>>> map, Func<TKey, IEnumerable<TValue>, TResult> reduce) { Dictionary<TKey, List<TValue>> mapResult = new Dictionary<TKey, List<TValue>>(); foreach (var item in list) { foreach (var one in map(item)) { List<TValue> mapValues; if (!mapResult.TryGetValue(one.Key, out mapValues)) { mapValues = new List<TValue>(); mapResult.Add(one.Key, mapValues); } mapValues.Add(one.Value); } } var result = new Dictionary<TKey, TResult>(); foreach (var m in mapResult) { result.Add(m.Key, reduce(m.Key, m.Value)); } return result; } #endregion
注:由於在map方法裡可emit多次,所以這裡返回IEnumerable,下文例子中可以看到用yield return來實現。
例:
public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }
static void Main(string[] args) { List<Person> list=new List<Person> (); list.Add(new Person { ID=1, Name="user1", Age=23 }); list.Add(new Person { ID = 2, Name = "user2", Age = 24 }); list.Add(new Person { ID = 3, Name = "user3", Age = 23 }); list.Add(new Person { ID = 4, Name = "user4", Age = 25 }); list.Add(new Person { ID = 5, Name = "user5", Age = 20 }); var result = list.MapReduce<Person, int, string, string>(Map, (key, values) => string.Join(",", values)); foreach (var d in result) { Console.WriteLine(d.Key + ":" + d.Value); } } public static IEnumerable<KeyValuePair<int, string>> Map(Person p) { if (p.Age > 22) yield return new KeyValuePair<int, string>(p.Age, p.Name); }
上面程序所做的事為統計年齡大於22的,各個年齡都有誰,顯示如:
C:\Windows\system32\cmd.exe
23:user1,user3
24:user2
25:user4
請按任意鍵繼續。。。
(嫌上傳圖片太麻煩,弄了個html版控制台,見諒!)
肯定有人會問為什麼map不像reduce方法一樣用lambda表達式,因為yield return不能在匿名方法和lambda表達式中!MS表示已知道這個問題,但重寫yield花費很大,將來肯定會解決!
查看本欄目