C # functional programming style-Implementation of Fan Filter, Map, and CT Functions

Source: Internet
Author: User

In the morning, I read an article about lambda and closure from yuanyou, and suddenly I think of Python I just learned. I was so happy when I first came into contact with FP. The yearning for the no-side-effect of FP and my love for Declaration-type programming have led me to a change in programming and another idea of programming.

I still remember seeing the built-in function Filter, Map, and Reduce in Python, wondering why. NET's BCL didn't exist. After C #3.0 is released, you can basically DIY a cottage Filter, Map, and Reduce. Check the Code:

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace FuncPro{    class Program    {        static T[] Filter<T>(Func<T, bool> func, IEnumerable<T> iSource)        {            List<T> iResult = new List<T>();            foreach (var item in iSource)            {                if (func(item))                    iResult.Add(item);            }            return iResult.ToArray();        }        static T[] Map<T>(Func<T, T> func, IEnumerable<T> iSource)        {            List<T> iResult = new List<T>();            foreach (var item in iSource)            {                iResult.Add(func(item));            }            return iResult.ToArray();        }        static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> iSource)        {            T sum = default(T);            foreach (var item in iSource)            {                sum = func(sum, item);            }            return sum;        }        static void PrintArray(int[] iSource)        {            Console.Write("\t");            iSource.ToList().ForEach(x => Console.Write("{0} ", x));            Console.WriteLine();        }        static void Indent(string msg)        {            Console.WriteLine("{0}:", msg);        }        static void Main(string[] args)        {            int[] iSource = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };            Indent("Source");            PrintArray(iSource);                        int[] iFilterResult = Filter(x => x > 5, iSource);            Indent("Filter, item more than 5");            PrintArray(iFilterResult);            int[] iMapResult = Map(x => x * 2, iSource);            Indent("Map, multiple with 2");            PrintArray(iMapResult);            int iReduceResult = Reduce((x, y) => x + y, iSource);            Indent("Reduce");            Console.Write("\t{0}\n ", iReduceResult);            Console.ReadKey();        }    }}

Chatting with a friend a few days ago. NET has added many functional programming elements and should be able to write functional programming-style code. It should also be a language that supports functional programming, today, I am writing the code.

PS.

Today, when I read "CLR via C # 3e", I saw an example of how Jefferey improved code readability by using the Extension Method. I reconstructed the above Code to improve readability:

static class ExtendMethod{    public static void ShowItems<T>(this IEnumerable<T> collection)    {        Console.Write("\t");        collection.ToList().ForEach((x) => Console.Write("{0} ", x));        Console.WriteLine();    }}class Program{    static T[] Filter<T>(Func<T, bool> func, IEnumerable<T> iSource)    {        List<T> iResult = new List<T>();        foreach (var item in iSource)        {            if (func(item))                iResult.Add(item);        }        return iResult.ToArray();    }    static T[] Map<T>(Func<T, T> func, IEnumerable<T> iSource)    {        List<T> iResult = new List<T>();        foreach (var item in iSource)        {            iResult.Add(func(item));        }        return iResult.ToArray();    }    static T Reduce<T>(Func<T, T, T> func, IEnumerable<T> iSource)    {        T sum = default(T);        foreach (var item in iSource)        {            sum = func(sum, item);        }        return sum;    }    static void PrintArray(int[] iSource)    {        Console.Write("\t");        iSource.ToList().ForEach(x => Console.Write("{0} ", x));        Console.WriteLine();    }    static void Indent(string msg)    {        Console.WriteLine("{0}:", msg);    }    static void Main(string[] args)    {        int[] source = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };        Indent("Source");        source.ShowItems();                int[] filterResult = Filter(x => x > 5, source);        Indent("Filter, item more than 5");        filterResult.ShowItems();        int[] mapResult = Map(x => x * 2, source);        Indent("Map, multiple with 2");        mapResult.ShowItems();        int reduceResult = Reduce((x, y) => x + y, source);        Indent("Reduce");        new[] { reduceResult }.ShowItems();        Console.ReadKey();    }}
Related Article

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.