C ++ => C # => F #, more functional, more parallel (1)

Source: Internet
Author: User

On DOTNET board of ustc bbs, Orochi recommended a book named "functional programming for the real world ". it demonstrates advantages of functional programming towards traditional imperative programming, which has res me a lot. here, I will first narrate my understanding of such advantages as a C # developer, then take an example to address how the programming pattern changes from pure imperative language as C ++, to C # which contains a lot of functional programming elements, then to typical functional language F #.

As I mentioned in this blog before, functional programming is another programming paradigm besides imperative programming. it is based on lambda calculus, which is as descriptive as a Turing machine. anyway, this statement demonstrates nothing valuable. let see the differences between imperative programming and functional programming in detail:

1. function is treated as the fundamental element in functional programming. in functional programming versions, functions can be treated as independent variables (instead of considered as a pointer in different ages like C ++), can be created and destroyed dynamically like other kinds of variables, and can have no name (I. e. anonymous ). this is a difference, but I admit that I haven't found any advantages caused by this point directly. l maybe when finish the studying of F #, I can answer this question.

2. functional Programs tell computer what to do rather than how to do it. for example, if we want to select all the costumers living in UK, we may write like this in imperative versions like C ++:

  1   For (Vector  <  Ccustomer  >  : Const_iterator I  =  Customers. Begin (); I  ! =  Customers. End ();  ++  I)
2 {
3 If (I -> Livingin = " UK " )
4 {
5 Ukcustomers. push_back ( * I );
6 }
7 }

This code snippet tells the computer: first pick up a customer from the customer list, check whether he/she lives in UK, if so, copy it to a new list called ukcustomers, do noting otherwise, then pick up next and do the same thing, until all the customers are processed. we are quite familiar with such "algorithms", huh? Let's see how we tell the computer to do the same thing with C #:

  1   VaR ukcustomer  =  
2 From customer In MERs
3 Where Customer. livingin = " UK "
4 Select customer;

What we need to do is just tell the computer: to pick up all the MERs living in UK. tell the computer what to do rather than how to do, this is called declarative style, which is used by popular programming ages such as SQL, as well as functional programming languages.

3. variables in functional versions ages are immutable, I. e. they cannot be changed after created and initialized. A little advantage caused by this feature is, the codes is easier to understand and with less ambiguous. for example, without reading the manual, we don't know what is the result of rectangle. inflate (INT width, int height ). will it inflate the original rectangle or return a new inflated re Ctangle? Both the choices are reasonable. however, in the functional world, only the second situation is possible. of course, this style is not intended to save the "mass World" of imperative versions, but to make it possible that:

4. write parallel code very easily . as every object in functional language is immutable, there will be no blocking, no lock or no writing collisions in the multi-thread world. furthermore, it becomes very cheap to recognize which part of the program can be executed currently, so that the automatic optimization by the compiler is possible. tha T means, we need only make some tiny changes, then the program can run on multi-core computer or clusters with compiler's optimizations. Take the Sample Code mentioned in the o_ops! T-shirt:

  1 Girls. Where (x=>X. isbeauty). All (x=>{X. Hi ();Return True;});

Which means say hi to all the beautiful girls in a list named girls. if you are a mono-core computer, you have to select all the beautiful girls, then say hi to them one by one. it is very uncompetitive, isn' t it? So we can modify the program like this, with the help of parallel extensions of. Net, which you can download here:

  1 Girls. asparallel (). Where (x=>X. isbeauty). All (x=>{X. Hi ();Return True;});

Now you can say hi to all the beautiful girls simultaneously, with enough cores. Is it simple? No explicit processes or threads, no configuration for MPI or OpenMP. Thanks to the immutability of LINQ in C #3.0, parallel programming turns so simple.
Besides easy parallel programming, immutability also makes lazy computing possible, which is implemented in C # LINQ and F #.

(to be continued... Next part you will see how we accomplish part of a map rendering engine in C ++, C # And F #, demonstrating how the functional programming paradigm makes parallel coding so easy and natural .)

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.