From 147d3e1044bb0211d6b981b4eca079a4900fa8bd Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Sat, 26 Nov 2022 12:26:48 -0800 Subject: Add the iteration optimization --- entries/braxtonhall/hitl2/fib.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'entries') diff --git a/entries/braxtonhall/hitl2/fib.js b/entries/braxtonhall/hitl2/fib.js index 3f1a148..e050d96 100644 --- a/entries/braxtonhall/hitl2/fib.js +++ b/entries/braxtonhall/hitl2/fib.js @@ -123,6 +123,13 @@ const hitlfib = (n) => .then((map) => Promise.resolve(build(n, map)) .then((top) => fill(map, top))); +const hitlfibiter = (n, a = 0, b = 1) => + n === 0 + ? Promise.resolve(a) + : n === 1 + ? Promise.resolve(b) + : add(a, b).then((result) => hitlfibiter(n - 1, b, result)); + const number = () => prompt("give me a number") .then((str) => str.trim()) @@ -131,6 +138,12 @@ const number = () => ? n : slow(hedge()).then(() => slow(innumerable())).then(number))); +const choose = () => + prompt("would you like to use my iteration optimization??? yes????") + .then((response) => ["y", "yes"].includes(response.trim().toLowerCase())) + .then((should) => should + ? fast("yes!").then(() => hitlfibiter) + : fast("oh.").then(() => slow("okay.")).then(() => slow("we can take it slow.")).then(() => hitlfib)); const flow = () => slow("\nwhat's the fib you'd like to compute? i can help u out") @@ -138,7 +151,8 @@ const flow = () => .then((n) => slow(confirmation()) .then(() => fast(hedge())) .then(() => slow(`i will help u compute ${green(`fib ${n}`)}`)) - .then(() => hitlfib(n)) + .then(choose) + .then((fib) => fib(n)) .then((fibn) => slow(hedge()) .then(() => fast(done())) .then(() => fast(thinking())) -- cgit v1.2.3-70-g09d2