C#版的MapReduce

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mapReduce
{
    public static class helper
    {
        public static Dictionary<TKey, TResult> MapReduce<TInput, TKey, TValue, TResult>(
           this IEnumerable<TInput> list,
           //Func<TInput, IEnumerable<KeyValuePair<TKey, TValue>>> map,
                   Func<TInput, 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))
                //{
                var one = map(item);
                if (Convert.ToInt32( one.Key) == 0)
                {
                    continue;
                }
                    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;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace mapReduce
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>();
            list.Add(new Person { ID = 1, Name = "肖银龙", Age = 23 });
            list.Add(new Person { ID = 2, Name = "郭彦磊", Age = 24 });
            list.Add(new Person { ID = 3, Name = "王老师", Age = 23 });
            list.Add(new Person { ID = 4, Name = "路人甲", Age = 25 });
            list.Add(new Person { ID = 5, Name = "路人乙", 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);
            }

            Console.Read();
        }

        //public static IEnumerable<KeyValuePair<int, string>> Map(Person p)
        //{
        //    if (p.Age > 22)
        //        yield return new KeyValuePair<int, string>(p.Age, p.Name);
               
        //}

        public static KeyValuePair<int, string> Map(Person p)
        {
            if (p.Age > 22)
                return new KeyValuePair<int, string>(p.Age, p.Name);
            else
                return new KeyValuePair<int, string>(0,null);

        }

    }

    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值