1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
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");
});
|