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:
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:
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
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:
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.