“person flip skiing above snow during daytime” by on Jörg Angeli Unsplash Many people look at the Coroutines TS and wonder, what is all of the fuss about? This article gives you one motivating example where lazy sequences enable you to and maximize code reuse without increasing complexity. seperate your concerns Example: Approximating the Golden Ratio Every Math student knows how to approximate the golden ratio via the Fibonacci series: fib(n+1) / fib(n) -> φ = 1.6180… Or using C++… The function computes the golden ratio to a given approximation. On each iteration we get a little bit closer to the golden ratio, and the function returns once we are close enough. golden This was straightforward, but what if we have some additional use-cases for ? golden Maybe we want to limit the maximum number of iterations Or maybe we want to print after every iteration Or maybe we want to return a tuple of { fib(n+1), fib(n) } Or maybe there is a use-case we haven’t thought of yet, but someone might have down the line In order to satisfy all of these, we would need to modify the algorithm and provide some customization points. We could add a set of configuration options or perhaps use generic functions: We can see that the complexity of this function increased as we generalized it to handle more use-cases. This version of is much less readable than the original! golden An alternative approach would have been to write a variant of this function for each specific use-case. Here’s a version that prints each iteration: This would be readable, but now we have to maintain multiple functions! How can we achieve reusability and simplicity? Here’s the problem. The templated version is flexible, but it is not simple. The function variants are simple, but not flexible. How can we achieve reusability and simplicity? Why do we have to choose between them at all? This issue is caused by the fact that the algorithm controls the and . These concerns should be separated. iteration of the sequence representation of the sequence So what kind of abstraction do we need to have the get the best of both worlds? Inversion of Control via Coroutines TS The Coroutines TS enables you to define lazy sequences. A lazy sequence is just an ordered set of values, where each value is only computes as it is requested. You might think of a lazy sequence as a container with two properties: is there another value in this sequence? bool hasNext() return the next value in the sequence, and advance by 1. T takeNext() As a result, we are able to decouple computation and representation, while maintaining full control over the sequence. The Coroutine TS is quite low-level, so the following examples are built using our library. This library gives us various primitives that we can use to create lazy sequences. We won’t dive into the implementation details here (there is full ), but instead look at how all of this is useful! Conduit source-code over on GitHub OK. So using we can define a lazy sequence for Fibonacci: Conduit Since the golden ratios are a transformation of the Fibonacci numbers, we can compute them using a : scan Here we use the sequence to print the first 10 Fibonacci-ratios: Now that we have an iterable, we can control the iteration by computing a delta as before: The beauty of this is that we can take just one definition of the sequence, but use it in multiple places. Unlike the templated solution we saw earlier, the code still maps closely to the mathematical definition. If you are curious how to implement lazy sequences using the Coroutine TS,checkout our . GitHub Conclusion The Coroutines TS allows us to give control back to the consumer of an algorithm. This frees the implementer from providing customization hooks to the end-user to modify the behaviour of the algorithm. Since You’re Here… We recently announced , a new tool to optimize your C/C++ build scripts. ! BuildInfer Take a look You might also be interested in… _TL;DR_hackernoon.com Error Handling in C++ or: Why You Should Use Eithers in Favor of Exceptions and Error-codes _TL;DR_hackernoon.com value_ptr — The Missing C++ Smart-pointer