diff options
author | Determinant <ted.sybil@gmail.com> | 2018-06-28 16:47:49 -0400 |
---|---|---|
committer | Determinant <ted.sybil@gmail.com> | 2018-06-28 16:47:49 -0400 |
commit | 924be7bb303bacae3eedd7610383e638d5b096d8 (patch) | |
tree | 541ab8b69157bb3c15d9072ecf5728e02fcfc477 /README.rst | |
parent | c63ace092b179c029865cd861f50b502fa0e2e26 (diff) |
add an example
Diffstat (limited to 'README.rst')
-rw-r--r-- | README.rst | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -14,6 +14,35 @@ the underlying `any` type, an exception will be thrown when the resolved value types do not match the types expected in the subsequent computation. See `test.cpp` for detailed examples. +Example +======= + +Calculate the factorial of n, the hard way: + +.. code-block:: cpp + + #include "promise.hpp" + + using promise::promise_t; + + int main() { + promise_t root; + promise_t t = root; + for (int i = 0; i < 10; i++) + t = t.then([](std::pair<int, int> p) { + p.first *= p.second; + p.second++; + return p; + }); + t.then([](std::pair<int, int> p) { + printf("fac(%d) = %d\n", p.second, p.first); + }); + /* no actual calculation until here */ + root.resolve(std::make_pair(1, 1)); + return 0; + } + + API === |