An explanation of lazy evaluation in C # functional programming

Source: Internet
Author: User


Lazy evaluation

Before we begin to introduce the knowledge we are going to talk about today, we want to understand the difference between a rigorous evaluation strategy and a non-strict evaluation strategy so that we can understand why we need to use this technology. The first thing to note is that the C # language is a small subset of non-strict evaluation strategies, most of them are strict evaluation strategy. First we demonstrate the case of the non-strict evaluation strategy, we first write a doonething method in the console project.

Then write the following string of code in the Main method:

Then we run the program and we find that the Doonething method is not executed. This, of course, also looks normal, because this is or, and the first one is already true. The whole expression is true, and the second one has no demand value. But this is precisely the non-rigorous evaluation of the strategy, if the strict evaluation strategy, the entire expression will be calculated. Then is the strict evaluation strategy of the situation, this is believed many people will immediately understand, first we need to write a dosomething method:

Then modify the Main method:

After execution we can see the following results:

However, we can clearly see that the value of a is false, the B value is not used at all, but when the parameter is passed the Doonething method is executed and assigned to B, assuming this method is a very time-consuming operation. Then we will waste this time, the final value is not used. And this is the strict evaluation strategy, and today's main goal is to change this situation, can be calculated when we determine the need for a certain value. Now we can start to transform this method so that it can support lazy evaluation. First we modify the DoSomething method:

Here we change the parameter types to functions, so that the parameters that will be passed in are changed to functions. The evaluation is performed only when we need it, otherwise it will not run, and we need to modify it as follows in the corresponding Main method:

Here we don't need to get rid of the return type of the Doonething method, if that's the case. Using functional programming on existing projects can be cumbersome. Here we just need to use the anonymous function can be done, the following we can see the final performance:

The Doonething method is not executed because the variable is not determined at all in dosomething, so we can save time on this part of the calculation, but in fact we are not finished, we may need to use this value multiple times in actual development, For example, we modify the DoSomething method as follows:

And when the DoSomething method is called in the Main method, the first parameter is changed to True, and then we can see the following output:

The Doonething method was executed two times, and of course we can save with local variables, which you might write:

If this is the case, then our lazy evaluation does not make any sense, because the method is executed as soon as we enter this method, and it is not any different to assign the result of the operation directly to B when the parameter is passed. Of course there are other tricks that can be avoided, but these techniques are not the following, and we can encapsulate them, for example, we can write a lazys<t> class:

We can see that in the Construction method section we take the corresponding function as an argument and save it to the function, and only call value will execute it and save the value, and at the next call, if it has been evaluated, it will return the cached value directly, so as to avoid repeated execution. We also want to modify the DoSomething method and the Main method:

After the final execution we can see that only one doonething method is executed:

Some readers may ask why the class name does not lazy but add a s, because. NET has already included the Lazy<t> class for us, believing that many people have never used it. Just know the existence of Func and action, and below we modify our code directly to take advantage of the:

The final result is a touch of the same, of course, the system comes with more lazy functions, and support multithreading.

So far here, in Friday, we have to press the resistance, wrote too much may not mind down, so will all one of these split up to speak.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
An explanation of lazy evaluation in C # functional programming

This address: http://www.paobuke.com/develop/c-develop/pbk23154.html






Related Content C # example of file monitoring using FileSystemWatcher control C # datetime.tostring generate corresponding time format for different languages C # data Structure Order table (Seqlist) instance in C # To implement a method to convert pixels to page units
C # Methods for manipulating Windows Registry C # Determine whether access source is a search engine link method C # functional programming in the standard high-order functions in the example of using UDP communication in C #

An explanation of lazy evaluation in C # functional programming

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.