Code Monkey home page Code Monkey logo

pseudo_generator's Introduction

Generators (continuations) for C++

Some languages, partcaularly, Python, support a very useful feature called "generators".
It is a special case of the thing, known as "continuations".

Here is a sample Python code for those, who by some reason do now know about them:

[pyton]

def primes():
    yield 2
    x = 3
    while True:
        for d in range( 
    

[/python]

Is there any way to implement this in C++?
Well, not really. 
Support for continuations require that execution state can be saved and restored arbitratily.
C++ model of execution does not allows this.

BOOST offers generators library, based on a trick with threads: generator code is effectively runned in the separate thread,
which eliminates the nececerity for saving execution state.

This offers excellent "real" genrators, just like in Python, but some may think about such approach as about overkill.

What I want is a simple, standart-compliant, low-overhead library, that can facilitate writing code in generator style.

ANd, here it is.

That's how you can implement the same genreator with this library:

[C++]

struct my_generator: public pseudo_generator<int>
{
    int,;
    int operator()();
};

int my_generator::operator()()
{
    BEGIN_GENERATOR2( s1, s2 );
    for 	
    END_GENERATOR;
    return -1; //dummy return;
};

[/C++]

SOmewhat more dirty, than Python, but significantly more clear than onriginal C++.


{ How it works }

pseudo_generator's People

Contributors

dmishin avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.