forked from naumen-student/naumen.scala.course.2020.autumn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscala_lecture_6.html
1306 lines (1073 loc) · 38.4 KB
/
scala_lecture_6.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="ru">
<head>
<meta charset="utf-8">
<title>Scala Collection</title>
<meta name="description" content="Scala. Collection">
<meta name="author" content="Titov Egor">
<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>Scala collection</h2>
</section>
<section>
<h2>Коллекции</h2>
<ul>
<li>списки</li>
<li>множества</li>
<li>массивы</li>
<li>вектора</li>
<li>Option</li>
</ul>
</section>
<section>
<h2>Списки. List</h2>
<ul>
<li>однородны
<ul><li>List[String] не может содержать элементы Int</li></ul>
</li>
<li>ковариантны
<ul><li>если T подтип S, то и List[T] подтип List[S]</li></ul>
</li>
</ul>
</section>
<section>
<h2>List. Создание 1</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val list0 = List()
//list0: List[Nothing] = List()
val list1 = List(1, 2, 3)
//list1: List[Int] = List(1, 2, 3)
val list2 = List.range(1,5)
//list2: List[Int] = List(1, 2, 3, 4)
val list2a = List.range(1, 5, 2)
//list2a: List[Int] = List(1, 3)
val list3 = List.fill(5)("lol")
//list3: List[String] = List("lol", "lol", "lol", "lol", "lol")
val list4 = List.tabulate(5)(n => n/2.0)
//list4: List[Double] = List(0.0, 0.5, 1.0, 1.5, 2.0)
</code></pre>
</section>
<section>
<h2>List. Создание 2</h2>
Nil - пустой список
<pre style="width:auto;"><code data-trim class="scala small">
val empty = Nil
//empty: Nil.type = List()
val list = 3 :: 2 :: 1 :: Nil
//list: List[Int] = List(3, 2, 1)
val d = Nil
//d: Nil.type = List()
val c = 3 :: d
//c: List[Int] = List(3)
val b = 2 :: c
//b: List[Int] = List(2, 3)
val a = 1 :: b
//a: List[Int] = List(1, 2, 3)
</code></pre>
</section>
<section>
<h2>List. Шаблоны</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val List(a, b, c) = List(1, 2, 3)
//a: Int = 1
//b: Int = 2
//c: Int = 3
val x :: y :: z = List(1, 2, 3)
//x: Int = 1
//y: Int = 2
//z: List[Int] = List(3)
</code></pre>
</section>
<section>
<h2>List. Шаблоны</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val List(a, b, c) = List(1, 2, 3, 4)
//scala.MatchError: List(1, 2, 3, 4) (of class
// scala.collection.immutable.$colon$colon)
// ammonite.$sess.cmd33$.<clinit>(cmd33.sc:1)
val x :: y :: z = List(1, 2, 3, 4, 5)
//x: Int = 1
//y: Int = 2
//z: List[Int] = List(3, 4, 5)
val x :: y :: z = List(1, 2)
//x: Int = 1
//y: Int = 2
//z: List[Int] = List()
</code></pre>
</section>
<section>
<h2>Методы. head, tail, isEmpty</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val head = List(1, 2, 3, 4, 5).head
//head: Int = 1
val tail = List(1, 2, 3, 4, 5).tail
//tail: List[Int] = List(2, 3, 4, 5)
val isEmpty = List(1, 2, 3, 4, 5).isEmpty
//isEmpty: Boolean = false
val isEmptyNil = Nil.isEmpty
//isEmptyNil: Boolean = true
</code></pre>
</section>
<section>
<h2>List. много других методов</h2>
<ul>
<li>Scala предоставляет множество методов для работы с коллекциями</li>
<li>Начнем с методов первого порядка</li>
</ul>
</section>
<section>
<h2>::: Объединение списков</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val `2list1` = List(1, 2) ::: List(3, 4, 5)
//`2list1`: List[Int] = List(1, 2, 3, 4, 5)
val `2list2`= List() ::: List(1, 2, 3)
//`2list2`: List[Int] = List(1, 2, 3)
val `2list3` = Nil ::: List(1, 2, 3)
//`2list3`: List[Int] = List(1, 2, 3)
</code></pre>
</section>
<section>
<h2>length, indices</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val len1 = List(1, 2, 3).length
//len1: Int = 3
val len2 = List().length
//len2: Int = 0
val len3 = Nil.length
//len3: Int = 0
val idx1 = List(1, 2, 3).indices
//idx1: Range = Range(0, 1, 2)
val idx2 = Nil.indices
//idx2: Range = Range()
</code></pre>
</section>
<section>
<h2> init, last. Конец и начало</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val abcde = List('a', 'b', 'c', 'd', 'e')
//abcde: List[Char] = List('a', 'b', 'c', 'd', 'e')
val last1 = abcde.last
//last1: Char = 'e'
val init1 = abcde.init
//init1: List[Char] = List('a', 'b', 'c', 'd')
List().init
//java.lang.UnsupportedOperationException: init of empty list
// scala.collection.immutable.Nil$.init(List.scala:596)
// ...
List().last
//java.util.NoSuchElementException: last of empty list
// scala.collection.immutable.Nil$.last(List.scala:595)
// ...
</code></pre>
</section>
<section>
<h2>reverse</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val edcba = List("e", "d", "c", "b", "a")
//edcba: List[String] = List("e", "d", "c", "b", "a")
val reverse1 = edcba.reverse
//reverse1: List[String] = List("a", "b", "c", "d", "e")
</code></pre>
</section>
<section>
<h2>reverse для внимательных</h2>
<pre style="width:auto;"><code data-trim class="text">
lst.reverse.init <=> lst.tail.reverse
lst.reverse.tail <=> lst.init.reverse
lst.reverse.head <=> lst.last
lst.reverse.last <=> lst.head
</code></pre>
</section>
<section>
<h2>drop, take и splitAt</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val abcde = List('a', 'b', 'c', 'd', 'e')
val teke1 = abcde.take(2)
//teke1: List[Char] = List('a', 'b')
val drop1 = abcde.drop(2)
//drop1: List[Char] = List('c', 'd', 'e')
val splitat1 = abcde.splitAt(2)
//splitat1: (List[Char], List[Char]) = (List('a', 'b'), List('c', 'd', 'e'))
</code></pre>
</section>
<section>
<h2> flatten</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val flatten1 = List(List(1, 2), List(3), List(), List(4,5)).flatten
//flatten1: List[Int] = List(1, 2, 3, 4, 5)
val flatten2 = List(1, 2, 3).flatten
//cmd22.sc:1: No implicit view available from Int => scala.collection.IterableOnce[B].
//val flatten2 = List(1, 2, 3).flatten
// ^
//Compilation Failed
val flatten3 = List(List(List(), List(1)), List(List(2))).flatten
//flatten3: List[List[Int]] = List(List(), List(1), List(2))
</code></pre>
</section>
<section>
<h2>zip и unzip</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val abcde = List('a', 'b', 'c', 'd', 'e')
val zip1 = abcde.indices.zip(abcde)
//zip1: IndexedSeq[(Int, Char)] = Vector((0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'))
val zip2 = abcde.zip(List(1, 2, 3))
//zip2: List[(Char, Int)] = List(('a', 1), ('b', 2), ('c', 3))
val zipIdx = abcde.zipWithIndex
/zipIdx: List[(Char, Int)] = List(('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4))
val unzip = zip2.unzip
//unzip: (List[Char], List[Int]) = (List('a', 'b', 'c'), List(1, 2, 3))
</code></pre>
</section>
<section>
<h2>toString</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val abcde = List('a', 'b', 'c', 'd', 'e')
val abcStr = abcde.toString
//abcStr: String = "List(a, b, c, d, e)"
val list = List(1, 2, 3, 4, 5)
val lstStr = list.toString
//lstStr: String = "List(1, 2, 3, 4, 5)"
</code></pre>
</section>
<section>
<h2>mkSting</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val str1 = abcde.mkString ("[", ",", "]")
//str1: String = "[a,b,c,d,e]"
val str2 = abcde.mkString ("", ",", "")
//str2: String = "a,b,c,d,e"
val str3 = abcde.mkString (",")
//str3: String = "a,b,c,d,e"
val str4 = abcde.mkString (",", "]")
//cmd37.sc:1: overloaded method value mkString with alternatives:
//val str4 = abcde.mkString (",", "]")
// ^
//Compilation Failed
</code></pre>
</section>
<section>
<h2>List. методы высшего порядка</h2>
</section>
<section>
<h2>map, foreach</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val abc = List(97 ,98, 99).map(_.toChar)
//abc: List[Char] = List('a', 'b', 'c')
abc.foreach(println)
//a
//b
//c
</code></pre>
</section>
<section>
<h2>filter, partition</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val filter1 = List(1, 2, 3, 4, 5).filter(_ % 2 == 0)
//filter1: List[Int] = List(2, 4)
val filter2 = List(1, 2, 3, 4, 5).filter(_ < 0)
/filter2: List[Int] = List()
val partition1 = List(1, 2, 3, 4, 5).partition(_ % 2 == 0)
//partition1: (List[Int], List[Int]) = (List(2, 4), List(1, 3, 5))
val partition2 = List(1, 2, 3, 4, 5).partition(_ < 0)
//partition2: (List[Int], List[Int]) = (List(), List(1, 2, 3, 4, 5))
</code></pre>
</section>
<section>
<h2>find</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val find1 = List(1, 2, 3, 4, 5).find(_ % 2 == 0)
//find1: Option[Int] = Some(2)
val find2 = List(1, 2, 3, 4, 5).find(_ <= 0)
//find2: Option[Int] = None
</code></pre>
</section>
<section>
<h2>takeWhile, dropWhile</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val takeWhile1 = List(1, 2, 3, -4, 5).takeWhile(_ > 0)
//takeWhile1: List[Int] = List(1, 2, 3)
val dropWhile1 = List("banana", "pear", "apple", "orange")
.dropWhile(_.startsWith("b"))
//dropWhile2: List[String] = List("pear", "apple", "orange")
</code></pre>
</section>
<section>
<h2>span</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val lst = List(1, 2, 3, -4, 5)
val span1 = lst.span(_ > 0)
//span1: (List[Int], List[Int]) = (List(1, 2, 3), List(-4, 5))
val span2 = (lst.takeWhile(_ > 0), lst.dropWhile(_ > 0) )
//span2: (List[Int], List[Int]) = (List(1, 2, 3), List(-4, 5))
</code></pre>
</section>
<section>
<h2>forall, exist, contains</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val forall1 = List(1, 2, 3).forall( _ > 0)
//forall1: Boolean = true
val forall2 = List(1, 2,-3).forall( _ > 0)
//forall2: Boolean = false
val exists1 = List(1, 2,-3).exists( _ < 0)
//exists1: Boolean = true
val exists2 = List(1, 2, 3).exists( _ < 0)
//exists2: Boolean = false
val contains1 = List(1, 2, 3).contains(3)
//contains1: Boolean = true
val contains2 = List(1, 2, 3).contains(0)
//contains2: Boolean = false
</code></pre>
</section>
<section>
<h2>foldLeft foldRight</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val num = List(1, 2, 3)
val foldLeft1 = num.foldLeft(0)(_ + _)
//foldLeft1: Int = 6
val foldLeft2 = num.foldLeft(-6)(_ + _)
//foldLeft2: Int = 0
val foldRight1 = num.foldRight(0)(_ + _)
//foldRight1: Int = 6
val foldRight2 = num.foldRight(-6)(_ + _)
//foldRight2: Int = 0
</code></pre>
</section>
<section>
<h2>foldLeft fildRight. нагляднее</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val lst = List("a", "b", "c", "d")
val foldLeft1 = lst.foldLeft("z")(_ + _)
//foldLeft1: String = "zabcd"
//op(op(op(z, a), b), c)
val foldRight1 = lst.foldRight("z")(_ + _)
//foldRight1: String = "abcdz"
//op(a, op(b, op(c, z)))
val foldLeft2 = lst.tail.foldLeft(lst.head)(_ + " " + _)
//foldLeft2: String = "a b c d"
val foldRight2 = lst.tail.foldRight(lst.head)(_ + " " + _)
//foldRight2: String = "b c d a"
</code></pre>
</section>
<section>
<h2>reduceLeft reduceRight</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val num = List(1, 2, 3)
val reduceLeft1 = lst.reduceLeft(_ + _)
//reduceLeft1: Int = 6
val reduceLeft2 = num.reduceLeft(_ min _)
//reduceLeft2: Int = 1
val reduceRight1 = num.reduceRight(_ + _)
//reduceRight1: Int = 6
val reduceRight2 = num.reduceRight(_ max _)
//reduceRight2: Int = 3
</code></pre>
</section>
<section>
<h2>reduce vs fold</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val emptyLst = List.empty[String]
val foldLeftEmpty = emptyLst.foldLeft("i")(_ + _)
//foldLeftEmpty: String = "i"
val reduceLeftEmpty = emptyLst.reduceLeft(_ + _)
//java.lang.UnsupportedOperationException: empty.reduceLeft
// at scala.collection.IterableOnceOps.reduceLeft(IterableOnce.scala:721)
// at scala.collection.IterableOnceOps.reduceLeft$(IterableOnce.scala:718)
// at scala.collection.AbstractIterable.reduceLeft(Iterable.scala:921)
// ... 28 elided
</code></pre>
</section>
<section>
<h2>sortWith</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val sort1 = List(10, 5, 8, 1, 7).sortWith(_ > _)
//res22: List[Int] = List(10, 8, 7, 5, 1)
val sort2 = List("banana", "pear", "apple", "orange")
.sortWith(_ < _)
//sort2: List[String] = List("apple", "banana",
// "orange", "pear")
val sort3 = List("banana", "pear", "apple", "orange")
.sortWith(_.length < _.length)
//sort3: List[String] = List("pear", "apple",
// "banana", "orange")
</code></pre>
</section>
<section>
<h2>sorted</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val a = List(10, 5, 8, 1, 7).sorted
//sort4: List[Int] = List(1, 5, 7, 8, 10)
</code></pre>
</section>
<section>
<h2>не всё умеет sorted*</h2>
<pre style="width:auto;"><code data-trim class="scala small">
case class Cat (name: String, age: Int)
val s = List(Cat("Мурзик", 2), Cat("Murka", 1)).sorted
// error: No implicit Ordering defined for Cat.
</code></pre>
</section>
<section>
<h2>не всё умеет sorted, решение</h2>
<pre style="width:auto;"><code data-trim class="scala small">
implicit class CatOrdered (cat: Cat) extends Ordered [Cat] {
val m1 = -1
def compare (that: Cat): Int = {
if (cat.name == that.name)
0
else if (cat.name > that.name)
1
else
m1 //ammonite ломается
}
}
</code></pre>
</section>
<section>
<h2>sorted по максимому</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val s = List(Cat("Мурзик", 2), Cat("Murka", 1)).sorted
//s: List[Cat] = List(Cat("Murka", 1), Cat("Мурзик", 2))
</code></pre>
</section>
<section>
<h2>Чего же есть другого?</h2>
</section>
<section>
<h2>Array</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val arr1 = new Array[Int](5)
//arr1: Array[Int] = Array(0, 0, 0, 0, 0)
val arr2 = Array(5, 4, 3, 2, 1)
//arr2: Array[Int] = Array(5, 4, 3, 2, 1)
val arr3 = Array(5)
//arr3: Array[Int] = Array(5)
</code></pre>
</section>
<section>
<h2>Set, Map</h2>
<ul>
<li>Set - множество уникальных значений</li>
<li>Map - множество уникальных отображений</li>
</ul>
</section>
<section>
<h2>Set</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val str = "Беги Лола, беги!"
//str: String = "Беги Лола, беги!"
val split = str.toLowerCase.split("[ !,]+")
//split: Array[String] = Array("беги", "лола", "беги")
var set = collection.mutable.Set.empty[String]
//set: collection.mutable.Set[String] = HashSet()
for (word <- split)
set += word
//set: collection.mutable.Set[String] = HashSet("беги", "лола")
</code></pre>
</section>
<section>
<h2>Map</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val map1 = Map("one" -> 1, "two" -> 2)
//map1: Map[String, Int] = Map("one" -> 1, "two" -> 2)
val map2 = Map("one" -> 1, "two" -> 3, "three" -> 3, "two" -> 2)
//map2: Map[String, Int] = Map("one" -> 1, "two" -> 2, "three" -> 3)
</code></pre>
</section>
<section>
<h2>TreeSet, TreeMap</h2>
SortedSet
<pre style="width:auto;"><code data-trim class="scala small">
import scala.collection.immutable.TreeSet
val tSet1 = TreeSet(9, 4, 5, 3, 2, 5, 8)
//tSet1: TreeSet[Int] = TreeSet(2, 3, 4, 5, 8, 9)
val tSet2 = TreeSet('h', 'e', 'l', 'l', 'o')
//tSet2: TreeSet[Char] = TreeSet('e', 'h', 'l', 'o')
</code></pre>
SortedMap
<pre style="width:auto;"><code data-trim class="scala small">
import scala.collection.immutable.TreeMap
var tMap = TreeMap(3 -> 'c', 1 -> 'b', 4 -> 'a')
//tMap: TreeMap[Int, Char] = TreeMap(1 -> 'b', 3 -> 'c', 4 -> 'a')
</code></pre>
</section>
<section>
<h2>iterator</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val it = Iterator("су", "е", "фа")
while (it.hasNext)
println(it.next())
//су
//е
//фа
</code></pre>
</section>
<section>
<h2>mutable immutable</h2>
<ul>
<li>scala.collection
<ul><li>immutable</li>
<li>mutable</li>
<li>generic</li>
<li>concurrent</li>
</ul>
</li>
</ul>
<pre style="width:auto;"><code data-trim class="scala small">
scala.collection.immutable.List // Полное объявление
scala.List // объявление через псевдоним
List // тк scala._ всегда автоматически импортируется
// можно просто указать имя коллекции
</code></pre>
</section>
<section>
<h2>Иерархия trait</h2>
<center>
<img src="img/lecture_6/collections-diagram.svg"/>
</center>
</section>
<section>
<h2>Иерархия immutable</h2>
<center>
<img src="img/lecture_6/collections-immutable-diagram.svg"/>
</center>
</section>
<section>
<h2>Иерархия mutable</h2>
<center>
<img src="img/lecture_6/collections-mutable-diagram.svg"/>
</center>
</section>
<section>
<h2>Равенство</h2>
<ul>
<li>Set</li>
<li>Map</li>
<li>Seq</li>
</ul>
</section>
<section>
<h2>Равенство</h2>
<pre style="width:auto;"><code data-trim class="scala small">
Set(1, 2, 3) != List(1, 2, 3)
Set(1, 3, 2) == Set(1, 2, 3)
</code></pre>
</section>
<section>
<h2>Равенство</h2>
<ul>
<li>mutable</li>
<li>immutable</li>
<li>НЕ-ВА-ЖНО</li>
</ul>
</section>
<section>
<h2>Равенство</h2>
<pre style="width:auto;"><code data-trim class="scala small">
List(1, 2, 3) == Vector(1, 2, 3)
HashSet(1, 2) == TreeSet(2, 1)
</code></pre>
</section>
<section>
<h2>Отображение. Методы</h2>
<pre style="width:auto;"><code data-trim class="scala small">
<ul>
<li>view</li>
<li>to <i>collection</i></li>
</ul>
</code></pre>
</section>
<section>
<h2>Отображение.</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val v = Vector(1 to 10: _ * )
//v: scala.collection.immutable.Vector[Int] =
// Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val v2 = v.map(_ + 1).map(_ * 2)
//v2: scala.collection.immutable.Vector[Int] =
// Vector(4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
</code></pre>
</section>
<section>
<h2>Отображение Ленивость</h2>
<pre style="width:auto;"><code data-trim class="scala small">
def lazyMap[T, U](coll: Iterable[T], f: T => U) =
new Iterable[U] {
def iterator = coll.iterator.map(f)
}
</code></pre>
</section>
<section>
<h2>Отображение. Делаем ленивым</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val vv = v.view
//vv: scala.collection.IndexedSeqView[Int] = IndexedSeqView(<not computed>)
val vv2 = vv.map(_ + 1)
//vv2: scala.collection.IndexedSeqView[Int] = IndexedSeqView(<not computed>)
val vv3 = vv2.map(_ * 2)
//vv3: scala.collection.IndexedSeqView[Int] = IndexedSeqView(<not computed>)
</code></pre>
</section>
<section>
<h2>Отобржение. Вычисляем.</h2>
<pre style="width:auto;"><code data-trim class="scala small">
val vv5 = vv3.to(Vector)
//vv5: scala.collection.immutable.Vector[Int] =
// Vector(4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
val vv6 = vv3.to(List)
//vv6: List[Int] =
// List(4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
</code></pre>
</section>
<section>
<h2>Performance. Seq. Immutable</h2>
<pre style="width:auto;"><table>
<thead>
<tr>
<th> </th>
<th>head</th>
<th>tail</th>
<th>apply</th>
<th>update</th>
<th>prepend</th>
<th>append</th>
<th>insert</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="scala small">List</code></td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">LazyList</code></td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">ArraySeq</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">Vector</code></td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td title="константное время, но с погрешностью, к примеру, на распределение хеш-ключей">eC</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">Queue</code></td>
<td title="амортизированное константное время">aC</td>
<td title="амортизированное константное время">aC</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">Range</code></td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td>-</td>
<td>-</td>
<td>-</td>
<td>-</td>
</tr>
<tr>
<td><code class="scala small">String</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td>-</td>
</tr>
</tbody>
</table></pre>
</section>
<section>
<h2>Performance. Seq. Mutable</h2>
<pre style="width:auto;">
<table>
<thead>
<tr>
<th> </th>
<th>head</th>
<th>tail</th>
<th>apply</th>
<th>update</th>
<th>prepend</th>
<th>append</th>
<th>insert</th>
</tr>
</thead>
<tbody>
<tr>
<td><code class="scala small">ArrayBuffer</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="амортизированное константное время">aC</td>
<td title="линейное время">L</td>
</tr>
<tr>
<td><code class="scala small">ListBuffer</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
</tr>
<tr>
<td><code class="scala small">StringBuilder</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="амортизированное константное время">aC</td>
<td title="линейное время">L</td>
</tr>
<tr>
<td><code class="scala small">Queue</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
</tr>
<tr>
<td><code class="scala small">ArraySeq</code></td>
<td title="константное время">C</td>
<td title="линейное время">L</td>
<td title="константное время">C</td>