From 924be7bb303bacae3eedd7610383e638d5b096d8 Mon Sep 17 00:00:00 2001 From: Determinant Date: Thu, 28 Jun 2018 16:47:49 -0400 Subject: add an example --- README.rst | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'README.rst') diff --git a/README.rst b/README.rst index c1d9088..eb1f591 100644 --- a/README.rst +++ b/README.rst @@ -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 p) { + p.first *= p.second; + p.second++; + return p; + }); + t.then([](std::pair 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 === -- cgit v1.2.3