show pure function

This commit is contained in:
Elizabeth Hunt 2024-02-09 11:27:11 -07:00
parent 7cf2017e95
commit 609338de80
Signed by: simponic
GPG Key ID: 52B3774857EB24B1
3 changed files with 47 additions and 24 deletions

View File

@ -300,7 +300,7 @@ export class FunctionBox extends Node {
yield* all( yield* all(
this.boxMoji().text("", duration), this.boxMoji().text("", duration),
this.boxMoji().opacity(0, duration), this.boxMoji().opacity(0, duration),
this.block().fontSize(30, duration), this.block().fontSize(20, duration),
this.node().opacity(1, duration), this.node().opacity(1, duration),
); );
} }

View File

@ -8,12 +8,12 @@ import hungry_partner from "./hungry_partner?scene";
import pure_functions from "./pure_functions?scene"; import pure_functions from "./pure_functions?scene";
export const scenes = [ export const scenes = [
title, //title,
me, //me,
partone, //partone,
flirtingwithfunctions, //flirtingwithfunctions,
doctor, //doctor,
first_box, //first_box,
hungry_partner, //hungry_partner,
pure_functions, pure_functions,
]; ];

View File

@ -1,4 +1,4 @@
import { Node, Rect, Txt, makeScene2D } from "@motion-canvas/2d"; import { Rect, Txt, makeScene2D } from "@motion-canvas/2d";
import { import {
Direction, Direction,
all, all,
@ -10,11 +10,10 @@ import {
import { FunctionBox } from "../components/function_box"; import { FunctionBox } from "../components/function_box";
import { theme } from "../theme"; import { theme } from "../theme";
const pureFunction = `const pureFib = (n: number) => { const pureFib = `const pureFib = (n: number) => {
if (n <= 1) { if (n <= 1)
retun 1; return 1;
} return pureFib(n - 1) + pureFib(n - 2);
return fib(n - 1) + fib(n - 2);
}; };
pureFib;`; pureFib;`;
@ -27,7 +26,8 @@ const impureFib = (n: number) => {
impureFib;`; impureFib;`;
const impureFactFunction = `const impureFact = (n: number): number => { const impureFactFunction = `const impureFact = (n: number): number => {
while (cache.length <= n) while (cache.length <= n)
cache.push(cache.length * cache[cache.length - 1]); cache.push(cache.length *
cache[cache.length - 1]);
return cache[n]; return cache[n];
}; };
impureFact;`; impureFact;`;
@ -66,7 +66,7 @@ export default makeScene2D(function* (view) {
arity={1} arity={1}
idlingText="" idlingText=""
workingText="" workingText=""
source={pureFunction} source={pureFib}
ref={pureFunctionBox} ref={pureFunctionBox}
/> />
</Rect> </Rect>
@ -81,24 +81,47 @@ export default makeScene2D(function* (view) {
impureFactFunctionBox().showCode(0.5), impureFactFunctionBox().showCode(0.5),
); );
yield* impureFibFunctionBox().setInputs([{ val: 5 }], 0.05); yield* impureFibFunctionBox().setInputs([{ val: 5 }], 0.2);
yield* beginSlide("Show FibImpure(5)"); yield* beginSlide("Show FibImpure(5)");
yield* impureFibFunctionBox().propogateInput(0.05); yield* impureFibFunctionBox().propogateInput(0.2);
yield* impureFibFunctionBox().propogateOutput(0.05); yield* impureFibFunctionBox().propogateOutput(0.2);
yield* beginSlide("FibImpure(5)"); yield* beginSlide("FibImpure(5)");
yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.05); yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.2);
yield* beginSlide("Show FactImpure(5)"); yield* beginSlide("Show FactImpure(5)");
yield* impureFactFunctionBox().propogateInput(0.05); yield* impureFactFunctionBox().propogateInput(0.2);
yield* impureFactFunctionBox().propogateOutput(0.05); yield* impureFactFunctionBox().propogateOutput(0.2);
yield* beginSlide("FactImpure(5)"); yield* beginSlide("FactImpure(5)");
yield* impureFactFunctionBox().reset(0.15); yield* impureFactFunctionBox().reset(0.15);
yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.15); yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.15);
yield* beginSlide("FactImpure(5) Add 5"); yield* beginSlide("FactImpure(5) Add 5");
yield* impureFactFunctionBox().propogateInput(0.05); yield* impureFactFunctionBox().propogateInput(0.2);
yield* impureFactFunctionBox().propogateOutput(0.05); yield* impureFactFunctionBox().propogateOutput(0.2);
yield* beginSlide("FactImpure(5) Correct"); yield* beginSlide("FactImpure(5) Correct");
yield* all(
impureFibFunctionBox().reset(0.5),
impureFactFunctionBox().reset(0.5),
impureFibFunctionBox().hideCode(0.5),
pureFunctionBox().showCode(0.5),
);
yield* beginSlide("Show Fib Pure");
yield* pureFunctionBox().reset(0.15);
yield* all(
pureFunctionBox().setInputs([{ val: 5 }], 0.15),
impureFactFunctionBox().setInputs([{ val: 5 }], 0.15),
);
yield* beginSlide("Set Inputs");
yield* pureFunctionBox().propogateInput(0.2);
yield* pureFunctionBox().propogateOutput(0.2);
yield* impureFactFunctionBox().propogateInput(0.2);
yield* impureFactFunctionBox().propogateOutput(0.2);
yield* beginSlide("Compute Fib");
}); });