{"id":18315373,"url":"https://github.com/lefticus/lambda_coroutines","last_synced_at":"2026-01-22T17:08:56.425Z","repository":{"id":137251311,"uuid":"290948063","full_name":"lefticus/lambda_coroutines","owner":"lefticus","description":"A lightweight macro-based coroutine / resumable / cooperative multitasking function utility designed for C++14 lambdas.","archived":false,"fork":false,"pushed_at":"2020-08-30T20:09:51.000Z","size":45,"stargazers_count":37,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-09T13:11:44.912Z","etag":null,"topics":["coroutines","lambdas"],"latest_commit_sha":null,"homepage":"","language":"CMake","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lefticus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"lefticus","patreon":"lefticus"}},"created_at":"2020-08-28T04:10:34.000Z","updated_at":"2024-05-18T06:47:51.000Z","dependencies_parsed_at":null,"dependency_job_id":"a04b062f-5c1a-4fce-812f-ca5e2706d648","html_url":"https://github.com/lefticus/lambda_coroutines","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"cpp-best-practices/gui_starter_template","purl":"pkg:github/lefticus/lambda_coroutines","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefticus%2Flambda_coroutines","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefticus%2Flambda_coroutines/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefticus%2Flambda_coroutines/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefticus%2Flambda_coroutines/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lefticus","download_url":"https://codeload.github.com/lefticus/lambda_coroutines/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lefticus%2Flambda_coroutines/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28667387,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T17:07:18.858Z","status":"ssl_error","status_checked_at":"2026-01-22T17:05:02.040Z","response_time":144,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coroutines","lambdas"],"created_at":"2024-11-05T16:39:28.024Z","updated_at":"2026-01-22T17:08:56.392Z","avatar_url":"https://github.com/lefticus.png","language":"CMake","funding_links":["https://github.com/sponsors/lefticus","https://patreon.com/lefticus"],"categories":[],"sub_categories":[],"readme":"# Lambda Coroutines\n\n[![codecov](https://codecov.io/gh/lefticus/lambda_coroutines/branch/master/graph/badge.svg)](https://codecov.io/gh/lefticus/lambda_coroutines)\n![CMake](https://github.com/lefticus/lambda_coroutines/workflows/CMake/badge.svg)\n\n## Description\n\nA lightweight macro-based coroutine / resumable / cooperative multitasking function utility designed for C++14 lambdas.\n\n# Examples\n\n## Rotating Infinite Sequence\n\n```cpp\n// lambda must be mutable\nauto next_direction = [state=0]() mutable {\n  // set up coroutine, needs a captured integral `state` value\n  lambda_co_begin(state);\n\n  // for eternity yeild the next possible value\n  while (true) {\n    lambda_co_yield(directions::Left);\n    lambda_co_yield(directions::Right);\n    lambda_co_yield(directions::Up);\n    lambda_co_yield(directions::Down);\n  }\n\n  lambda_co_end();\n};\n\nint main() {\n  auto val1 = next_direction();  // returns Left\n  auto val2 = next_direction();  // returns Right\n  // etc\n}\n```\n\n## Obligatory Fibonacci Sequence\n\n```cpp\n// generates the set of all fibonacci numbers representable by a ull, returns\n// empty optional at end of list\nauto fib = [state = 0, fib_2 = 0ULL, fib_1 = 1ULL]() mutable -\u003e std::optional\u003cunsigned long long\u003e {\n  lambda_co_begin(state);\n\n  lambda_co_yield(0);\n  lambda_co_yield(1);\n\n  while (fib_1 \u003c std::numeric_limits\u003cdecltype(fib_1)\u003e::max() / 2) {\n    fib_2 = std::exchange(fib_1, fib_2 + fib_1);\n    lambda_co_yield(fib_1);\n  }\n\n  lambda_co_return({});\n};\n```\n\n## Ranged `for` Statement With Coroutine\n\n```cpp\n// Using the Obligatory Fibonacci Sequence\nfmt::print(\"All possible Fib numbers representable by 'unsigned long long'\");\nfor (const auto value : lambda_coroutines::while_has_value(fib)) {\n  fmt::print(\"{}\\n\", value);\n}\n```\n\n## CPU Instruction Decoding State Machine\n\n[Compiler Explorer playground](https://godbolt.org/z/7dr8j7) for this example.\n\n```cpp\nenum OpCodes : std::uint8_t {\n  ADD = 0,\n  STA = 1,\n  NOP = 2\n};\nstruct Machine {\n  std::uint8_t PC{0};\n  std::uint8_t A{0};\n  std::array\u003cuint8_t, 256\u003e RAM{STA, 10, ADD, 15};\n};\n\nMachine machine;\n\nauto CPU = [state = 0, \u0026machine, op = OpCodes::NOP]() mutable {\n  lambda_co_begin(state);\n\n  while(true) {\n    op = static_cast\u003cOpCodes\u003e(machine.RAM[machine.PC]);\n    ++machine.PC;\n    if (op == OpCodes::STA) {\n      lambda_co_yield();\n      machine.A = machine.RAM[machine.PC++];\n      lambda_co_yield();\n    } else if (op == OpCodes::ADD) {\n      lambda_co_yield();\n      machine.A += machine.RAM[machine.PC++];\n      lambda_co_yield();\n    } else if (op == OpCodes::NOP) {\n      lambda_co_yield();\n    };\n  }\n\n  lambda_co_end();\n};\n```\n\n\n\n# Limitations\n\n * Cannot use `switch` statements in coroutine code\n * Cannot declare variables in coroutine code\n * Anything you care about must be part of the lambda capture declaration\n \n \n# Using In Your Project\n\nIt is one VERY simple header file https://github.com/lefticus/lambda_coroutines/blob/main/include/lambda_coroutines/lambda_coroutines.hpp\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flefticus%2Flambda_coroutines","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flefticus%2Flambda_coroutines","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flefticus%2Flambda_coroutines/lists"}