aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbraxtonhall2022-11-26 20:26:48 +0000
committerbraxtonhall2022-11-26 20:26:48 +0000
commit147d3e1044bb0211d6b981b4eca079a4900fa8bd (patch)
treec3efccd434de6ae2cccac474be505ad9e02153b4
parent85e2f948ee4ae6793448969c6d2f9610d74e42ce (diff)
Add the iteration optimization
-rw-r--r--entries/braxtonhall/hitl2/fib.js16
1 files changed, 15 insertions, 1 deletions
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()))