Ejemplo n.º 1
0
        public static T Fold <T>(Fold函数 <T> func, T[] arr)
        {
            T val = arr[0];

            foreach (T item in arr)
            {
                val = func(val, item);
            }
            return(val);
        }
Ejemplo 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();
        }