summaryrefslogtreecommitdiff
path: root/src/scenes/hungry_partner.tsx
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-02-08 18:36:10 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-02-08 18:36:10 -0700
commit5b8b3abcba8746502014266583907421b71a330b (patch)
treefc3d8a2159a17c8af1d432d93307ff01474fb053 /src/scenes/hungry_partner.tsx
parentc18b81b2f26123481558cb3fffc794c2c13f74ad (diff)
downloadcompiling-the-lambda-calculus-5b8b3abcba8746502014266583907421b71a330b.tar.gz
compiling-the-lambda-calculus-5b8b3abcba8746502014266583907421b71a330b.zip
add hungry partner function
Diffstat (limited to 'src/scenes/hungry_partner.tsx')
-rw-r--r--src/scenes/hungry_partner.tsx108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/scenes/hungry_partner.tsx b/src/scenes/hungry_partner.tsx
new file mode 100644
index 0000000..696870e
--- /dev/null
+++ b/src/scenes/hungry_partner.tsx
@@ -0,0 +1,108 @@
+import { Node, Txt, makeScene2D } from "@motion-canvas/2d";
+import {
+ Direction,
+ beginSlide,
+ createRef,
+ slideTransition,
+} from "@motion-canvas/core";
+
+import { FunctionBox } from "../components/function_box";
+import { theme } from "../theme";
+
+const hungryValentine = `(
+ savoryCravingRatio: number, sweetCravingRatio: number,
+ acidityCravingRatio: number, spiceCravingRatio: number,
+): string => {
+ const foods = {
+ "🍕": {savory: 0.8, sweet: 0.1, acidity: 0.5, spice: 0.5},
+ "🧁": {savory: 0.2, sweet: 0.9, acidity: 0.1, spice: 0.1},
+ "🍋🍰": {savory: 0.1, sweet: 0.8, acidity: 0.9, spice: 0.1},
+ "🌮🔥": {savory: 0.6, sweet: 0.2, acidity: 0.5, spice: 0.8},
+ };
+
+ const weight = (foodProfile: Record<string, number>) =>
+ foodProfile["savory"] * savoryCravingRatio
+ + foodProfile["sweet"] * sweetCravingRatio
+ + foodProfile["acidity"] * acidityCravingRatio
+ + foodProfile["spice"] * spiceCravingRatio;
+
+ const bestFood = Object.keys(foods).reduce((a, b) =>
+ weight(foods[a]) > weight(foods[b]) ? a : b
+ );
+
+ const foodNames = Array.from(Object.keys(foods));
+ const shouldChooseRandom = Math.random() > 0.4; // side effect
+ return shouldChooseRandom
+ ? foodNames[Math.floor(Math.random() * foodNames.length)]
+ : bestFood;
+};`;
+
+export default makeScene2D(function* (view) {
+ const functionBox = createRef<FunctionBox>();
+
+ view.add(
+ <FunctionBox
+ arity={4}
+ idlingText={"😴"}
+ workingText={"😋💭"}
+ source={hungryValentine}
+ ref={functionBox}
+ />,
+ );
+
+ yield* slideTransition(Direction.Right);
+
+ yield* beginSlide("Get Best Food For Partner");
+
+ const order = ["savory", "sweet", "acidic", "spice"];
+
+ for (const [[a, b, c, d], i] of [
+ [0.7, 0.1, 0.4, 0.1],
+ [0.7, 0.1, 0.4, 0.1],
+ [0.7, 0.1, 0.4, 0.1],
+ [0.7, 0.1, 0.4, 0.1],
+ ].map((x, i) => [x, i]) as [[number, number, number, number], number][]) {
+ const inputId = "(" + [a, b, c, d, i].join(",") + ")";
+
+ yield* functionBox().reset(0.5);
+ yield* functionBox().setInputs(
+ [a, b, c, d].map((ratio, i) => ({
+ val: ratio,
+ node: (
+ <Txt fontFamily={theme.font} fill={theme.text.hex}>
+ {order[i]}:{" "}
+ <Txt
+ fontFamily={theme.font}
+ fill={
+ ratio > 0.6
+ ? theme.red.hex
+ : ratio < 0.3
+ ? theme.green.hex
+ : theme.lavender.hex
+ }
+ >
+ {ratio.toString()}
+ </Txt>
+ </Txt>
+ ),
+ })),
+ 0.5,
+ );
+
+ yield* beginSlide("Add Inputs " + inputId);
+
+ yield* functionBox().propogateInput(0.5);
+ yield* beginSlide("Propogate Inputs " + inputId);
+
+ yield* functionBox().propogateOutput(0.5);
+ yield* beginSlide("Propogate Outputs of " + inputId);
+ }
+
+ yield* functionBox().reset(0.5);
+
+ yield* functionBox().showCode(0.85);
+ yield* beginSlide("Show Code");
+
+ yield* functionBox().hideCode(0.85);
+ yield* beginSlide("Hide Code");
+});