Sequence of functional programming in C #

Source: Internet
Author: User

After a long time, finally take the leisure time to continue to the functional programming of the album, this time begins to prepare for the new direction of IOS , the OC Tutorial written in the SWIFT version, of course, I personally support Xamarin, but I will generally first master the original ecological development, and then master Xamarin. The rest of the time developed a Xamarin App project that took more than 10 days to complete. today's content will be relatively simple, that is, how to use functional programming to implement the list deduction. The simple point is that the list of data is not as we normally develop to write well, but through a certain algorithm calculated (not the program is completed in the calculation of the open, but in the use of the time only calculated, and only calculate to the one we use so far).

Implementing iterators with Function methods

First of all, of course we first write a high-order function to implement the main functional part of the list deduction, the specific code is as follows:

1          Public StaticIenumerable<t> sequence<t> (func<t, t> getNext, T startval, Func<t,BOOL>endreached)2         {3             if(GetNext = =NULL)4                 yield  Break;5             yield returnStartval;6T val =Startval;7              while(endreached = =NULL|| !endreached (val))8             {9val =GetNext (val);Ten                 yield returnVal; One             } A}

c#  2.0 startval getnext endreached endreached null

        Static void Main (string[] args)        {            var oddNumbersForm1 = sequence<int2 1  - );             foreach (int in oddNumbersForm1)            {                Console.WriteLine (x);            }            Console.readkey ();        }

Finally, we can see the following display in the command line interface:

Range

Most of the time we use the sequence is very simple, and the use of the above sqquence function will become complex, and this function can not implement some of the functions we need, such as the need to determine the existence of a value in the scope and so on, so we will use a simple Range to implement these functions, of course, the internal use of the Sequence function as above , the following is the specific code:

1          Public classRange<t>: ienumerable<t>2         {3             PrivateT start;4             PrivateT end;5             PrivateComparison<t>Compare;6             PrivateIenumerable<t>sequence;7 8             Private Static intCompare<u>(U one,u Other)9             {Ten                 returnComparer<u>. Default.compare (one,other); One             } A  -              PublicRange (t start, t end, Func<t, t> GetNext, comparison<t>Compare) -             { the                  This. Start =start; -                  This. end =end; -                  This. Compare =Compare; -                  This. sequence = Sequence<t> (GetNext, start, V = compare (GetNext (v), end) >0); +             } -  +              PublicRange (t start, t end, Func<t, t>getNext) A: This(Start, End, GetNext, Compare) {} at  -              PublicIenumerator<t>GetEnumerator () -             { -                 returnsequence. GetEnumerator (); -             } -  in System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator () -             { to                 return((ienumerable<t>) This). GetEnumerator (); +             } -  the              Public BOOLContains (T value) *             { $                 returnCompare (value, start) >=0&& Compare (end, value) >=0;Panax Notoginseng             } -}

With the code above we can see that the Range is Much more powerful than the Sequence function, and we can see that we also provide Contains method is used to determine whether the specified value is in the range, and we can also obtain IEnumerable excuses for the various extension methods for our use.

Limit

Sometimes the implementation of our iterators can be complex, and if we add a restriction that not only affects its performance, but also causes the code to be less readable, we can separate the two, and the function that generates the sequence requires no restrictions, and the restriction is transferred externally, such as the function Tfunc is a function that returns a sequence, and there is no limit, and Cfunc accepts a maximum value, and the function that computes the sequence, and returns the sequence of the responsible condition, we can loop to get the value as follows:

1 foreach (int in Cfunc (tfunc)) {2    Console.WriteLine (x); 3 }

This is the end of the sequence of functional programming, the later time the author will seize the time as soon as possible to end this album, began to focus on the xamarin.android series of tutorials.

Sequence of functional programming in C #

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.