Model in the .NET 2.0 Basic Base - Functional Programming

zhaozj2021-02-16  84

Functional Programming

Functional Programming is not a fresh concept, such as C , although not a Functional Programming language, but it also has a variable support - Provide STL, Boost, etc. Smart, high performance algorithms and functions. These features that seem often to be C can be insulated for object-oriented programming languages ​​and frameworks such as Java and C #. Now, in the support of the CLR model and C # 2.0 anonymous entrustment, we can also construct a surprising Functional Programming program, and more simple than C (of course, the performance cannot be compared, because the model in the CLR is a run Time technology, and the template in C is compiled technology). Current .NET BCL's support for Functional Programming is limited to the set class, which is exactly List

And array.

Let's take a simple example. Suppose there is a contact list List

The contact's definition is as follows:

Class Contact {

Public String Name;

...

}

Now we have to copy the name of all the contacts in this list to another list. You may immediately write it immediately:

List

C1 = ...;

List

C2 = new list

();

Foreach (Contact C IN C1) {

C2.Add (c.name);

}

This is a very rule of C # code. In .NET 2.0, there is a paradition and anonymous commission, we can write the implementation of the same function as follows:

List

C1 = ...;

List

C2 = c1.convertall

(

Delegate (Contact C) {return c.name;});

Obviously this code is more concisely than manually written Foreach code, and it is more clear and directly in the expression intent. Where the CONVERTALL method is a model method that converts the list element to a list of specified types. The prototype is:

List convertall (converter

CONVERTER);

Converter

It is a model delegate that specifies how to convert (similar to the function object in C ), the prototype is (T is the original type, U is the target type):

Delegate U Converter

(T from);

Here is just a simple example, for more complex situations, models and anonymous entrustments allow you to implement more imagination (for example, anonymous delegate allows you to reference the variables on the stack).

Below is the model delegate (located in the system namespace) in the BCL:

Prototype Description Delegate Bool Predicate

(T Obj);

When accessing a collection, an assertion of the specified element (TRUE or FALSE) DELEGATE VOID ACTION (T OBJ);

When accessing the collection, make a specific action delegate int COMPARISON

(T x, t y);

Compare two elements DELEGATE U Converter

(T from);

Convert an element to another for copying elements between two collections

List

Methods of supporting Functional Programming:

Prototype Description INT Findex (Predicate

Match);

INT Findex (int index, predicate

Match);

INT Findex (int index, int count, predicate

Match);

Find the first index of the elements that meet the assertion conditions Int FindlastIndex (Predicate

Match);

INT FindlastIndex (int index, predicate

Match);

INT FindlastIndex (int index, int count, predicate

Match);

Find the last index of the elements that meet the assessment conditions List

Findall (Predicate

Match);

Find all elements that meet the assertion conditions Nullable

Find (Predicate

Match);

Find the first element NULLABLE satisfying the assertion conditions

Findlast (Predicate Match);

Find the last element BOOL EXISTS (Predicate ) that meets the assertion conditions

Match);

It is judged whether or not the elements that meet the assertion conditions have Bool TrueForll (Predicate

Match);

Judging whether all elements meet the assertion conditions Int Removeall (Predicate

Match);

Delete all elements that meet the assertion conditions, return to the number of elements of the delete void foreach (action

Action);

Similar to Foreach Statement Void Sort (Comparison

Comparison);

Sort List Convertall (Converter

CONVERTER);

Conversion set elements

The Array class provides a similar way to support Functional Programming, which is different from that are all types of methods and non-instance methods, which are limited to the space. Let's take a look at what the example is changed to an array:

Contact [] Contacts = ...;

String [] names = array.convertall

(Contacts,

Delegate (Contact C) {return c.name;});

转载请注明原文地址:https://www.9cbs.com/read-14790.html

New Post(0)