C # sequence of functional programming

Source: Internet
Author: User

C # sequence of functional programming
After a long time, I finally spent my spare time continuing to upload the function programming album. This time begins with preparing for the new IOS direction, I have written the OC tutorials into the SWIFT version. Of course, I personally support Xamarin, but I usually master the original ecological development first and then Xamarin. Later I developed an Xamarin App project, which took over ten days. Today's content is relatively simple, that is, how to use Functional Programming to Implement list derivation. To put it simply, the data in the list is not written as we do in normal times, but calculated through certain algorithms (not as soon as the program is opened, it is calculated only when it is used, and only until the one we use ). To implement the iterator using the function method, first of all, we first compile a high-level function to implement the main function part of list derivation. The specific code is as follows: 1 public static IEnumerable <T> Sequence <T> (Func <T, T> getNext, T startVal, Func <T, bool> endReached) 2 {3 if (getNext = null) 4 yield break; 5 yield return startVal; 6 T val = startVal; 7 while (endReached = null |! EndReached (val) 8 {9 val = getNext (val); 10 yield return val; 11} 12} the function is used to iterate based on certain algorithms, the C #2.0 syntax is used here. This function starts iteration Based on startVal and calculates the next Value Based on getNext, while endReached is used to constrain the end condition. So we can see that if endReached is NULL, it is also feasible, so this sequence is infinite, of course not an endless loop. The following code hides how to use this function: static void Main (string [] args) {var oddNumbersForm1 = Sequence <int> (x => x + 2, 1, x => x> = 20); foreach (int x in oddNumbersForm1) {Console. writeLine (x);} Console. readKey ();} finally, we can see the following display on the command line interface: in many cases, the sequence we use is very simple, and using the above Sqquence function will become complicated, at the same time, this function cannot implement some functions we need, such as the existence of a Range when a value needs to be determined, so we will use a simple Range to implement these functions, of course, the above Sequence function is used internally. The specific code is as follows: 1 public class Range <T>: I Enumerable <T> 2 {3 private T start; 4 private T end; 5 private Comparison <T> compare; 6 private IEnumerable <T> sequence; 7 8 private static int Compare <U> (U one, U other) 9 {10 return Comparer <U>. default. compare (one, other); 11} 12 13 public Range (T start, T end, Func <T, T> getNext, Comparison <T> compare) 14 {15 this. start = start; 16 this. end = end; 17 this. compare = compare; 18 this. sequence = Sequenc E <T> (getNext, start, v => compare (getNext (v), end)> 0); 19} 20 21 public Range (T start, T end, func <T, T> getNext) 22: this (start, end, getNext, Compare) {}23 24 public IEnumerator <T> GetEnumerator () 25 {26 return sequence. getEnumerator (); 27} 28 29 System. collections. IEnumerator System. collections. IEnumerable. getEnumerator () 30 {31 return (IEnumerable <T>) this ). getEnumerator (); 32} 33 34 public bool Contains (T value) 35 {36 return compare (value, start)> = 0 & compare (end, value)> = 0; 37} 38} with the above code, we can see that the Range function is much more powerful than the Sequence function, in addition, we can see that the Contains method is also provided to determine whether the specified value is in the value range. In addition, we can also obtain various extension methods that can be used by IEnumerable. Restrictions sometimes our implementation of the iterator may be very complicated. If we add restrictions, it will not only affect the performance of the iterator, but also lead to poor code readability, so we can separate the two, the function responsible for generating sequences only requires no restrictions, but transfers the restriction to the outside. For example, the function TFunc is a function that returns the sequence without restrictions, while the CFunc accepts a maximum value, and the function used to calculate the sequence, and return the sequence responsible for the condition, we can obtain the value in a loop as follows: 1 foreach (int x in CFunc (10, TFunc )) {2 Console. writeLine (x); 3} Now the sequence in functional programming is over. In the future, I will take the time to end this album as soon as possible and start focusing on Xamarin. android tutorials.

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.