diff options
Diffstat (limited to 'src/scenes/currying_detail.tsx')
-rw-r--r-- | src/scenes/currying_detail.tsx | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/scenes/currying_detail.tsx b/src/scenes/currying_detail.tsx new file mode 100644 index 0000000..fd7c3d3 --- /dev/null +++ b/src/scenes/currying_detail.tsx @@ -0,0 +1,155 @@ +import { Layout, makeScene2D } from "@motion-canvas/2d"; +import { + Direction, + beginSlide, + createRef, + slideTransition, +} from "@motion-canvas/core"; +import { + CodeBlock, + edit, + insert, + range, +} from "@motion-canvas/2d/lib/components/CodeBlock"; +import { cardGeneratorForSource } from "./function_dna"; +import { cardGeneratorsForSource } from "./currying"; + +export default makeScene2D(function* (view) { + const curried = createRef<CodeBlock>(); + + view.add( + <Layout direction="row" gap={100} layout> + <CodeBlock + fontSize={25} + language="typescript" + ref={curried} + code={cardGeneratorForSource} + /> + </Layout>, + ); + + yield* slideTransition(Direction.Left); + + yield* beginSlide("show function"); + + yield* curried().edit( + 1, + )`const cardGeneratorFor = (person: PersonI, type: string): (() => CardI) => { + const birthdayCardGenerator = () => birthdayCardFn(person); // closure + const valentineCardGenerator = () => valentineCardFn(person); // closure${insert( + "\n", + )} + ${insert(`const generatorForType = (type: string) => {`)} + ${insert(" ")}switch (type) { + ${insert(" ")} case "valentine": + ${insert(" ")} return valentineCardGenerator; + ${insert(" ")} case "birthday": + ${insert(" ")} return birthdayCardGenerator; + ${insert(" ")}} + + ${insert(" ")}throw new Error(type + " not implemented"); + ${insert("};\n")}}`; + + yield* beginSlide("first currying step"); + + yield* curried().edit( + 1, + )`const cardGeneratorFor = (person: PersonI, type: string): (() => CardI) => { + const birthdayCardGenerator = () => birthdayCardFn(person); // closure + const valentineCardGenerator = () => valentineCardFn(person); // closure + + const generatorForType = (type: string) => { + switch (type) { + case "valentine": + return valentineCardGenerator; + case "birthday": + return birthdayCardGenerator; + } + + throw new Error(type + " not implemented"); + };${insert("\n\n return generatorForType;")} +}`; + yield* beginSlide("second currying step"); + + yield* curried().edit(1)`const ${edit( + "cardGeneratorFor", + "cardGeneratorsFor", + )} = ${edit( + "(person: PersonI, type: string)", + "(person: PersonI)", + )}: (() => CardI) => { + const birthdayCardGenerator = () => birthdayCardFn(person); // closure + const valentineCardGenerator = () => valentineCardFn(person); // closure + + const generatorForType = (type: string) => { + switch (type) { + case "valentine": + return valentineCardGenerator; + case "birthday": + return birthdayCardGenerator; + } + + throw new Error(type + " not implemented"); + }; + + return generatorForType; +}`; + yield* beginSlide("third currying step"); + + yield* curried().edit(1)`const cardGeneratorsFor = (person: PersonI): ${edit( + "(() => CardI)", + "((type: string) => () => CardI)", + )} => { + const birthdayCardGenerator = () => birthdayCardFn(person); // closure + const valentineCardGenerator = () => valentineCardFn(person); // closure + + const generatorForType = (type: string) => { + switch (type) { + case "valentine": + return valentineCardGenerator; + case "birthday": + return birthdayCardGenerator; + } + + throw new Error(type + " not implemented"); + }; + + return generatorForType; +}`; + yield* beginSlide("fourth currying step"); + + yield* curried().edit(1)`${edit( + cardGeneratorsForSource, + `const alanCardGenerator = (type: string): (() => CardI) => { + const alan: PersonI = { + name: "Alan Turing", + birthday: new Date("06/23/1912"), + color: theme.green.hex, + }; + + return cardGeneratorFor(alan, type); +};`, + )}`; + yield* beginSlide("partial application"); + + yield* curried().selection( + [...range(7, 9, 7, 38), ...range(0, 26, 0, 40)], + 1, + ); + + yield* beginSlide("highlight"); + + yield* curried().edit( + 1, + )`const alanCardGenerator = (type: string): (() => CardI) => { + const alan: PersonI = { + name: "Alan Turing", + birthday: new Date("06/23/1912"), + color: theme.green.hex, + }; + + return cardGeneratorFor(alan, type); +};${insert(` +(alanCardGenerator("valentine")()).message === (cardGeneratorsFor(alan)("valentine")()).message;`)}`; + yield* beginSlide("show application of pa"); +}); |