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(); view.add( , ); 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"); });