-
Notifications
You must be signed in to change notification settings - Fork 9
/
functional-programming-emojis.tsx
2366 lines (2328 loc) · 86.4 KB
/
functional-programming-emojis.tsx
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { css, Global } from '@emotion/react'
import { useState, useContext } from 'react'
import Page from 'src/components/Page'
import Head from 'next/head'
import {
ns,
radii,
fontSizes,
colors,
spaces,
maxWidths,
lineHeights
} from 'src/lib/theme'
import { lessonTitle } from 'src/lib/titles'
import Container from 'src/components/Container'
import ExpressionRunnerSeparator from 'src/components/ExpressionRunnerSeparator'
import EmojiSeparator from 'src/components/EmojiSeparator'
import ExpressionRunnerCaptionWrapper from 'src/components/ExpressionRunnerCaptionWrapper'
import BottomRightBadge from 'src/components/BottomRightBadge'
import Emoji from 'src/components/Emoji'
import H from 'src/components/H'
import EmojiForLetter from 'src/components/EmojiForLetter'
import EpisodeHero from 'src/components/EpisodeHero'
import InlinePrioritiesLabel from 'src/components/InlinePrioritiesLabel'
import EpisodePageFooter from 'src/components/EpisodePageFooter'
import EmojiNumber from 'src/components/EmojiNumber'
import CustomEmoji from 'src/components/CustomEmoji'
import ButtonWithTouchActiveStates from 'src/components/ButtonWithTouchActiveStates'
import * as R from 'src/components/Runners'
import { JimsTalk } from 'src/contents/0.en'
import {
ExternalLink,
P,
Italic,
Bold,
InternalLink,
Highlight,
H3,
Ul,
Hr,
UlLi
} from 'src/components/ContentTags'
import locale from 'src/lib/locale'
import { DateTime } from 'luxon'
import { enBaseUrl, githubRepo } from 'src/lib/meta'
import BaseAlert, { alertSpacing, AlertProps } from 'src/components/Alert'
import PrismHighlight, { defaultProps } from 'prism-react-renderer'
import theme from 'prism-react-renderer/themes/nightOwlLight'
import BubbleQuoteContext from 'src/components/BubbleQuoteContext'
import EmojiWithText from 'src/components/EmojiWithText'
import ExpressionRunnerConfigContext from 'src/components/ExpressionRunnerConfigContext'
import {
StepOne,
StepTwo,
InstructionTwo,
StepThree,
InstructionThree,
InstructionFour,
StepFour
} from 'src/contents/4.en'
import VariableShadeContext from 'src/components/VariableShadeContext'
import TwoColGrid from 'src/components/TwoColGrid'
const numSteps = 10
const date = DateTime.fromISO('2019-11-08T12:00:00Z')
const dateString = date
.setLocale('en')
.setZone('America/Los_Angeles')
.toFormat('LLL d, yyyy')
const dateSchemaString = date.setZone('America/Los_Angeles').toISO()
const title = 'You Can Explain Functional Programming Using Emojis'
const description =
'A visual representation of Lambda calculus, Church encoding, and Y combinator'
const url = `${enBaseUrl}/functional-programming-emojis`
const ogImageUrl = `${enBaseUrl}/static/images/blog-og.png`
const tweetUrl = `https://twitter.com/intent/tweet?url=${encodeURIComponent(
url
)}&via=chibicode&text=${encodeURIComponent(title)}`
const Ads = () => (
<>
<Ul>
<UlLi>
I’d love it if you could share this on Twitter.{' '}
<ExternalLink href={tweetUrl}>
<CustomEmoji type="twitter" /> Click here to Tweet this article.
</ExternalLink>
</UlLi>
<UlLi>
Also, the source code is{' '}
<ExternalLink href={githubRepo}>available on GitHub</ExternalLink>:{' '}
<iframe
css={css`
vertical-align: middle;
transform: translateY(-0.1em);
`}
src="https://ghbtns.com/github-btn.html?user=chibicode&repo=Y-Combinator-for-Non-programmers&type=star&count=true&size=large"
frameBorder="0"
scrolling="0"
width="160px"
height="30px"
></iframe>
</UlLi>
</Ul>
</>
)
const Alert = (props: AlertProps) => (
<BubbleQuoteContext.Provider value={{ inQuote: false }}>
<div
css={css`
margin-left: ${spaces('-0.25')};
margin-right: ${spaces('-0.25')};
${ns} {
margin-left: 0;
margin-right: 0;
}
`}
>
<BaseAlert {...props} />
</div>
</BubbleQuoteContext.Provider>
)
Alert.defaultProps = BaseAlert.defaultProps
const Subheading = ({
step,
noHrTop,
children,
coveredIn,
...props
}: JSX.IntrinsicElements['h3'] & {
noHrTop?: boolean
step: number | 'none'
coveredIn: number | 'none'
}) => (
<>
{!noHrTop && (
<Hr
id={step !== 'none' ? `${step}` : undefined}
css={css`
margin-top: ${spaces(2.25)};
`}
/>
)}
{step !== 'none' && coveredIn !== 'none' && (
<div
css={css`
color: ${colors('indigo300')};
margin-bottom: ${spaces(0.25)};
`}
>
Step{' '}
<Bold
css={css`
color: ${colors('indigo400')};
`}
>
{step}
</Bold>
/{numSteps}
<span
css={css`
font-size: ${fontSizes(0.75)};
${ns} {
font-size: ${fontSizes(0.85)};
}
`}
>
{' '}
· From my course’s{' '}
<InternalLink href={`/${coveredIn}`}>
<H
args={{
name: 'titlePrefixColored'
}}
episodeNumberOverrides={coveredIn}
/>
</InternalLink>{' '}
level
</span>
</div>
)}
<H3
{...props}
css={css`
margin: ${noHrTop ? spaces(2) : 0} 0 ${spaces(0.5)};
`}
>
{children}
</H3>
</>
)
const codeFontFamily = `'Victor Mono', SFMono-Regular, Consolas,
Liberation Mono, Menlo, Courier, monospace`
const InlineCode = ({
children,
highlighted
}: {
children: string
highlighted?: boolean
}) => (
<code
css={css`
font-weight: 400;
font-family: ${codeFontFamily};
background-color: ${highlighted ? colors('yellow200') : colors('codeBg')};
display: inline-block;
font-size: 0.85em;
padding: 0.075em 0.2em;
border-radius: ${radii(0.25)};
`}
>
{children}
</code>
)
const CodeBlock = ({
children,
shouldHighlight,
result,
pointToRunButton,
defaultResultVisible,
caption,
noHighlight
}: {
children: string
shouldHighlight?: (lineNumber: number, tokenNumber: number) => boolean
result?: string
pointToRunButton?: boolean
defaultResultVisible: boolean
caption?: React.ReactNode
noHighlight?: boolean
}) => {
const [resultVisible, setResultVisible] = useState(defaultResultVisible)
const buttonOnClick = () => setResultVisible(true)
return (
<div
css={css`
margin-left: auto;
margin-right: auto;
max-width: ${maxWidths('xs')};
`}
>
{caption && (
<ExpressionRunnerCaptionWrapper
css={css`
margin-top: ${spaces(1.75)};
`}
>
{caption}
</ExpressionRunnerCaptionWrapper>
)}
<PrismHighlight
{...defaultProps}
code={children}
theme={theme}
language={noHighlight ? 'diff' : 'javascript'}
>
{({ tokens, getLineProps, getTokenProps }) => (
<pre
css={[
alertSpacing,
css`
background-color: ${colors('codeBg')};
font-weight: 400;
font-family: ${codeFontFamily};
margin-top: ${caption ? 0 : spaces(1.75)};
margin-left: ${spaces('-0.25')};
margin-right: ${spaces('-0.25')};
margin-bottom: ${result ? 0 : spaces(1.75)};
font-size: ${fontSizes(0.8)};
${ns} {
margin-left: 0;
margin-right: 0;
font-size: ${fontSizes(0.85)};
}
`,
result
? css`
border-top-left-radius: ${radii(0.5)};
border-top-right-radius: ${radii(0.5)};
`
: css`
border-radius: ${radii(0.5)};
`,
(!(result && resultVisible) || !result) &&
css`
border-bottom-right-radius: ${radii(0.5)};
`
]}
>
<div
css={css`
overflow-x: auto;
`}
>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{line.map((token, key) => (
<span
{...getTokenProps({ token, key })}
css={[
css`
font-style: normal !important;
`,
!!shouldHighlight &&
shouldHighlight(i, key) &&
css`
background: ${colors('yellow300')};
border-bottom: 2px solid ${colors('deepOrange400')};
`
]}
/>
))}
</div>
))}
</div>
</pre>
)}
</PrismHighlight>
{result && (
<>
<div
css={css`
max-width: ${maxWidths('xs')};
margin-bottom: ${spaces(1.75)};
margin-left: ${spaces('-0.25')};
margin-right: ${spaces('-0.25')};
${ns} {
margin-left: 0;
margin-right: 0;
}
`}
>
{resultVisible ? (
<div
css={[
alertSpacing,
css`
border-top-left-radius: 0;
border-top-right-radius: 0;
border-bottom-left-radius: ${radii(0.5)};
border-bottom-right-radius: ${radii(0.5)};
background: #fff;
border-left: 0.25rem solid ${colors('codeBg')};
border-bottom: 0.25rem solid ${colors('codeBg')};
border-right: 0.25rem solid ${colors('codeBg')};
padding-top: 0.425rem;
padding-bottom: 0.425rem;
margin-top: 0;
${ns} {
padding-top: 0.65rem;
padding-bottom: 0.65rem;
}
`
]}
>
<Bold
css={css`
color: ${colors('indigo400')};
font-size: ${fontSizes(0.85)};
margin-left: -0.25rem;
`}
>
Result:{' '}
</Bold>
<span
css={css`
color: ${colors('indigo500')};
`}
>
<InlineCode highlighted>{result}</InlineCode>
</span>
</div>
) : (
<>
<ButtonWithTouchActiveStates
onClick={buttonOnClick}
activeBackgroundColor={colors('indigo50')}
css={[
alertSpacing,
css`
border-top-left-radius: 0;
border-top-right-radius: 0;
border-bottom-left-radius: ${radii(0.5)};
border-bottom-right-radius: ${radii(0.5)};
line-height: 1.1rem;
border: none;
margin-top: 0;
margin-bottom: 0;
font-weight: bold;
font-size: ${fontSizes(0.85)};
background: ${colors('codeButtonBg')};
color: ${colors('indigo500')};
padding-left: ${spaces(1.25)};
padding-right: ${spaces(1.25)};
&:enabled {
cursor: pointer;
}
@media (hover: hover) {
&:hover:enabled {
background: ${colors('indigo50')};
}
&:focus {
box-shadow: inset 0 0 0 1px ${colors('codeBg')};
outline: none;
}
}
&:active:enabled {
background: ${colors('indigo50')};
}
`
]}
>
Run <Emoji>▶️</Emoji>
</ButtonWithTouchActiveStates>
{pointToRunButton && (
<span
css={[
alertSpacing,
css`
font-size: ${fontSizes(0.85)};
animation: pointToCodeRunButton 1s infinite;
color: ${colors('grey700')};
@keyframes pointToCodeRunButton {
0% {
margin-left: 0px;
}
50% {
margin-left: -5px;
}
100% {
margin-left: 0px;
}
}
`
]}
>
← <H args={{ name: 'pointToRunButton' }} />
</span>
)}
</>
)}
</div>
</>
)}
</div>
)
}
CodeBlock.defaultProps = {
defaultResultVisible: false
}
const PointToRunButton = ({ children }: { children: React.ReactNode }) => {
const config = useContext(ExpressionRunnerConfigContext)
return (
<ExpressionRunnerConfigContext.Provider
value={{
...config,
pointToRunButton: true
}}
>
{children}
</ExpressionRunnerConfigContext.Provider>
)
}
const DimUnhighlighted = ({ children }: { children: React.ReactNode }) => (
<VariableShadeContext.Provider
value={{
shadeNonHighlighted: true
}}
>
{children}
</VariableShadeContext.Provider>
)
const guessTheResult = (
<>
Guess what the result would be
<br />
before pressing the <H args={{ name: 'run' }} /> button.
</>
)
export default () => {
let step = 1
return locale === 'en' ? (
<Page>
<Global
styles={[
css`
@font-face {
font-family: 'Victor Mono';
src: url('/static/fonts/VictorMono-SemiBold.woff2')
format('woff2'),
url('/static/fonts/woff/SemiBold.woff') format('woff');
font-weight: 400;
font-display: fallback;
font-style: normal;
}
`
]}
/>
<Head>
<title key="title">{title}</title>
<meta property="og:title" content={title} />
<meta property="og:site_name" content={lessonTitle} />
<meta property="og:url" content={url} />
<link rel="canonical" href={url} />
<meta property="og:description" content={description} />
<meta name="description" content={description} />
<meta property="og:image" content={ogImageUrl} />
<meta property="article:published_time" content={dateSchemaString} />
</Head>
<Container
Component="article"
size="smsm"
horizontalPadding={0.875}
cssOverrides={css`
padding-top: ${spaces(1.5)};
padding-bottom: ${spaces(6)};
`}
>
<EpisodeHero mainTitle={title} threeLineTitle />
<h2
css={css`
text-align: center;
color: ${colors('indigo400')};
font-size: ${fontSizes(1)};
font-style: italic;
font-weight: normal;
margin: 0 0 ${spaces(0.5)};
${ns} {
font-size: ${fontSizes(1.2)};
}
`}
>
{description}
</h2>
<div
css={css`
font-size: ${fontSizes(0.8)};
color: ${colors('grey600')};
text-align: center;
margin-bottom: ${spaces(2)};
`}
>
Shu Uesugi (
<ExternalLink href="https://twitter.com/chibicode">
@chibicode
</ExternalLink>
)
</div>
<img
src="/static/images/[email protected]"
alt={title}
css={css`
width: 6rem;
margin: ${spaces(2)} auto ${spaces(2.25)};
display: block;
${ns} {
width: 7rem;
}
`}
/>
<ExpressionRunnerConfigContext.Provider
value={{ churchNumerals: true }}
>
<BubbleQuoteContext.Provider value={{ inQuote: true }}>
<P>
Last month, I published a free online course called “
<InternalLink href="/">
<Bold>Y Combinator for Non-programmers</Bold>
</InternalLink>
”. In this 17-page course, I explain functional programming
concepts such as lambda calculus, Church encoding, and Y
combinator in a way such that people{' '}
<Italic>who have zero programming knowledge</Italic> can
understand.
</P>
<P>
I didn’t use any code to explain these concepts. Instead, I
created something called “<Bold>emoji puzzles</Bold>” that can{' '}
<Italic>visualize</Italic> functional code. In this article, I’ll
explain how my emoji puzzles can represent and execute functional
code visually.
</P>
<Subheading noHrTop step="none" coveredIn="none">
Quick Demo
</Subheading>
<P>
First, here’s simple, functional JS code.{' '}
<Highlight>
Try pressing <H args={{ name: 'run' }} /> below
</Highlight>{' '}
to see the result:
</P>
<CodeBlock
result={`'sandwich'`}
pointToRunButton
caption={<>Functional JS code:</>}
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<P>
The above code can be expressed visually using my{' '}
<Bold>emoji puzzle</Bold> below.{' '}
<Highlight>
Try pressing <H args={{ name: 'run' }} />
</Highlight>
:
</P>
<PointToRunButton>
<R.Itbm>Equivalent emoji puzzle:</R.Itbm>
</PointToRunButton>
<P>
I’ll explain how it works shortly. If you like to teach
programming, or if you like functional programming, I think you’ll
enjoy this article.
</P>
<Subheading noHrTop step="none" coveredIn="none">
Overview
</Subheading>
<P>
This article has <Bold>10 steps:</Bold>
</P>
<Ul>
<UlLi>
<Bold>In the first half (steps 1 - 5):</Bold> I’ll show you how
simple JavaScript code can be represented visually using my
emoji puzzles. You’ll be able to understand it even if you’re
not familiar with JS.
</UlLi>
<UlLi>
<Bold>In the second half (steps 6 - 10):</Bold> I’ll talk about
how I used my emoji puzzles to explain functional programming
concepts such as lambda calculus, Church encoding, and Y
combinator.
</UlLi>
</Ul>
<Alert>
<div
css={css`
font-size: ${fontSizes(0.85)};
`}
>
<Emoji>⚠️</Emoji> This article is for programmers. If you’re a
non-programmer, check out my course instead: “
<InternalLink href="/">
<Bold>Y Combinator for Non-programmers</Bold>
</InternalLink>
”.
</div>
</Alert>
<Subheading step={step++} coveredIn={3}>
Identity function in JS
</Subheading>
<P>
Take a look at the following code. It’s an{' '}
<Italic>identity function</Italic> in JavaScript that returns the
argument.
</P>
<CodeBlock>{`// Identity function in JS
sushi => sushi`}</CodeBlock>
<P>
If you apply the above function on a string{' '}
<InlineCode>'sandwich'</InlineCode>, the result will be{' '}
<InlineCode>'sandwich'</InlineCode>.
</P>
<CodeBlock>{`// The result will be 'sandwich'
(sushi => sushi)('sandwich')`}</CodeBlock>
<P>
One day, I realized that the above JS code can be{' '}
<Italic>described visually</Italic> using emojis like below. I
called this an “<Bold>emoji puzzle</Bold>”.
</P>
<R.Ilpo>
An “<Bold>emoji puzzle</Bold>” that visually describes
<br />
<InlineCode>{`(sushi => sushi)('sandwich')`}</InlineCode>
</R.Ilpo>
<P>
The above emoji puzzle is equivalent to the earlier JS code.
First, the identity function{' '}
<InlineCode>{`sushi => sushi`}</InlineCode>…
</P>
<CodeBlock
shouldHighlight={(lineNumber, tokenNumber) =>
lineNumber === 0 && tokenNumber > 0 && tokenNumber < 5
}
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<P>
…is represented by <Italic>the bottom two items</Italic>, which
are both <EmojiWithText letter="a" />.
</P>
<DimUnhighlighted>
<R.Elku>
<InlineCode>{`sushi => sushi`}</InlineCode> is represented
<br />
by the bottom two items
</R.Elku>
</DimUnhighlighted>
<P>
Second, the argument <InlineCode>'sandwich'</InlineCode> to the
identity function…
</P>
<CodeBlock
shouldHighlight={(lineNumber, tokenNumber) =>
lineNumber === 0 && tokenNumber > 6 && tokenNumber < 8
}
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<P>
…is represented by <Italic>the top item</Italic>, which is a{' '}
<EmojiWithText letter="b" />.
</P>
<DimUnhighlighted>
<R.Vowa>
The argument <InlineCode>'sandwich'</InlineCode> is represented
<br />
by the top item
</R.Vowa>
</DimUnhighlighted>
<P>
That’s how my emoji puzzles can visually describe a simple JS
expression. Next, let’s talk about how we can{' '}
<H args={{ name: 'run', lowerCase: true }} /> it.
</P>
<Alert backgroundColor="brown">
<P>
<Bold>Note:</Bold> To keep things simple, emoji puzzles don’t
distinguish between variable names (e.g.{' '}
<InlineCode>sushi</InlineCode>) and strings (e.g.{' '}
<InlineCode>'sushi'</InlineCode>). Therefore, both{' '}
<InlineCode>sushi</InlineCode> and{' '}
<InlineCode>'sushi'</InlineCode> will be represented as{' '}
<EmojiForLetter letter="a" size="semilg" />.
</P>
<P
css={css`
margin-bottom: 0;
`}
>
<Bold>Why emojis?</Bold> I used emojis because they are not
scary-looking for non-programers. I used food emojis because…I
like food. <Emoji>😉</Emoji>
</P>
</Alert>
<Subheading step={step++} coveredIn={4}>
Running the function
</Subheading>
<P>
I’ve added the <H args={{ name: 'run' }} /> button to the JS code
snippet below. If you press it, you’ll see that the result is{' '}
<InlineCode>'sandwich'</InlineCode>.
</P>
<CodeBlock
result={`'sandwich'`}
pointToRunButton
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<P>
We can also “run” the equivalent emoji puzzle and get the same
result.{' '}
<Highlight>
Try pressing the <H args={{ name: 'run' }} /> button below.
</Highlight>
</P>
<PointToRunButton>
<R.Itbm>
An emoji puzzle that’s equivalent to
<br />
<InlineCode>{`(sushi => sushi)('sandwich')`}</InlineCode>
</R.Itbm>
</PointToRunButton>
<P>
The result is a <EmojiWithText letter="b" />, which is the same as
what happens when you run the equivalent JS code.
</P>
<P>
So, you can <H args={{ name: 'run', lowerCase: true }} /> an emoji
puzzle just as you can run a piece of JS code. This is how I
taught functional programming to non-programmers in my course (
<InternalLink href="/">
Y Combinator for Non-programmers
</InternalLink>
)—without showing any code.
</P>
<P>
<Bold>Let’s take a look at another example.</Bold> Here’s a piece
of JS code that’s slightly different from before. It’s a function
that <Italic>ignores the input</Italic> and always returns{' '}
<InlineCode>'pizza'</InlineCode>.
</P>
<CodeBlock>{`// A function that ignores the input
// and always returns 'pizza'
sushi => 'pizza'`}</CodeBlock>
<P>
Let’s run the above code with <InlineCode>'sandwich'</InlineCode>{' '}
as the argument. <H args={{ name: 'pressRun' }} />
</P>
<CodeBlock
result={`'pizza'`}
>{`(sushi => 'pizza')('sandwich')`}</CodeBlock>
<P>
As expected, the result is <InlineCode>'pizza'</InlineCode>. Now,
this code can be represented using an emoji puzzle as follows.{' '}
<H args={{ name: 'pressRun' }} />
</P>
<R.Qcme>
An emoji puzzle that’s equivalent to
<br />
<InlineCode>{`(sushi => 'pizza')('sandwich')`}</InlineCode>
</R.Qcme>
<P>
Just like the JS code, the emoji puzzle ended up with a{' '}
<EmojiWithText letter="f" /> after running it.
</P>
<Alert backgroundColor="pink">
<P>
<Bold>What we have learned so far:</Bold> Simple JS code like
below can be represented using emoji puzzles.
</P>
<CodeBlock
defaultResultVisible
result={`'sandwich'`}
caption={<>Functional JS code:</>}
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<R.Ilpo>Equivalent emoji puzzle:</R.Ilpo>
<ExpressionRunnerSeparator />
<R.Lngo></R.Lngo>
<Hr />
<CodeBlock
defaultResultVisible
result={`'pizza'`}
caption={<>Functional JS code:</>}
>{`(sushi => 'pizza')('sandwich')`}</CodeBlock>
<R.Bjny>Equivalent emoji puzzle:</R.Bjny>
<ExpressionRunnerSeparator />
<R.Ukzq></R.Ukzq>
</Alert>
<Subheading step={step++} coveredIn={4}>
Visualizing evaluation rules
</Subheading>
<P>
If you know how to code, you have a <Italic>mental model</Italic>{' '}
of how function evaluation works:
</P>
<Ul>
<UlLi>
If you see{' '}
<InlineCode>{`(sushi => sushi)('sandwich')`}</InlineCode>, you
can quickly figure out that the result would be{' '}
<InlineCode>'sandwich'</InlineCode>.
</UlLi>
<UlLi>
If you see{' '}
<InlineCode>{`(sushi => 'pizza')('sandwich')`}</InlineCode>, you
know that the result would be <InlineCode>'pizza'</InlineCode>.
</UlLi>
<UlLi>
You know what free variables and bound variables mean.
</UlLi>
</Ul>
<P>
On the other hand, most non-programmers don’t have a mental model
of how function evaluation works. To help them develop the mental
model, I created a <Italic>step-by-step visualization</Italic> of
function evaluation rules using emoji puzzles.
</P>
<P>Let’s reuse the earlier example again:</P>
<CodeBlock
caption={<>Functional JS code:</>}
>{`(sushi => sushi)('sandwich')`}</CodeBlock>
<R.Ilpo>Equivalent emoji puzzle:</R.Ilpo>
<P>
On the puzzle below,{' '}
<Highlight>
try pressing the <H args={{ name: 'run' }} /> button
</Highlight>
. This button is a bit different from the last time—it shows{' '}
<Italic>every step</Italic> that happens in between.
</P>
<PointToRunButton>
<R.Wunw></R.Wunw>
</PointToRunButton>
<P>Here are the four steps it displayed:</P>
<P>
<Bold>Step 1.</Bold> <StepOne />
</P>
<P>
First, we label each emoji. The top item is labeled as{' '}
<BottomRightBadge inline bottomRightBadgeType="callArg" /> (for “
<Bold>T</Bold>op”), the left item is labeled as{' '}
<BottomRightBadge inline bottomRightBadgeType="funcArg" /> (for “
<Bold>L</Bold>eft”), and the right item is labeled as{' '}
<BottomRightBadge inline bottomRightBadgeType="funcBound" /> (for
“<Bold>R</Bold>ight”).
</P>
<R.Zzxj>
<StepOne />
</R.Zzxj>
<P>
<Bold>Step 2.</Bold> <StepTwo />
</P>
<P>
Second, <InstructionTwo lowerCase /> In this case, both the
bottom-left and the bottom-right are <EmojiWithText letter="a" />,
so there’s a match.
</P>
<R.Keck>
<StepTwo />
</R.Keck>
<P>
<Bold>Step 3.</Bold> <StepThree />
</P>
<P>
Third, <InstructionThree lowerCase /> In this case, we copy the{' '}
<EmojiWithText letter="b" /> on the top to the bottom-right.
</P>
<R.Qoms>
<StepThree />
</R.Qoms>
<P>
<Bold>Step 4.</Bold> <StepFour />
</P>
<P>
Finally, <InstructionFour lowerCase /> We’re left with just the{' '}
<EmojiWithText letter="b" /> at the end.
</P>
<R.Mhgm>
<StepFour />
</R.Mhgm>
<ExpressionRunnerSeparator />
<R.Lngo></R.Lngo>
<P>
The above steps are a visual representation of how functions are
evaluated. They are equivalent to how JavaScript evaluates{' '}
<InlineCode>{`(sushi => sushi)('sandwich')`}</InlineCode>.
</P>
<P>
By learning these rules, non-programmers will be able to evaluate
functions intuitively.
</P>
<Alert>
<P>
<Bold>Note:</Bold> The above steps would look{' '}
<Italic>very</Italic> confusing to most programmers. As
programmers, we already know how to evaluate functional
expressions, and we instinctively try to map the above diagrams
to code in our head.
</P>
<P>
But in my testing,{' '}
<Italic>
it seems to be less confusing to non-programmers
</Italic>{' '}
who don’t already have a mental model of function evaluation.
Furthermore,{' '}
<InternalLink href="/">in the course itself</InternalLink>, I
slow down and spend a lot more time covering the above rules—the
explanations are spread out in a full page (
<InternalLink href="/4">here</InternalLink>
). And I don’t show any code in my course—I only show emoji
puzzles, so one less source of confusion compared to this
article.
</P>
<P>
One thing I learned is that the feedback on my course from
programmers and non-programmers are complete opposites—
<Italic>generally negative from programmers</Italic> but{' '}
<Italic>generally positive from non-programmers</Italic>. I
believe that’s because{' '}
<Bold>
many programmers find that using code is simpler than using
the above diagrams, but many non-programmers think the
opposite.
</Bold>
</P>
<P
css={css`
margin-bottom: 0;
`}
>
Some people have incredible difficulty understanding code or
math symbols, and sometimes visual alternatives help them. I
believe the more alternative teaching methods are offered in CS
education, the better.