This repository has been archived by the owner on May 24, 2022. It is now read-only.
forked from naumen-student/naumen.scala.course.2021.spring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scala_lecture_7.html
794 lines (737 loc) · 85.9 KB
/
scala_lecture_7.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
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<title>Введение в функциональное программирование</title>
<meta name="description" content="Название">
<meta name="author" content="автор">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/white.css" id="theme">
<link rel="stylesheet" href="css/hljs/vs.css" id="highlight-theme">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
<div class="reveal">
<div class="footer">
<span class="corp-name"><b>NAUMEN </b></span>Яценко Иван
</div>
<div class="slides">
<section>
<h1>Введение в функциональное программирование</h1>
<div style="position: relative; right: 0;">Яценко Иван</div>
</section>
</div>
<div class="slides">
<section>
<h1>Императивное программирование</h1>
<ul>
<li>в исходном коде программы записываются инструкции (команды);</li>
<li>инструкции должны выполняться последовательно;</li>
<li>данные, получаемые при выполнении предыдущих инструкций, могут читаться из памяти последующими инструкциями;</li>
<li>данные, полученные при выполнении инструкции, могут записываться в память.</li>
</ul>
</section>
</div>
<div class="slides">
<section>
<b>Функция в программировании</b> — фрагмент программного кода (подпрограмма), к которому можно обратиться из другого места программы.
</section>
</div>
<div class="slides">
<section>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">scala.collection.mutable</span>
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Sort</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">def</span> bubble<span style="color: #333333">(</span>input<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">mutable.ListBuffer</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Unit</span> <span style="color: #333333">=</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>input<span style="color: #333333">.</span>isEmpty<span style="color: #333333">)</span> <span style="color: #333333">()</span>
<span style="color: #008800; font-weight: bold">else</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span>i <span style="color: #008800; font-weight: bold"><-</span> input<span style="color: #333333">.</span>indices<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span>j <span style="color: #008800; font-weight: bold"><-</span> input<span style="color: #333333">.</span>indices<span style="color: #333333">.</span>drop<span style="color: #333333">(</span>i<span style="color: #333333">).</span>reverse<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">val</span> a <span style="color: #008800; font-weight: bold">=</span> input<span style="color: #333333">(</span>i<span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold">val</span> b <span style="color: #008800; font-weight: bold">=</span> input<span style="color: #333333">(</span>j<span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>a <span style="color: #333333">></span> b<span style="color: #333333">)</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">val</span> buffer <span style="color: #008800; font-weight: bold">=</span> a
input<span style="color: #333333">(</span>i<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span> input<span style="color: #333333">(</span>j<span style="color: #333333">)</span>
input<span style="color: #333333">(</span>j<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span> buffer
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">val</span> arr <span style="color: #008800; font-weight: bold">=</span> mutable<span style="color: #333333">.</span><span style="color: #BB0066; font-weight: bold">ListBuffer</span><span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">3</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">6</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">4</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">6</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">)</span> <span style="color: #888888">// ListBuffer(3, 2, 6, 4, 6, 1)</span>
<span style="color: #BB0066; font-weight: bold">Sort</span><span style="color: #333333">.</span>bubble<span style="color: #333333">(</span>arr<span style="color: #333333">)</span>
arr <span style="color: #888888">// ListBuffer(1, 2, 3, 4, 6, 6)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<img src="img/lecture_9/bubble_sort.png" />
</section>
</div>
<div class="slides">
<section>
<br><br>
<b>Функция в математике</b> — соответствие между элементами двух множеств, <br>
установленное по такому правилу, что каждому элементу одного множества соответствует<br>
один и только один элемент из другого множества.
</section>
</div>
<div class="slides">
<section>
<img src="img/lecture_9/function.png" />
</section>
</div>
<div class="slides">
<section>
<img src="img/lecture_9/function2.png" />
</section>
</div>
<div class="slides">
<section>
<b>Функциональное программирование</b> - парадигама программирования, <br>
в которой процесс вычислени трактуется,<br>
как вычисление функции от входных параметров.
</section>
</div>
<div class="slides">
<section>
<h1>Основные концепции:</h1>
<ul>
<li>чистота функций;</li>
<li>иммутабельность данных;</li>
<li>ссылочная прозрачность;</li>
<li>код - композиция функций;</li>
<li>функция - объект первого класса;</li>
<li>функции высших порядков;</li>
<li>сопоставление образцом.</li>
</ul>
</section>
</div>
<div class="slides">
<section>
<h1>Чистые функции</h1>
Функция называется <b>чистой</b>, когда она детерминированна и не несёт в себе побочных эффектов.
<img src="img/lecture_9/pure_function.png" />
</section>
</div>
<div class="slides">
<section>
<h1>Чистые функции</h1>
<h2>Детерминированность</h2>
Функция является детерминированной, если для одного и того же набора входных значений она возвращает одинаковый результат.
</section>
</div>
<div class="slides">
<section>
<h1>Чистые функции</h1>
<h2>Детерминированность</h2>
Недетерминированная функция
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Math</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">var</span> pi <span style="color: #008800; font-weight: bold">=</span> <span style="color: #6600EE; font-weight: bold">3.14d</span>
<span style="color: #008800; font-weight: bold">def</span> circleArea<span style="color: #333333">(</span>radius<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Double</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span>
radius <span style="color: #333333">*</span> radius <span style="color: #333333">*</span> pi
<span style="color: #333333">}</span>
<span class="fragment"><span style="color: #BB0066; font-weight: bold">Math</span><span style="color: #333333">.</span>circleArea<span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">10</span><span style="color: #333333">)</span> <span style="color: #888888">// 314.0</span></span>
<span class="fragment"><span style="color: #BB0066; font-weight: bold">Math</span><span style="color: #333333">.pi <span style="color: #008800; font-weight: bold">=</span> <span style="color: #6600EE; font-weight: bold">3.1415926d</span></span>
<span class="fragment"><span style="color: #BB0066; font-weight: bold">Math</span><span style="color: #333333">.</span>circleArea<span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">10</span><span style="color: #333333">)</span> <span style="color: #888888">// 314.15926</span></span>
</pre>
</div>
<div class="fragment">
Детерминированная функция
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Math</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">def</span> circleArea<span style="color: #333333">(</span>radius<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Double</span><span style="color: #333333">, </span>pi<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Double</span><span style="color: #333333"></span>)</span> <span style="color: #008800; font-weight: bold">=</span>
radius <span style="color: #333333">*</span> radius <span style="color: #333333">*</span> pi
<span style="color: #333333">}</span>
<span style="color: #BB0066; font-weight: bold">Math</span><span style="color: #333333">.</span>circleArea<span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">10</span><span style="color: #333333">, <span style="color: #0000DD; font-weight: bold">3.14</span><span style="color: #333333">)</span> <span style="color: #888888">// 314.00</span>
</pre>
</div>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Чистые функции</h1>
<h2>Побочные эффекты</h2>
IO-операции, передача управления, модификация данных и т.д.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Math</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">private var</span> pi <span style="color: #008800; font-weight: bold">=</span> <span style="color: #6600EE; font-weight: bold">3.14d</span>
<span style="color: #008800; font-weight: bold">def</span> setPI<span style="color: #333333">(</span>newPi<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Double</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span>
pi <span style="color: #333333">=</span> newPi
<span style="color: #008800; font-weight: bold">def</span> getPI<span style="color: #333333">()</span> <span style="color: #008800; font-weight: bold">=</span> pi
<span style="color: #008800; font-weight: bold">def</span> circleArea<span style="color: #333333">(</span>radius<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Double</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span>
radius <span style="color: #333333">*</span> radius <span style="color: #333333">*</span> getPi()
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Иммутабельность данных</h1>
Данные неизменны.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">var</span> sumOfValues <span style="color: #008800; font-weight: bold">=</span> <span style="color: #0000DD; font-weight: bold">0</span>
<span style="color: #008800; font-weight: bold">def</span> findSum<span style="color: #333333">(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Seq</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Unit</span> <span style="color: #333333">=</span>
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span>item <span style="color: #008800; font-weight: bold"><-</span> list<span style="color: #333333">)</span> <span style="color: #333333">{</span>
sumOfValues <span style="color: #008800; font-weight: bold">=</span> sumOfValues <span style="color: #333333">+</span> item
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> findSumImmutable<span style="color: #333333">(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Seq</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">],</span> sumOfValues<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=</span> <span style="color: #0000DD; font-weight: bold">0</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=</span>
<span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>list<span style="color: #333333">.</span>isEmpty<span style="color: #333333">)</span> sumOfValue
<span style="color: #008800; font-weight: bold">else</span> findSumImmutable<span style="color: #333333">(</span>list<span style="color: #333333">.</span>tail<span style="color: #333333">,</span> sumOfValues <span style="color: #333333">+</span> list<span style="color: #333333">.</span>head<span style="color: #333333">)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Ссылочная прозрачность</h1>
Выражение называется ссылочно прозрачным, если его можно заменить соответствующим значением без изменения поведения программы.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">val</span> values <span style="color: #008800; font-weight: bold">=</span> <span style="color: #BB0066; font-weight: bold">Seq</span><span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">)</span>
findSumImmutable<span style="color: #333333">(</span>values<span style="color: #333333">)</span> <span style="color: #888888">// 3</span>
findSumImmutable<span style="color: #333333">(</span><span style="color: #BB0066; font-weight: bold">Seq</span><span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">1</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">))</span> <span style="color: #888888">// 3</span>
<span style="color: #0000DD; font-weight: bold">3</span>
</pre></div>
</section>
</div>
<div class="slides">
<section>
<h1>Функции - объекты первого класса</h1>
<ul>
<li>функции можно хранить в переменных/константах;</li>
<li>функции можно передавать в качестве агрументов;</li>
<li>функция может быть результатом вычисления.</li>
</ul>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Math</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">def</span> sqr<span style="color: #333333">(</span>n<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=</span> n <span style="color: #333333">*</span> n
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">ListFunctor</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">def</span> map<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #008800; font-weight: bold">_</span><span style="color: #333333">.</span>map<span style="color: #333333">(</span>f<span style="color: #333333">)</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">val</span> sqr<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=></span> <span style="color: #BB0066; font-weight: bold">Int</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #BB0066; font-weight: bold">Math</span><span style="color: #333333">.</span>sqr _
<span style="color: #008800; font-weight: bold">val</span> sqrList<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #BB0066; font-weight: bold">ListFunctor</span><span style="color: #333333">.</span>map<span style="color: #333333">(</span>sqr<span style="color: #333333">)</span>
sqrList<span style="color: #333333">(</span><span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">(</span><span style="color: #0000DD; font-weight: bold">2</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">3</span><span style="color: #333333">,</span> <span style="color: #0000DD; font-weight: bold">4</span><span style="color: #333333">))</span> <span style="color: #888888">// List(4, 9, 16)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Функции - объекты первого класса</h1>
Любая функция - экземпляр класса FunctionN[...]
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">abstract</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">Function1</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">def</span> apply<span style="color: #333333">(</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">B</span>
<span style="color: #333333">}</span>
<span style="color: #333333">(</span>A <span style="color: #008800; font-weight: bold">=></span> B<span style="color: #333333">)</span> <span style="color: #333333">~</span> <span style="color: #BB0066; font-weight: bold">Function1</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Лямбда-функции</h1>
- выражение, которое состоит исключительно из следующих составляющих:
<ul>
<li>переменные - включений имён "переменных" (f, x);</li>
<li>аппликации - применения терм друг к другу (f(x));</li>
<li>абстракции - реализация лямда-функций (λx.f(x)).</li>
</ul>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">val</span> product<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">,</span> <span style="color: #333399; font-weight: bold">Int</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">Int</span> <span style="color: #008800; font-weight: bold">=</span> tuple <span style="color: #008800; font-weight: bold">=></span> tuple<span style="color: #333333">.</span>_1 <span style="color: #333333">*</span> tuple<span style="color: #333333">.</span>_2
<span style="color: #008800; font-weight: bold">val</span> sqr<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=></span> <span style="color: #BB0066; font-weight: bold">Int</span> <span style="color: #008800; font-weight: bold">=</span> x <span style="color: #008800; font-weight: bold">=></span> product (x, x)
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Композиция функций</h1>
∀ f: A → B, g: B → C<br>
∃ c: A → C = g ∘ f<br>
Т.е. для <i>f(a): b</i> и <i>g(b): c</i> есть <i>c(a) = g(f(a))</i>
<br>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> decode<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Array</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Byte</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">String</span>
<span style="color: #008800; font-weight: bold">def</span> parseString<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">](</span>parser<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Parser</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=></span> T
<span style="color: #008800; font-weight: bold">def</span> parseBytes<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">](</span>parser<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Parse</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Array</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Byte</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=></span> T <span style="color: #008800; font-weight: bold">=</span>
decode andThen parseString<span style="color: #333333">(</span>parser<span style="color: #333333">)</span> <span style="color: #888888">// ~ bytes => parseString(parser)(decode(bytes))</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Каррирование</h1>
Преобразование функции нескольких аргументов в функцию одного аргумента, которая возвращает функцию от следующего аргумента, которая возвращает функцию от...<br>
curr: ((A × B) → (C)) → (A → (B → C))
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #888888">//curr - операция каррирования</span>
foo<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">,</span> <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">,</span> C<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span> D
curr<span style="color: #333333">(</span>foo<span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> <span style="color: #333333">((</span>B<span style="color: #333333">,</span> C<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span> D<span style="color: #333333">)</span>
curr<span style="color: #333333">(</span>curr<span style="color: #333333">(</span>foo<span style="color: #333333">))</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> <span style="color: #333333">(</span>B <span style="color: #008800; font-weight: bold">=></span> <span style="color: #333333">(</span>C <span style="color: #008800; font-weight: bold">=></span> D<span style="color: #333333">))</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Каррирование</h1>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> log<span style="color: #333333">(</span>time<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">DateTime</span><span style="color: #333333">,</span> level<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Level</span><span style="color: #333333">,</span> msg<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span>
<span style="color: #008800; font-weight: bold">def</span> curriedLog<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">DateTime</span> <span style="color: #333333">=></span> <span style="color: #BB0066; font-weight: bold">Level</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">String</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">String</span> <span style="color: #008800; font-weight: bold">=</span>
time<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">DateTime</span> <span style="color: #333333">=></span>
level<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Level</span> <span style="color: #333333">=></span>
msg<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=></span> log<span style="color: #333333">(</span>time<span style="color: #333333">,</span> level<span style="color: #333333">,</span> msg<span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold">val</span> fixedTimeLogger<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Level</span> <span style="color: #333333">=></span> <span style="color: #BB0066; font-weight: bold">String</span> <span style="color: #008800; font-weight: bold">=></span> <span style="color: #BB0066; font-weight: bold">String</span> <span style="color: #008800; font-weight: bold">=</span> curriedLog<span style="color: #333333">(</span><span style="color: #BB0066; font-weight: bold">DateTime</span><span style="color: #333333">.</span>now<span style="color: #333333">())</span>
<span style="color: #008800; font-weight: bold">val</span> fixedTimeErrorLogger<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=></span> <span style="color: #BB0066; font-weight: bold">String</span> <span style="color: #008800; font-weight: bold">=</span>
fixedTimeLogger<span style="color: #333333">(</span><span style="color: #BB0066; font-weight: bold">Level</span><span style="color: #333333">.</span>error<span style="color: #333333">)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Каррирование</h1>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> log<span style="color: #333333">(</span>time<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">DateTime</span><span style="color: #333333">,</span> level<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Level</span><span style="color: #333333">,</span> msg<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span>
<span style="color: #008800; font-weight: bold">def</span> curriedLog<span style="color: #333333">(</span>time<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">DateTime</span><span style="color: #333333">)</span>
<span style="color: #333333">(</span>level<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Level</span><span style="color: #333333">)</span>
<span style="color: #333333">(</span>msg<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=</span> log<span style="color: #333333">(</span>time<span style="color: #333333">,</span> level<span style="color: #333333">,</span> msg<span style="color: #333333">)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Функции высших порядков</h1>
Функция высшего порядка - функция, которая принимает в качестве параметров(-а) или/и возвращает функцию.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> map<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">,</span> list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span>
<span style="color: #008800; font-weight: bold">def</span> compose<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span>, <span style="color: #333399; font-weight: bold">C</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">,</span> g<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">B</span> <span style="color: #333333">=></span> C<span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> C <span style="color: #008800; font-weight: bold">=</span> <span style="color: #333333">(</span>x<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span> g<span style="color: #333333">(</span>f<span style="color: #333333">(</span>x<span style="color: #333333">))</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Рекурсивные функции</h1>
<img src="img/lecture_9/recursion.jpg" />
</section>
</div>
<div class="slides">
<section>
<h1>Рекурсивные функции</h1>
Функция называыется <b>рекурсивной</b>, если она вызывает сама себя.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> map<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span>
<span style="color: #008800; font-weight: bold"> if</span><span style="color: #333333">(</span>list<span style="color: #333333">.</span>nonEmpty<span style="color: #333333">)</span> f<span style="color: #333333">(</span>list<span style="color: #333333">.</span>head<span style="color: #333333">)</span> <span style="color: #333333">+:</span> map<span style="color: #333333">(</span>f<span style="color: #333333">)(</span>list<span style="color: #333333">.</span>tail<span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold"> else</span> <span style="color: #BB0066; font-weight: bold">Nil</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
Стек вызовов для списка из двух элементов<br>
<p><code>
at map.appy<br>
at map.appy<br>
at map.appy<br>
at map.appy<br>
at map.appy<br>
</code></p>
</section>
</div>
<div class="slides">
<section>
<h1>Хвостовая рекурсия</h1>
<b>Хвостовая рекурсия</b> - рекурсия, в которой рекурсивный вызов является последним.<br>Т.е. результат рекурсивного вызова - результат вычисления функции.
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> iterativeMap<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">import</span> <span style="color: #0e84b5; font-weight: bold">scala.collection.mutable</span>
<span style="color: #008800; font-weight: bold">val</span> buffer <span style="color: #008800; font-weight: bold">=</span> muttable<span style="color: #333333">.</span><span style="color: #BB0066; font-weight: bold">ListBuffer</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> muttable<span style="color: #333333">.</span><span style="color: #BB0066; font-weight: bold">ListBuffer</span><span style="color: #333333">.</span>empty
<span style="color: #008800; font-weight: bold">for</span><span style="color: #333333">(</span>item <span style="color: #008800; font-weight: bold"><-</span> list<span style="color: #333333">)</span> <span style="color: #333333">{</span>
buffer <span style="color: #333333">+=</span> f<span style="color: #333333">(</span>item<span style="color: #333333">)</span>
<span style="color: #333333">}</span>
buffer
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">def</span> tailMap<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #333333">{</span>
<span style="color: #555555; font-weight: bold">@tailrec</span>
<span style="color: #008800; font-weight: bold">def</span> loop<span style="color: #333333">(</span>l<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">],</span> buffer<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>l<span style="color: #333333">.</span>isEmpty<span style="color: #333333">)</span> <span style="color: #333333">{</span>
buffer
<span style="color: #333333">}</span> <span style="color: #008800; font-weight: bold">else</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">val</span> head <span style="color: #008800; font-weight: bold">=</span> l<span style="color: #333333">.</span>head
<span style="color: #008800; font-weight: bold">val</span> tail <span style="color: #008800; font-weight: bold">=</span> l<span style="color: #333333">.</span>tail
loop<span style="color: #333333">(</span>tail<span style="color: #333333">,</span> buffer <span style="color: #333333">:+</span> f<span style="color: #333333">(</span>head<span style="color: #333333">))</span>
<span style="color: #333333">}</span>
loop<span style="color: #333333">(</span>list<span style="color: #333333">,</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">.</span>empty)
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
Стек вызовов для списка из двух элементов<br>
<p><code>
at map.loop<br>
at map.appy<br>
</code></p>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом<br>(Pattern matching)</h1>
<img style="height: auto;" src="img/lecture_9/matching.jpg" />
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<table>
<tr>
<th>Возможность</th>
<th>switch-case</th>
<th>match-case</th>
</th>
<tr>
<td>сопоставление с вычисляемым значением (литерал, выражение)</td>
<td>+</td>
<td>+</td>
</tr>
<tr>
<td>сопоставление с типом</td>
<td>-</td>
<td>+</td>
</tr>
<tr>
<td>"Распаковка" объектов</td>
<td>-</td>
<td>+</td>
</tr>
<tr>
<td>Сопоставление с xml</td>
<td>-</td>
<td>+</td>
</tr>
<tr>
<td>Завершения проверок после совпадения</td>
<td>break</td>
<td>+</td>
</tr>
<tr><td>и много прочего</td><td>-</td><td>+</td></tr>
</table>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<h2>Bound-pattern</h2>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> foo<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">](</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Unit</span> <span style="color: #333333">=</span> a <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> x <span style="color: #008800; font-weight: bold">=></span> <span style="color: #333333">()</span> <span style="color: #888888">// x is A</span>
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<h2>Type-pattern</h2>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> foo<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">](</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Unit</span> <span style="color: #333333">=</span> a <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> x<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">B</span> <span style="color: #333333">=></span> <span style="color: #333333">()</span> <span style="color: #888888">// x is B if a is B</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">_:</span> <span style="color: #333399; font-weight: bold">C</span> <span style="color: #333333">=></span> <span style="color: #888888">// there are no new names and we know that a is C</span>
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<h2>Extractor-pattern</h2>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> foo<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">](</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Option[A]</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=</span> a <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> Some(x) <span style="color: #333333">=></span> <span style="color: #333333">x</span> <span style="color: #888888">// x is A</span>
<span style="color: #008800; font-weight: bold">case</span> _ <span style="color: #333333">=></span> <span style="color: #333333">A.default
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<h2>@-pattern (pattern binder)</h2>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> foo<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">](</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Option[A]</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=</span> a <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> some@Some(x) <span style="color: #333333">=></span> <span style="color: #333333">x</span> <span style="color: #888888">// x is A, some is Some[A]</span>
<span style="color: #008800; font-weight: bold">case</span> _ <span style="color: #333333">=></span> <span style="color: #333333">A.default
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Сопоставление с образцом</h1>
<h2>Pattern guards</h2>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> foo<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">](</span>a<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Option[A]</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Int</span> <span style="color: #333333">=</span> a <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> Some(x: Int) <span style="color: #008800; font-weight: bold">if</span> x > 0 <span style="color: #333333">=></span> <span style="color: #333333">x</span> <span style="color: #888888">// x is positive Int</span>
<span style="color: #008800; font-weight: bold">case</span> _ <span style="color: #333333">=></span> <span style="color: #333333">1
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> tailMap<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #333333">{</span>
<span style="color: #555555; font-weight: bold">@tailrec</span>
<span style="color: #008800; font-weight: bold">def</span> loop<span style="color: #333333">(</span>l<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">],</span> buffer<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #008800; font-weight: bold">if</span><span style="color: #333333">(</span>l<span style="color: #333333">.</span>isEmpty<span style="color: #333333">)</span> <span style="color: #333333">{</span>
buffer
<span style="color: #333333">}</span> <span style="color: #008800; font-weight: bold">else</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">val</span> head <span style="color: #008800; font-weight: bold">=</span> l<span style="color: #333333">.</span>head
<span style="color: #008800; font-weight: bold">val</span> tail <span style="color: #008800; font-weight: bold">=</span> l<span style="color: #333333">.</span>tail
loop<span style="color: #333333">(</span>tail<span style="color: #333333">,</span> buffer <span style="color: #333333">:+</span> f<span style="color: #333333">(</span>head<span style="color: #333333">))</span>
<span style="color: #333333">}</span>
loop<span style="color: #333333">(</span>list<span style="color: #333333">,</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">.</span>empty)
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">def</span> newTailMap<span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span>, <span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">](</span>f<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">A</span> <span style="color: #333333">=></span> B<span style="color: #333333">)(</span>list<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> <span style="color: #333333">{</span>
<span style="color: #555555; font-weight: bold">@tailrec</span>
<span style="color: #008800; font-weight: bold">def</span> loop<span style="color: #333333">(</span>l<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">A</span><span style="color: #333333">],</span> buffer<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">B</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=</span> l <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> head <span style="color: #333333">::</span> tail <span style="color: #008800; font-weight: bold">=></span> loop<span style="color: #333333">(</span>tail<span style="color: #333333">,</span> buffer <span style="color: #333333">:+</span> f<span style="color: #333333">(</span>head<span style="color: #333333">))</span> <span style="color: #888888">// ::(head, tail)</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">_</span> <span style="color: #008800; font-weight: bold">=></span> buffer
<span style="color: #333333">}</span>
loop<span style="color: #333333">(</span>list<span style="color: #333333">,</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">.</span>empty)
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<div><img src="img/lecture_9/algebraic.gif"></img></div>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>Комбинации типов:</h2>
<ul>
<li>сумма (A | B);</li>
<li>произведение (A & B).</li>
</ul>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>Сумма:</h2>
type Option[T] = None | Some[T]<br>
type List[T] = Nil | ::[T]<br>
type Boolean = True | False<br>
...
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">sealed</span> <span style="color: #008800; font-weight: bold">trait</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">+T</span><span style="color: #333333">]</span>
<span style="color: #BB0066; font-weight: bold">final</span> <span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">::</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">+T</span><span style="color: #333333">](</span>head<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">, tail: <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">]</span>)</span> <span style="color: #008800; font-weight: bold">extends</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">T</span><span style="color: #333333">]</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">Nil</span> <span style="color: #008800; font-weight: bold">extends</span> <span style="color: #BB0066; font-weight: bold">List</span><span style="color: #333333">[</span><span style="color: #333399; font-weight: bold">Nothing</span><span style="color: #333333">]</span>
</pre>
</div>
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">sealed</span> <span style="color: #008800; font-weight: bold">trait</span> <span style="color: #BB0066; font-weight: bold">Boolean</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">True</span> <span style="color: #008800; font-weight: bold">extends</span> <span style="color: #BB0066; font-weight: bold">Boolean</span></span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">object</span> <span style="color: #BB0066; font-weight: bold">False</span> <span style="color: #008800; font-weight: bold">extends</span> <span style="color: #BB0066; font-weight: bold">Boolean</span></span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>Произведение:</h2>
type StandAloneApp = Host & Port<br>
type BasicAppEnvironment = Database & StandAloneApp<br>
BasicAppEnvironment ≈ Database & (Host & Port)<br>
BasicAppEnvironment ≈ Database & Host & Port<br>
...
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">Database</span><span style="color: #333333">(</span>url<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">,</span> username<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">,</span> password<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span><span style="color: #333333">)</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">StandAloneApp</span><span style="color: #333333">(</span>socketConf<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333333">(</span><span style="color: #333399; font-weight: bold">Host</span><span style="color: #333333">,</span> <span style="color: #333399; font-weight: bold">Port</span><span style="color: #333333">))</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">class</span> <span style="color: #BB0066; font-weight: bold">BasicAppEnvironment</span><span style="color: #333333">(</span>appConf<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">StandAloneApp</span><span style="color: #333333">,</span> db<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Database</span><span style="color: #333333">)</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>И Pattern Matching</h2>
Сопоставление с образцом - один из способов извлечения данных из алгебраического типа.
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>И Pattern Matching</h2>
Для произведения типов Extractor-pattern
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> startApp<span style="color: #333333">(</span>
hostAllowed<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Host</span><span style="color: #333333">,</span>
listenPort<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Port</span><span style="color: #333333">,</span>
db<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Database</span><span style="color: #333333">)</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Unit</span>
<span style="color: #008800; font-weight: bold">def</span> run<span style="color: #333333">(</span>env<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">BasicAppEnvironment</span><span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=</span> env <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #BB0066; font-weight: bold">BasicAppEnvironment</span><span style="color: #333333">(</span><span style="color: #BB0066; font-weight: bold">StandAloneApp</span><span style="color: #333333">((</span>host<span style="color: #333333">,</span> port<span style="color: #333333">)),</span> db: Database<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span>
startApp<span style="color: #333333">(</span>host<span style="color: #333333">,</span> port<span style="color: #333333">,</span> db<span style="color: #333333">)</span>
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Algebraic data type</h1>
<h2>И Pattern Matching</h2>
Для суммы типов Type-pattern и/или Extractor-pattern
<div style="background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;">
<pre style="margin: 0; line-height: 125%">
<span style="color: #008800; font-weight: bold">def</span> describeValue<span style="color: #333333">(</span>opt<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Option</span><span style="color: #333333">[</span><span style="color: #008800; font-weight: bold">_</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=</span> opt <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #BB0066; font-weight: bold">Some</span><span style="color: #333333">(</span>value<span style="color: #333333">)</span> <span style="color: #008800; font-weight: bold">=></span> s<span style="background-color: #fff0f0">"There is a \"${value.toString}\""</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #BB0066; font-weight: bold">None</span> <span style="color: #008800; font-weight: bold">=></span> <span style="background-color: #fff0f0">"There is nothing"</span>
<span style="color: #333333">}</span>
<span style="color: #008800; font-weight: bold">def</span> describe<span style="color: #333333">(</span>opt<span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">Option</span><span style="color: #333333">[</span><span style="color: #008800; font-weight: bold">_</span><span style="color: #333333">])</span><span style="color: #008800; font-weight: bold">:</span> <span style="color: #333399; font-weight: bold">String</span> <span style="color: #333333">=</span> opt <span style="color: #008800; font-weight: bold">match</span> <span style="color: #333333">{</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">_:</span> <span style="color: #333399; font-weight: bold">Some</span><span style="color: #333333">[</span><span style="color: #008800; font-weight: bold">_</span><span style="color: #333333">]</span> <span style="color: #008800; font-weight: bold">=></span> <span style="background-color: #fff0f0">"There is something"</span>
<span style="color: #008800; font-weight: bold">case</span> <span style="color: #008800; font-weight: bold">_</span> <span style="color: #008800; font-weight: bold">=></span> <span style="background-color: #fff0f0">"There is nothing"</span>
<span style="color: #333333">}</span>
</pre>
</div>
</section>
</div>
<div class="slides">
<section>
<h1>Что мы получаем:</h1>
<ul>
<li>можно (и нужно) отказаться от переменных;</li>
<li>можно тестировать по принципу черного ящика;</li>
<li>огромное число гарантий;</li>
<li>отказоустойчивость;</li>
<li>оптимизация на этапе компиляции/разработки;</li>
<li>продолжать можно долго...</li>
</ul>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: false,
progress: true,
history: true,
center: true,
slideNumber: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{
src: 'lib/js/classList.js', condition: function () {
return !document.body.classList;
}
},
{
src: 'plugin/markdown/marked.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'plugin/markdown/markdown.js', condition: function () {
return !!document.querySelector('[data-markdown]');
}
},
{
src: 'plugin/highlight/highlight.js', async: true, condition: function () {
return !!document.querySelector('pre code');
}, callback: function () {
hljs.initHighlightingOnLoad();
}
},
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
window.addEventListener("mousedown", handleClick, false);
window.addEventListener("contextmenu", function (e) {
e.preventDefault();
}, false);
function handleClick(e) {
e.preventDefault();
if (e.button === 0) Reveal.next();
if (e.button === 2) Reveal.prev();
}
</script>
</body>
</html>