C ++ 0x: Simulate Ruby process objects using lambda expressions

Source: Internet
Author: User
ArticleDirectory
    • About C ++ 0x
    • Lambda expressions
    • Ruby code
    • C ++ code
With regard to C ++ 0x, the new standard C ++ 0x is expected to be launched by the end of next year, although it is nearing the closing stage, there are still a number of new proposals to be accepted. In particular, the proposal "unified function syntax (unified function syntax)" related to lambda expressions and local functions is worth noting )", despite not being visible to the Standards Committee (four delayed votes in two years and two rejected), the 7th n2989 version was launched "Stubbornly, the perseverance of the author is admirable. With regard to the impact of lambda expressions on functional programming styles, some static languages with the idea of OOP have successively launched lambda, a short language facility characterized by anonymous functions, typical examples include Microsoft's vb9 and C #3.0. Correspondingly, many dynamic scripting languages (Python, Ruby, and JavaScript) have long had the corresponding language features. Ruby's process object is one of them. This time, taking advantage of the new C ++ 0x standard, this exciting new feature has also been introduced after the C ++ language is unwilling, it laid a solid foundation for the further promotion of functional programming styles in the C ++ language. The following uses the lambda expression in c ++ 0x to simulate Ruby process objects. Ruby Code <textarea cols="87" rows="15" name="code" class="ruby">Class array <br/> def inject (n) <br/> Each {| value | n = yield (n, value )} <br/> n <br/> end <br/> def sum <br/> inject (0) {| n, value | n + value} <br/> end <br/> def product <br/> inject (1) {| n, value | N * value} <br/> end <br/> def find <br/> for I in 0... size <br/> value = self [I] <br/> return value if yield (value) <br/> end <br/> return nil <br/> end <br/> [1, 2, 3, 4, 5]. sum #15 <br/> [1, 2, 3, 4, 5]. product #120 <br/> [1, 2, 3, 4, 5]. find {| v | V * V> 10 }# 4 <br/></textarea> For instructions on Ruby process objects and the ruby code, refer to the extensive callback in Ruby and Its Simulation Implementation in C ++. C ++ code # Include <array> <br/> # include <functional> <br/> // # include <numeric> <br/> # include <algorithm> <br/> # include <boost/assign. HPP> <br/> # include <iostream> </P> <p> using namespace STD; </P> <p> template <typename T> <br/> struct array: public vector <t> <br/>{< br/> template <typename _ ITER> <br/> array (_ ITER _ first, _ ITER _ last ): vector (_ first, _ last) {}</P> <p> T inject (t n, function <T (t, t)> F) const {<br/> F Or_each (begin (), end (), [&] (T value) {n = f (n, value) ;}); <br/> return N; <br/> // return accumulate (begin (), end (), N, f); <br/>}< br/> T sum () const {<br/> return inject (0, [] (t n, T value) {return N + value ;}); <br/>}< br/> T product () const {<br/> return inject (1, [] (t n, T value) {return N * value;}); <br/>}< br/> const T * Find (function <bool (t)> F) const {<br/> auto I = find_if (begin (), end (), f); <br /> Return I = end ()? Nullptr: & * I; <br/>}< br/>}; </P> <p> int main () <br/>{< br/> array <int> A = boost: Assign: list_of (1) (2) (3) (4) (5 ); <br/> // array <int> A = {1, 2, 3, 4, 5}; <br/> cout <. sum () <Endl; <br/> cout <. product () <Endl; <br/> cout <*. find ([] (INT v) {return v * V> 10 ;}) <Endl; </P> <p> return 0; <br/>}</P> <p> // 15 <br/> // 120 <br/> // 4C ++ Code Description
    1. The ruby array is simulated using the vector component of the standard library.
    2. The assign component of the boost library is used to simulate Ruby array initialization.
      Note: If the initialization list constructor is added to the array class (new feature C ++ 0x, which is not supported by VC10), the initialization method can be significantly improved (36th rows ).
    3. Ruby process objects are simulated using lambda expressions.
    4. The each method in Ruby Code uses for_each of the standard libraryAlgorithmTo simulate.
    5. If you do not simulate a specific algorithm, the inject method in ruby code can also be implemented using the accumulate algorithm of the standard library.
      The specific method is: Comment out rows 17 and 18, and comment out rows 3 and 19.
    6. The find method in ruby code is directly implemented using the find_if algorithm of the standard library (the specific algorithm is not simulated ).
    7. Auto in row 28th is a New Keyword introduced by C ++ 0x for automatic type derivation, similar to VaR in C #3.0.
      Note: strictly speaking, the original intention of the auto keyword (indicating the type of local variables automatically allocated, which can be omitted and rarely used) has been abolished in the new standard.
    8. The nullptr of row 29th is also a New Keyword introduced by C ++ 0x. It is used to replace null to indicate a null pointer.
    9. The template function of the original boost library will officially enter the standard library in the new standard.
      Function variables not only store function pointers and function objects, but also store lambda expressions.
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.