From 85e2f948ee4ae6793448969c6d2f9610d74e42ce Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Sat, 26 Nov 2022 12:04:43 -0800 Subject: Add human in the loop 2 for the exhibition --- entries/braxtonhall/hitl2/fib.js | 151 +++++++++++++++++++++++++++++++++++++++ people.json | 4 ++ 2 files changed, 155 insertions(+) create mode 100644 entries/braxtonhall/hitl2/fib.js diff --git a/entries/braxtonhall/hitl2/fib.js b/entries/braxtonhall/hitl2/fib.js new file mode 100644 index 0000000..3f1a148 --- /dev/null +++ b/entries/braxtonhall/hitl2/fib.js @@ -0,0 +1,151 @@ +const readline = require('readline'); + +const random = (lo, hi) => lo + Math.floor(Math.random() * (hi - lo)); +const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); +const rsleep = (lo, hi) => sleep(random(lo, hi)); + +const interface = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const prompt = (question) => + new Promise((resolve) => + interface.question(`${blue(question)}\n> `, resolve)); + +const coloured = (colour) => (str) => `${colour}${reset(str)}`; +const yellow = coloured("\x1b[33m"); +const blue = coloured("\x1b[34m"); +const green = coloured("\x1b[32m"); +const reset = (str) => `${str}\x1b[0m`; + +const add = (a, b) => compute(a, b, "+"); + +const hedges = [ + "um. okay. so,", + "right. next. uh,", + "hmm", + "okay. so,", + "um. okay.", + "uh okay..", + "so..", +]; +const confirmations = [ + "o right. ofc.", + "nice.", + "thank u.", + "thanks.", + "um right. yes.", + "right.", +]; +const innumerables = [ + "yeah i don't think we can do that one", + "that's not great for fib", + "um no sorry i can't help with that one", + "um????? no my b i can't help with that one", + "oh no my bad no i can't help u with that", + "sorry not that one", + "yaaa not that one though", +]; +const dones = [ + "we're done aren't we?", + "oh! that's it..?", + "all ... done??????", + "i think it's over", + "no more computation?", + "oh!", + "wait you did it all.", + "oh that's it?", +]; +const thinkings = [ + "let me just think a second..", + "hold on..", + "i just need a moment", + "uh wait", + "just looking at the data", + "sorry - looking over things", + "got everything just one sec", + "let me look at things", + "let me think a moment", + "just thinking ...", +]; +const rarray = (array) => array[random(0, array.length)]; +const hedge = () => rarray(hedges); +const confirmation = () => rarray(confirmations); +const innumerable = () => rarray(innumerables); +const done = () => rarray(dones); +const thinking = () => rarray(thinkings); + +const rentries = (map) => rarray(Array.from(map.entries())); + +const say = (msg, before, after) => + sleep(before) + .then(() => console.log(...msg)) + .then(() => sleep(after)); + +const fast = (...msg) => say(msg, random(30, 100), random(30, 100)); +const slow = (...msg) => say(msg, random(400, 2000), random(200, 1000)); + +const compute = (a, b, op) => + slow(hedge()) + .then(() => prompt(`what is ${a} ${op} ${b}?`)) + .then((value) => slow(confirmation()).then(() => value)); + +const build = (n, map) => { + if (n <= 0) { + const zero = () => Promise.resolve(); + map.set(zero, 0); + return zero; + } else if (n === 1) { + const one = () => Promise.resolve(); + map.set(one, 1); + return one; + } else { + const sub1 = build(n - 1, map); + const sub2 = build(n - 2, map); + const nth = () => + (map.get(nth) === null && (map.get(sub1) !== null && map.get(sub2) !== null)) + ? add(map.get(sub2), map.get(sub1)) + .then((value) => Promise.resolve(map.set(nth, value)).then(() => value)) + : Promise.resolve(); + map.set(nth, null); + return nth; + }; +}; + +const fill = (map, top) => + map.get(top) === null + ? rentries(map)[0]().then(() => fill(map, top)) + : Promise.resolve(map.get(top)); + +const hitlfib = (n) => + Promise.resolve(new Map()) + .then((map) => Promise.resolve(build(n, map)) + .then((top) => fill(map, top))); + +const number = () => + prompt("give me a number") + .then((str) => str.trim()) + .then((trimmed) => Promise.resolve(Number(trimmed)) + .then((n) => (trimmed !== "" && n !== null && isFinite(n)) + ? n + : slow(hedge()).then(() => slow(innumerable())).then(number))); + + +const flow = () => + slow("\nwhat's the fib you'd like to compute? i can help u out") + .then(number) + .then((n) => slow(confirmation()) + .then(() => fast(hedge())) + .then(() => slow(`i will help u compute ${green(`fib ${n}`)}`)) + .then(() => hitlfib(n)) + .then((fibn) => slow(hedge()) + .then(() => fast(done())) + .then(() => fast(thinking())) + .then(() => rsleep(Math.min(1000, n * 100), Math.max(n * 500, 1000))) + .then(() => slow(green(`fib ${n} is ${fibn}`))))) + .then(() => rsleep(1000, 4000)); + +const loop = () => flow().then(loop); + +loop(); diff --git a/people.json b/people.json index fc6a8c2..44c70b7 100644 --- a/people.json +++ b/people.json @@ -53,6 +53,10 @@ { "name": "wc", "link": "./entries/braxtonhall/wc/fib.sh" + }, + { + "name": "hitl2", + "link": "./entries/braxtohall/hitl2/fib.js" } ] }, -- cgit v1.2.3-70-g09d2 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(-) 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 From e5081339ca952c0c55cf12cef48d6f6f9e0bc05c Mon Sep 17 00:00:00 2001 From: braxtonhall Date: Sat, 26 Nov 2022 12:38:45 -0800 Subject: Fix a typo in people.json --- people.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/people.json b/people.json index 44c70b7..2f0ac41 100644 --- a/people.json +++ b/people.json @@ -56,7 +56,7 @@ }, { "name": "hitl2", - "link": "./entries/braxtohall/hitl2/fib.js" + "link": "./entries/braxtonhall/hitl2/fib.js" } ] }, -- cgit v1.2.3-70-g09d2