Implementation of MapReduce method in C #

Source: Internet
Author: User

If you do not know how mapreduce is working, please see here, if you do not know what MapReduce is, please google!

Today "Leisure" to come to nothing, suddenly think of C # no MapReduce method, the idea of the 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, L
            ist<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

Note: Because you can emit multiple times in the map method, you return IEnumerable here, as you can see in the example below, with yield returns.

Cases:

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 >) yield return to new Keyvaluepair<int, string> (P.age, p.name); }

What the above program does is that the statistics age is greater than 22, who are all ages, show such as:

C:\Windows\system32\cmd.exe

23:user1,user3

24:user2

25:user4

Please press any key to continue ...

(Too much trouble uploading pictures, get an HTML version of the console, forgive me!) )

Someone will certainly ask why map does not use lambda expressions like the reduce method, because yield return cannot be in anonymous methods and lambda expressions! Ms indicates that the problem has been known, but rewriting yield is expensive and will certainly be resolved in the future!

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/csharp/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.