Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <functional>
- #include <vector>
- using namespace std;
- #define show(X) cout << #X << ": " << X << endl;
- #define MAX_DEPTH 10
- template<class T>
- struct lazy {
- lazy() = default;
- using num_type = function<T()>;
- num_type number;
- int depth = 1;
- lazy(const T &in) {
- number = [in] () -> T {
- return in;
- };
- }
- lazy(num_type f, int d) : number(f), depth(d + 1) {
- if(depth >= MAX_DEPTH) {
- collapse();
- }
- }
- auto operator+(const lazy& other) -> lazy {
- auto f = [other, n = number] () -> T {
- return other.number() + n();
- };
- return lazy { f, depth };
- }
- inline void collapse() {
- auto result = number();
- depth = 1;
- if(depth - 1) {
- number = [result] () -> T {
- return result;
- };
- }
- }
- operator T() {
- collapse();
- return number();
- }
- };
- int main() {
- using lint = lazy<int>;
- lint a = 3;
- lint b = 3234;
- while(true) {
- a = a + b;
- }
- }
Add Comment
Please, Sign In to add comment