Model--functional programming in the. NET 2.0 base Class Library

Source: Internet
Author: User
Tags foreach anonymous bool comparison copy sort
Functional Programming


functional programming is not a new concept, for example, C + + is not a functional programming language, but it also has the flexibility to support it-by using templates, function objects (functions Objects) and operator overloading, such as the STL, Boost and other libraries provide a clever, high-performance algorithms and functions. These features, which have long seemed to be achievable by C + +, are insulated for object-oriented programming languages and frameworks that emphasize type safety, such as Java and C #. Now, with the support of CLR generics and C # 2.0 anonymous delegates, we can also construct amazing functional programming programs, and simpler than C + + (of course, performance is not comparable because the generics in the CLR are a run-time technology, while templates in C + + are is the compile-time technology). The current. NET BCL support for functional programming is limited to collection classes, specifically list<t> and Array.





let's look at a simple example. Suppose you have a contact list list<contact> The contact is defined as follows:











class Contact {





public string Name;





...





}











now we're going to copy the names of all the contacts in this list to a different list. You might have written it right away:











list<contact> C1 = ...;





list<string> c2 = new list<string> ();











foreach (Contact C-C1) {





C2. ADD (C.name);





}











This is a very well-behaved C # code. In. NET 2.0, with generic and anonymous delegates, we can write the following implementations that accomplish the same function:











list<contact> C1 = ...;





list<string> c2 = c1. Convertall<string> (





delegate (contact C) {return c.name;});











Obviously this code is simpler than the hand-written foreach code, and it becomes clearer and more straightforward in terms of expressing intent. Where the ConvertAll method is a generic method that converts a list element to a list of the specified types. The prototype is:











list<u> convertall<u> (converter<t, u> Converter);











Converter<t, u> is a generic delegate that specifies how to convert (similar to a function object in C + +), the prototype is (T is the original type, U is the target type):











delegate U converter<t, u> (T from);











here is just a simple example, for more complex situations, generics and anonymous delegates allow you to implement them in more imaginative ways (for example, anonymous delegates allow you to refer to variables on the stack).





below is the generic delegate for functional programming in BCL (in the System namespace):











prototype


Description





delegate bool Predicate<t> (T obj);


the assertion (true or false) of the specified element when accessing the collection





delegate void action<t> (T obj);


a specific action is made to the specified element when accessing the collection





delegate int comparison<t> (t x, t y);


compares two elements





delegate U converter<t, u> (T from);


converts one element to another, which is used to copy elements between two sets














List<t> provides the following methods to support functional programming:











prototype


Description





int FindIndex (predicate<t> match);





int findindex (int index, predicate<t> match);





int findindex (int index, int count, predicate<t> match);


find the index of the first element that satisfies the assertion condition





int FindLastIndex (predicate<t> match);





int findlastindex (int index, predicate<t> match);





int findlastindex (int index, int count, predicate<t> match);


find the index of the last element that satisfies the assertion condition





list<t> FindAll (predicate<t> match);


find all elements that satisfy the assertion condition





nullable<t> Find (predicate<t> match);


find the first element that satisfies the assertion condition





nullable<t> FindLast (predicate<t> match);


find the last element that satisfies the assertion condition





bool Exists (predicate<t> match);


determines whether an element that satisfies an assertion condition exists





bool Trueforall (predicate<t> match);


determines whether all elements satisfy the assertion condition





int RemoveAll (predicate<t> match);


deletes all elements that satisfy the assertion condition, returning the number of elements deleted





void ForEach (action<t> Action);


similar to a foreach statement





void Sort (comparison<t> Comparison);


Sort





list<u> ConvertAll (converter<t, u> Converter);


Conversion Collection Element













The
Array class provides similar methods for supporting functional programming, except that they are both class methods rather than instance methods, which are limited to space and are not enumerated at all. Now let's take a look at what the previous example might look like in an array:











contact[] contacts = ...;





string[] names = Array.convertall<contact, string> (Contacts,





delegate (contact C) {return c.name;});











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.