Skip to content

Instantly share code, notes, and snippets.

@Alexhuszagh
Last active June 24, 2016 20:38
Show Gist options
  • Save Alexhuszagh/b14dbc7564111cfeac5c4a07d383837b to your computer and use it in GitHub Desktop.
Save Alexhuszagh/b14dbc7564111cfeac5c4a07d383837b to your computer and use it in GitHub Desktop.
Generator Function Example
/**
* @brief Sample coroutine function which uses recursion for it's
* evaluation.
*
* @copyright 2016, Alex Huszagh
* @license MIT
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <co2/generator.hpp>
#include <cstdlib>
#include <stdio.h>
#include <vector>
/**
* @brief Random number generator, feeds into another pipeline.
*/
auto random_generator(int count) CO2_BEG(co2::generator<int>, (count))
{
while (count){
CO2_YIELD(rand() % 100);
count -= 1;
};
} CO2_END
int main(int argc, char* argv[])
{
for (int random: random_generator(1000)) {
printf("Random number, %d\n", random);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment