-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
4844 lines (4464 loc) · 190 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"
/>
<title>reveal.js</title>
<link rel="stylesheet" href="dist/reset.css" />
<link rel="stylesheet" href="dist/reveal.css" />
<link rel="stylesheet" href="dist/theme/nord.css" />
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-background-image="images/The Evolution of Functional Programming in C++ ACCU Title Card.png" data-background-size="100% 100%"></section>
<section data-auto-animate>
<h2 class="slide-header">About Me</h2>
<p>Functional and systems programmer focused on Haskell, C and C++</p>
</section>
<section class="socials">
<h2 class="slide-header">Socials</h2>
<p>
<a href="https://www.linkedin.com/in/abel-sen/">
<?xml version="1.0" encoding="UTF-8"?><svg
width="24px"
height="24px"
stroke-width="1.5"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
color="#000000"
>
<path
d="M21 8V16C21 18.7614 18.7614 21 16 21H8C5.23858 21 3 18.7614 3 16V8C3 5.23858 5.23858 3 8 3H16C18.7614 3 21 5.23858 21 8Z"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M7 17V13.5V10"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M11 17V13.75M11 10V13.75M11 13.75C11 10 17 10 17 13.75V17"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M7 7.01L7.01 6.99889"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
</svg>
<span>abel-sen</span>
</a>
</p>
<p>
<a href="https://github.com/neuroevolutus">
<?xml version="1.0" encoding="UTF-8"?><svg
width="24px"
height="24px"
stroke-width="1.5"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
color="#000000"
>
<path
d="M16 22.0268V19.1568C16.0375 18.68 15.9731 18.2006 15.811 17.7506C15.6489 17.3006 15.3929 16.8902 15.06 16.5468C18.2 16.1968 21.5 15.0068 21.5 9.54679C21.4997 8.15062 20.9627 6.80799 20 5.79679C20.4558 4.5753 20.4236 3.22514 19.91 2.02679C19.91 2.02679 18.73 1.67679 16 3.50679C13.708 2.88561 11.292 2.88561 8.99999 3.50679C6.26999 1.67679 5.08999 2.02679 5.08999 2.02679C4.57636 3.22514 4.54413 4.5753 4.99999 5.79679C4.03011 6.81549 3.49251 8.17026 3.49999 9.57679C3.49999 14.9968 6.79998 16.1868 9.93998 16.5768C9.61098 16.9168 9.35725 17.3222 9.19529 17.7667C9.03334 18.2112 8.96679 18.6849 8.99999 19.1568V22.0268"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M9 20.0267C6 20.9999 3.5 20.0267 2 17.0267"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
</svg>
<span>neuroevolutus</span>
</a>
</p>
<p>
<a href="https://gitlab.com/neuroevolutus">
<?xml version="1.0" encoding="UTF-8"?><svg
width="24px"
height="24px"
stroke-width="1.5"
viewBox="0 0 24 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
color="#000000"
>
<path
fill-rule="evenodd"
clip-rule="evenodd"
d="M17.0573 2.54429C17.1158 2.36426 17.3684 2.35862 17.4348 2.53587L20.5488 10.8457L21.9467 14.5766C21.9782 14.6607 21.9497 14.7555 21.8771 14.8084L12.1177 21.9143C12.0476 21.9654 11.9524 21.9654 11.8823 21.9143L2.1229 14.8084C2.05027 14.7555 2.02182 14.6607 2.05334 14.5766L3.45095 10.8462L3.61783 10.3963L6.56249 2.53587C6.62889 2.35863 6.88149 2.36422 6.93999 2.54422L9.40952 10.1431C9.4363 10.2255 9.51309 10.2813 9.59973 10.2813H14.4002C14.4868 10.2813 14.5637 10.2255 14.5904 10.143L17.0573 2.54429Z"
stroke="#000000"
stroke-width="1.5"
></path>
</svg>
<span>neuroevolutus</span>
</a>
</p>
<p>
<a href="https://stackoverflow.com/users/8972496/neuroevolutus">
<?xml version="1.0" encoding="UTF-8"?><svg
width="24px"
height="24px"
viewBox="0 0 24 24"
stroke-width="1.5"
fill="none"
xmlns="http://www.w3.org/2000/svg"
color="#000000"
>
<path
d="M19 15V21H5V15"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M16 17L8 17"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M15.9126 14.6633L8.0874 13"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M16.7127 12.3809L9.46228 9"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M18.1728 10.6423L12.0444 5.5"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
<path
d="M20.0338 8.80409L15.1085 2.5"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
></path>
</svg>
<span>neuroevolutus</span>
</a>
</p>
<p>
<a href="https://twitter.com/neuroevolutus">
<?xml version="1.0" encoding="UTF-8"?><svg
width="24px"
height="24px"
viewBox="0 0 24 24"
stroke-width="1.5"
fill="none"
xmlns="http://www.w3.org/2000/svg"
color="#000000"
>
<path
d="M16.8198 20.7684L3.75317 3.96836C3.44664 3.57425 3.72749 3 4.22678 3H6.70655C6.8917 3 7.06649 3.08548 7.18016 3.23164L20.2468 20.0316C20.5534 20.4258 20.2725 21 19.7732 21H17.2935C17.1083 21 16.9335 20.9145 16.8198 20.7684Z"
stroke="#000000"
stroke-width="1.5"
></path>
<path
d="M20 3L4 21"
stroke="#000000"
stroke-width="1.5"
stroke-linecap="round"
></path>
</svg>
<span>neuroevolutus</span>
</a>
</p>
</section>
<section>
<h2 class="slide-header">Agenda</h2>
<ul>
<li>C++ origins and ethos</li>
<li>
The evolution of FP in C++ from C++98 through C++23
<ul>
<li>Understanding the benefits of each FP feature</li>
<li>Comparisons with Haskell and its functional features</li>
</ul>
</li>
<li>FP in C++26 and beyond</li>
<li>Some musings about performance</li>
</ul>
</section>
<section>
<section>
<h2>What is C++?</h2>
</section>
<section>
<ul>
<li>
Inspired by the powerful type-checking and abstractions provided
by Simula
<ul>
<li>
Dr. Bjarne Stroustroup used Simula for his Ph.D. thesis to
implement a simulator for distributed systems
</li>
</ul>
</li>
<li>
Influenced by the difficulty of rewriting the simulator in BCPL
</li>
</ul>
</section>
<section>
<ul>
<li>Dr. Stroustroup went on to work at Bell Labs</li>
<li>
Found himself writing a distributed system again
<ul>
<li>
Could no longer repeat the Herculean effort of writing a
complex system in BCPL
</li>
</ul>
</li>
<li>Early C++, i.e. C with Classes, was born</li>
</ul>
</section>
<section>
So what can we say about C++ as a language?
</section>
<section>
<ul>
<li>
A systems language built to solve
<em>real-world</em> programming problems
</li>
<li>
Offers lightweight to zero-cost abstractions for effectively
modelling <em>concepts</em> from any problem domain
</li>
<li>
Can <em>scale</em> to handle the complexity of the problems you
face
</li>
</ul>
</section>
</section>
<section>
<section>
<h2>C++98</h2>
</section>
<section data-auto-animate>
<h3 class="slide-header">Core Functional Features</h3>
<ul>
<li>Functions as first-class citizens</li>
<li>Generic programming</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Core Functional Features</h3>
<ul>
<li>Function pointers</li>
<li>Function references</li>
<li>Pointers to members</li>
<li>Function objects</li>
<li>Templates</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Core Functional Features</h3>
<ul>
<li class="emphasised">Function pointers</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Functions as first-class citizens</h3>
<ul>
<li>Pass functions to functions</li>
<li>Return functions from functions</li>
<li>Bind functions to names</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Function pointers</h3>
<ul>
<li>A feature shared with C</li>
<li>Make functions an (arguably) first-class feature</li>
</ul>
</section>
<section>
Key benefit: refactoring high-level algorithms out of concrete implementations
</section>
<section>
<p>A real-world motivating example</p>
</section>
<section>
<p>So you need to sort a list...</p>
</section>
<section data-auto-animate>
<ul>
<li>Need to do it two ways</li>
</ul>
</section>
<section data-auto-animate>
<ul>
<li>Need to do it two ways</li>
<li>Prefer for it to be maintainable</li>
</ul>
</section>
<section data-auto-animate>
<ul>
<li>Need to do it two ways</li>
<li>Prefer for it to be maintainable</li>
<li>What do you do? 🤔</li>
</ul>
</section>
<section>
<p>Our initial attempt</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="63|64-74|75|76-77|57-59|41-55|35-39|30-33|14-28|8-12" class="language-cpp"><script type="text/template">
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
int lomuto_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
void lomuto_quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
) {
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = lomuto_partition(
v,
first_element_index,
last_element_index
);
lomuto_quicksort_impl(v, first_element_index, pivot_index - 1);
lomuto_quicksort_impl(v, pivot_index + 1, last_element_index);
}
void lomuto_quicksort(std::vector<int>& v)
{
lomuto_quicksort_impl(v, 0, static_cast<int>(v.size()) - 1);
}
int hoare_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
void hoare_quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
) {
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = hoare_partition(
v,
first_element_index,
last_element_index
);
hoare_quicksort_impl(v, 0, pivot_index - 1);
hoare_quicksort_impl(v, pivot_index + 1, last_element_index);
}
void hoare_quicksort(std::vector<int>& v) {
hoare_quicksort_impl(v, 0, static_cast<int>(v.size()) - 1);
}
int main()
{
std::vector<int> v1;
v1.reserve(10);
v1.push_back(3);
v1.push_back(2);
v1.push_back(0);
v1.push_back(1);
v1.push_back(8);
v1.push_back(9);
v1.push_back(7);
v1.push_back(5);
v1.push_back(6);
v1.push_back(4);
std::vector<int> v2(v1);
lomuto_quicksort(v1);
hoare_quicksort(v2);
for (std::vector<int>::size_type i = 0; i < v1.size(); ++i) {
std::cout << v1[i] << ' ';
}
std::cout << '\n';
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/zGqae4znb">https://godbolt.org/z/zGqae4znb</a>
</figcaption>
</figure>
</section>
<section>
<h3 class="slide-header">Same driver skeleton</h3>
<figure class="code-example">
<pre><code data-trim class="language-cpp"><script type="text/template">
void variant_quicksort(std::vector<int>& v) {
variant_quicksort_impl(v, 0, static_cast<int>(v.size()) - 1);
}
</script></code></pre>
</figure>
</section>
<section>
<h3 class="slide-header">Same algorithm skeleton</h3>
<figure class="code-example">
<pre><code data-trim data-line-numbers="1-15|8-12|13|14" class="language-cpp"><script type="text/template">
void variant_quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
) {
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = variant_partition(
v,
first_element_index,
last_element_index
);
variant_quicksort_impl(v, first_element_index, pivot_index - 1);
variant_quicksort_impl(v, pivot_index + 1, last_element_index);
}
</script></code></pre>
</figure>
</section>
<section><p>Only differing partition strategies!</p></section>
<section>
<p>💡 Extract out the function as a parameter</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="63-64|36-46|38|21-34|25|8-19" class="language-cpp"><script type="text/template">
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
int lomuto_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
int hoare_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
void quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index,
int (*const partition)(std::vector<int>&, int, int)
) {
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = partition(
v, first_element_index, last_element_index
);
quicksort_impl(v, 0, pivot_index - 1, partition);
quicksort_impl(v, pivot_index + 1, last_element_index, partition);
}
void quicksort(
std::vector<int>& v,
int (*const partition)(std::vector<int>&, int, int)
) {
quicksort_impl(
v,
0,
static_cast<int>(v.size()) - 1,
partition
);
}
int main()
{
std::vector<int> v1;
v1.reserve(10);
v1.push_back(3);
v1.push_back(2);
v1.push_back(0);
v1.push_back(1);
v1.push_back(8);
v1.push_back(9);
v1.push_back(7);
v1.push_back(5);
v1.push_back(6);
v1.push_back(4);
std::vector<int> v2(v1);
quicksort(v1, &lomuto_partition);
quicksort(v2, &hoare_partition);
for (std::vector<int>::size_type i = 0; i < v1.size(); ++i) {
std::cout << v1[i] << ' ';
}
std::cout << '\n';
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/W7caE7bT1">https://godbolt.org/z/W7caE7bT1</a>
</figcaption>
</figure>
</section>
<section>
<h3 class="slide-header">Benefits</h3>
<ul>
<li>
Sorting algorithm separated from concrete partition strategy
</li>
<li>
Code can be refactored or extended with new strategies without
unnecessary code bloat
</li>
</ul>
</section>
<section>
<h3 class="slide-header">Cons</h3>
<ul>
<li>Parameter drilling of function pointer</li>
<li>Runtime penalty of function pointer dereference</li>
</ul>
</section>
<section>
<h3 class="slide-header">Core Functional Features</h3>
<ul>
<li class="de-emphasised">Function pointers</li>
<li class="emphasised">Function references</li>
<li class="de-emphasised">Pointers to members</li>
<li class="de-emphasised">Function objects</li>
<li class="de-emphasised">Templates</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Function references</h3>
</section>
<section data-auto-animate>
<h3 class="slide-header">Function references</h3>
<ul>
<li>Similar to function pointers</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Function references</h3>
<ul>
<li>Similar to function pointers</li>
<li>But cannot be reseated</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">Function references</h3>
<ul>
<li>Similar to function pointers</li>
<li>But cannot be reseated</li>
<li>And can never be null</li>
</ul>
</section>
<section>
<p>Revisiting how we sort</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="62-63|37|24" class="language-cpp"><script type="text/template">
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
int lomuto_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
int hoare_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
void quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index,
int (&partition)(std::vector<int>&, int, int)
) {
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = partition(
v, first_element_index, last_element_index
);
quicksort_impl(v, 0, pivot_index - 1, partition);
quicksort_impl(v, pivot_index + 1, last_element_index, partition);
}
void quicksort(
std::vector<int>& v,
int (&partition)(std::vector<int>&, int, int)
) {
quicksort_impl(
v,
0,
static_cast<int>(v.size()) - 1,
partition
);
}
int main()
{
std::vector<int> v1;
v1.reserve(10);
v1.push_back(3);
v1.push_back(2);
v1.push_back(0);
v1.push_back(1);
v1.push_back(8);
v1.push_back(9);
v1.push_back(7);
v1.push_back(5);
v1.push_back(6);
v1.push_back(4);
std::vector<int> v2(v1);
quicksort(v1, lomuto_partition);
quicksort(v2, hoare_partition);
for (std::vector<int>::size_type i = 0; i < v1.size(); ++i) {
std::cout << v1[i] << ' ';
}
std::cout << '\n';
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/b5Gc7Yc4c">https://godbolt.org/z/b5Gc7Yc4c</a>
</figcaption>
</figure>
</section>
<section>
<h3 class="slide-header">Core Functional Features</h3>
<ul>
<li class="de-emphasised">Function pointers</li>
<li class="de-emphasised">Function references</li>
<li class="de-emphasised">Pointers to members</li>
<li class="emphasised">Function objects</li>
<li class="de-emphasised">Templates</li>
</ul>
</section>
<section data-auto-animate>
<h3>The <span class="code"><functional></span> header</h3>
</section>
<section data-auto-animate>
<h3 class="slide-header">The <span class="code"><functional></span> header</h3>
<ul>
<li>Defines a set of function objects that compose well with <em>algorithm templates</em>
<ul>
<li>A function object: any object for which <span class="code">operator()</span> is defined</li>
</ul>
</li>
<li>Introduces <em>closures</em> and <em>partial application</em> to the world of C++</li>
</ul>
</section>
<section>
<p>Doing another refactor</p>
</section>
<section>
<p>Key principle: allow user to pick the type of customization as before</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="24-30|24|27|29|8-22|8|13|17-21"><script type="text/template">
#ifndef QUICKSORT_HPP_INCLUDED
#define QUICKSORT_HPP_INCLUDED
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
template <typename PartitionStrategy>
void quicksort_impl(
std::vector<int>& v,
int const first_element_index,
int const last_element_index,
PartitionStrategy partition
{
if (first_element_index >= last_element_index
|| first_element_index < 0) return;
int const pivot_index = partition(
v, first_element_index, last_element_index
);
quicksort_impl(v, 0, pivot_index - 1, partition);
quicksort_impl(v, pivot_index + 1, last_element_index, partition);
}
template <typename PartitionStrategy>
void quicksort(
std::vector<int>& v,
PartitionStrategy partition
) {
quicksort_impl(v, 0, static_cast<int>(v.size()) - 1, partition);
}
#endif
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/6e4PbYzda">https://godbolt.org/z/6e4PbYzda</a>
</figcaption>
</figure>
</section>
<section>
<p>...and the client code can stay the same, too</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="37-38"><script type="text/template">
#include "quicksort.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
int lomuto_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
int hoare_partition(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
); // Definition elided
int main()
{
std::vector<int> v1;
v1.reserve(10);
v1.push_back(3);
v1.push_back(2);
v1.push_back(0);
v1.push_back(1);
v1.push_back(8);
v1.push_back(9);
v1.push_back(7);
v1.push_back(5);
v1.push_back(6);
v1.push_back(4);
std::vector<int> v2(v1);
quicksort(v1, lomuto_partition);
quicksort(v2, hoare_partition);
for (std::vector<int>::size_type i = 0; i < v1.size(); ++i) {
std::cout << v1[i] << ' ';
}
std::cout << '\n';
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/6e4PbYzda">https://godbolt.org/z/6e4PbYzda</a>
</figcaption>
</figure>
</section>
<section>
Now, we can pass function objects to <span class="code">quicksort</span> to specify the partition strategy.
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="10-16|18-24|41-42"><script type="text/template">
#include "quicksort.hpp"
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
// Referenced from: https://en.wikipedia.org/wiki/Quicksort
struct LomutoPartition {
int operator()(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
) const; // Definition elided
};
struct HoarePartition{
int operator()(
std::vector<int>& v,
int const first_element_index,
int const last_element_index
) const; // Definition elided
};
int main()
{
std::vector<int> v1;
v1.reserve(10);
v1.push_back(3);
v1.push_back(2);
v1.push_back(0);
v1.push_back(1);
v1.push_back(8);
v1.push_back(9);
v1.push_back(7);
v1.push_back(5);
v1.push_back(6);
v1.push_back(4);
std::vector<int> v2(v1);
quicksort(v1, LomutoPartition());
quicksort(v2, HoarePartition());
for (std::vector<int>::size_type i = 0; i < v1.size(); ++i) {
std::cout << v1[i] << ' ';
}
std::cout << '\n';
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/nq1654bMG">https://godbolt.org/z/nq1654bMG</a>
</figcaption>
</figure>
</section>
<section>
<ul>
<li>Benefit: potentially better inlining due to using templates and stateless objects</li>
<li>Con: potentially more binary bloat because of template instantiation</li>
</ul>
</section>
<section>
<p>
Function objects allow us to emulate closures by capturing values
or variables upon construction.
</p>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="6-15|18-21|22-23|24-26|25"><script type="text/template">
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
class AddNumber {
int number;
public:
AddNumber(int number) {
this->number = number;
}
int operator()(int other_number) const {
return number + other_number;
}
};
int main() {
std::vector<int> v1, v2;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
// Pretend x is a user-supplied int
int const x = 5;
std::transform(
v1.begin(), v1.end(), std::back_inserter(v2), AddNumber(x)
);
for (std::vector<int>::size_type i = 0; i < v2.size(); ++i) {
std::cout << v2[i] << ' ';
}
std::cout << '\n';
return EXIT_SUCCESS;
}
</script></code></pre>
<figcaption>
<a href="https://godbolt.org/z/7zznGc9vY">https://godbolt.org/z/7zznGc9vY</a>
</figcaption>
</figure>
</section>
<section data-auto-animate>
<h3>A slight problem</h3>
</section>
<section data-auto-animate>
<h3 class="slide-header">A slight problem</h3>
<ul>
<li>
Already have a function object that expresses our desired
computation
</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">A slight problem</h3>
<ul>
<li>
Already have a function object that expresses our desired
computation
</li>
<li>But its signature is a bit too general</li>
</ul>
</section>
<section data-auto-animate>
<h3 class="slide-header">A slight problem</h3>
<ul>
<li>
Already have a function object that expresses our desired
computation
</li>
<li>But its signature is a bit too general</li>
<li>Can we avoid reinventing the wheel?</li>
</ul>
</section>
<section>
<figure class="code-example">
<pre><code data-trim data-line-numbers="12-15|14"><script type="text/template">
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
std::vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
// Printing out the sum
std::cout << std::accumulate(
v1.begin(), v1.end(), 0, std::plus<int>()
) << '\n';
return EXIT_SUCCESS;
}