Exemplo n.º 1
0
        public static T[] Map <T>(Map函数 <T> func, T[] arr)
        {
            List <T> list = new List <T>();

            foreach (T item in arr)
            {
                list.Add(func(item));
            }
            return(list.ToArray());
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var func1 = new Reduce函数 <int>(max);
            var func2 = new Reduce函数 <string>(longest);
            var func3 = new Map函数 <int>(plus1);
            var func4 = new Fold函数 <int>(plus2);

            Console.WriteLine(Reduce <int>(func1, new int[] { 1, 2, 3, 5, 100 }));                                    //Output: 100
            Console.WriteLine(Reduce <string>(func2, new string[] { "你好,世界", "Hello, world", "Bonjour, la monde" })); //Output: Bonjour, la monde
            var arr = Map <int>(func3, new int[] { 1, 2, 3, 4 });

            foreach (int item in arr)
            {
                Console.Write(string.Format("{0} ", item));
            }
            Console.WriteLine();
            Console.WriteLine(Fold <int>(func4, new int[] { 1, 2, 3, 4, 5, 6, 7 }));

            Console.ReadKey();
        }