From 05572d84046d7113663ae02b3ba545766c69da33 Mon Sep 17 00:00:00 2001 From: Elizabeth Hunt Date: Thu, 8 Feb 2024 19:35:56 -0700 Subject: update --- src/scenes/pure_functions.tsx | 104 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/scenes/pure_functions.tsx (limited to 'src/scenes/pure_functions.tsx') diff --git a/src/scenes/pure_functions.tsx b/src/scenes/pure_functions.tsx new file mode 100644 index 0000000..c7e1f35 --- /dev/null +++ b/src/scenes/pure_functions.tsx @@ -0,0 +1,104 @@ +import { Node, Rect, Txt, makeScene2D } from "@motion-canvas/2d"; +import { + Direction, + all, + beginSlide, + createRef, + slideTransition, +} from "@motion-canvas/core"; + +import { FunctionBox } from "../components/function_box"; +import { theme } from "../theme"; + +const pureFunction = `const pureFib = (n: number) => { + if (n <= 1) { + retun 1; + } + return fib(n - 1) + fib(n - 2); +}; +pureFib;`; + +const impureFibFunction = `window.cache = [1, 1]; +const impureFib = (n: number) => { + while (cache.length <= n + 1) + cache.push(cache.at(-1) + cache.at(-2)); + return cache[n]; +}; +impureFib;`; +const impureFactFunction = `const impureFact = (n: number): number => { + while (cache.length <= n) + cache.push(cache.length * cache[cache.length - 1]); + return cache[n]; +}; +impureFact;`; + +export default makeScene2D(function* (view) { + const impureFibFunctionBox = createRef(); + const impureFactFunctionBox = createRef(); + const pureFunctionBox = createRef(); + + view.add( + + + + IMPURE + + + + + + + PURE + + + + , + ); + + yield* slideTransition(Direction.Right); + yield* beginSlide("Pure vs Impure"); + + yield* all( + impureFibFunctionBox().showCode(0.5), + impureFactFunctionBox().showCode(0.5), + ); + + yield* impureFibFunctionBox().setInputs([{ val: 5 }], 0.05); + yield* beginSlide("Show FibImpure(5)"); + + yield* impureFibFunctionBox().propogateInput(0.05); + yield* impureFibFunctionBox().propogateOutput(0.05); + yield* beginSlide("FibImpure(5)"); + + yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.05); + yield* beginSlide("Show FactImpure(5)"); + + yield* impureFactFunctionBox().propogateInput(0.05); + yield* impureFactFunctionBox().propogateOutput(0.05); + yield* beginSlide("FactImpure(5)"); + + yield* impureFactFunctionBox().reset(0.15); + yield* impureFactFunctionBox().setInputs([{ val: 5 }], 0.15); + yield* beginSlide("FactImpure(5) Add 5"); + yield* impureFactFunctionBox().propogateInput(0.05); + yield* impureFactFunctionBox().propogateOutput(0.05); + yield* beginSlide("FactImpure(5) Correct"); +}); -- cgit v1.2.3-70-g09d2