[TS] Recursive Calls with Boolean Flag, 69ms, 42.6MB
Nice warm up problem we can fix thinking of a series of recursive calls that are perpetuated by a boolean flag: each function will keep calling itself (after t 
Nice warm up problem we can fix thinking of a series of recursive calls that are perpetuated by a boolean flag: each function will keep calling itself (after `t` ms, via a timeout), as long as the boolean flag is not flipped.\n\nSo, we will start declaring our boolean flag `shouldRepeat` with a value of `true`.\n\nRight after, we need a function to unflag it and nothing else - this will be our first helper `cancel`, that will turn `shouldRepeat` to `false` when invoked.\n\nFinally, our many recursive function `repeat` will:\n* call `fn` with the provided `args`, as requested;\n* set a timeout to invoke `repeat` again in `t` milliseconds, but only if `shouldRepeat` is still `true`.\n\nWe will then boot `repeat` up and `return` `cancel`.\n\n# Complexity\n- Time complexity: $$O(1)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```ts\nfunction cancellable(fn: Function, args: any[], t: number): Function {\n    // support variables\n    let shouldRepeat = true;\n    // helper functions\n    const cancel = () => shouldRepeat = false;\n    const repeat = () => {\n        fn(...args);\n        setTimeout(() => {\n            shouldRepeat && repeat();\n        }, t);\n    }\n    // booting repeat up\n    repeat();\n    return cancel;\n};\n```\n\nAlternatively, `cancel` can just be `return`ed as a lambda:\n\n```ts\nfunction cancellable(fn: Function, args: any[], t: number): Function {\n    // support variables\n    let shouldRepeat = true;\n    // helper function\n    const repeat = () => {\n        fn(...args);\n        setTimeout(() => {\n            shouldRepeat && repeat();\n        }, t);\n    }\n    // booting repeat up\n    repeat();\n    return () => shouldRepeat = false;\n};\n```
['Recursion', 'TypeScript']