forked from darrenjw/scala-course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collections.scala
516 lines (429 loc) · 13.2 KB
/
collections.scala
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
val l1 = List(6,7,8,9,10)
// l1: List[Int] = List(6, 7, 8, 9, 10)
l1.head
// res0: Int = 6
l1.tail
// res1: List[Int] = List(7, 8, 9, 10)
l1(0)
// res2: Int = 6
l1(2)
// res3: Int = 8
l1(2) = 22
// <console>:9: error: value update is not a member of List[Int]
// l1(2) = 22
// ^
l1
// res5: List[Int] = List(6, 7, 8, 9, 10)
val l2: List[Double] = List(1,2,3)
// l2: List[Double] = List(1.0, 2.0, 3.0)
import scala.collection.mutable
// import scala.collection.mutable
val l3 = mutable.MutableList(5,6,7,8,9)
// l3: MutableList[Int] = MutableList(5, 6, 7, 8, 9)
l3.head
// res6: Int = 5
l3.tail
// res7: MutableList[Int] = MutableList(6, 7, 8, 9)
l3(2)
// res8: Int = 7
l3(2) = 22
l3
// res10: MutableList[Int] = MutableList(5, 6, 22, 8, 9)
val list1 = List(3,4,5,6)
// list1: List[Int] = List(3, 4, 5, 6)
val list2 = 2 :: list1
// list2: List[Int] = List(2, 3, 4, 5, 6)
val list3 = list1 ++ list2
// list3: List[Int] = List(3, 4, 5, 6, 2, 3, 4, 5, 6)
list3.take(3)
// res0: List[Int] = List(3, 4, 5)
list3.drop(2)
// res1: List[Int] = List(5, 6, 2, 3, 4, 5, 6)
list3.filter(_<5)
// res2: List[Int] = List(3, 4, 2, 3, 4)
val list4 = list1 map (_*2)
// list4: List[Int] = List(6, 8, 10, 12)
list4.length
// res3: Int = 4
list4.sum
// res4: Int = 36
list4.reverse
// res5: List[Int] = List(12, 10, 8, 6)
list1.foldLeft(0)(_+_)
// res6: Int = 18
list1.scanLeft(0)(_+_)
// res7: List[Int] = List(0, 3, 7, 12, 18)
list1.reduce(_+_)
// res8: Int = 18
list1.sortWith(_>_)
// res9: List[Int] = List(6, 5, 4, 3)
list1.foreach{e => println(e)}
// 3
// 4
// 5
// 6
val arr1 = Array(2,3,4,5)
// arr1: Array[Int] = Array(2, 3, 4, 5)
arr1(1)=6
arr1
// res12: Array[Int] = Array(2, 6, 4, 5)
val arr2 = arr1 map(_+1)
// arr2: Array[Int] = Array(3, 7, 5, 6)
arr1
// res13: Array[Int] = Array(2, 6, 4, 5)
arr1.reduce(_+_)
// res14: Int = 17
arr1 ++ arr2
// res15: Array[Int] = Array(2, 6, 4, 5, 3, 7, 5, 6)
val vec1 = Vector(6,5,4,3)
// vec1: Vector[Int] = Vector(6, 5, 4, 3)
vec1(2)
// res16: Int = 4
vec1.toList
// res17: List[Int] = List(6, 5, 4, 3)
vec1 map (_*0.5)
// res18: Vector[Double] = Vector(3.0, 2.5, 2.0, 1.5)
val vec2 = vec1.updated(2,10)
// vec2: Vector[Int] = Vector(6, 5, 10, 3)
vec2.length
// res19: Int = 4
vec1
// res20: Vector[Int] = Vector(6, 5, 4, 3)
vec2
// res21: Vector[Int] = Vector(6, 5, 10, 3)
vec1 ++ vec2
// res22: Vector[Int] = Vector(6, 5, 4, 3, 6, 5, 10, 3)
vec1 :+ 22
// res23: Vector[Int] = Vector(6, 5, 4, 3, 22)
33 +: vec1
// res24: Vector[Int] = Vector(33, 6, 5, 4, 3)
vec1.reduce(_+_)
// res25: Int = 18
Vector.fill(5)(0)
// res26: Vector[Int] = Vector(0, 0, 0, 0, 0)
Vector.fill(5)(math.random)
// res27: Vector[Double] = Vector(0.35160713387930087, 0.0892297132191533, 0.9061352705408103, 0.7020295276855067, 0.09434089580898397)
val map1 = Map("a"->10,"b"->20,"c"->30)
// map1: Map[String,Int] = Map(a -> 10, b -> 20, c -> 30)
map1("b")
// res28: Int = 20
val map2 = map1.updated("d",40)
// map2: Map[String,Int] = Map(a -> 10, b -> 20,
// c -> 30, d -> 40)
map1
// res29: Map[String,Int] = Map(a -> 10, b -> 20, c -> 30)
map2
// res30: Map[String,Int] = Map(a -> 10, b -> 20,
// c -> 30, d -> 40)
map2.updated("d",50)
// res31: Map[String,Int] = Map(a -> 10, b -> 20,
// c -> 30, d -> 50)
map2.keys
// res32: Iterable[String] = Set(a, b, c, d)
map2.values
// res33: Iterable[Int] = MapLike(10, 20, 30, 40)
map2 mapValues (_*2)
// res34: Map[String,Int] = Map(a -> 20, b -> 40,
// c -> 60, d -> 80)
val mapK = List(3,4,5,6)
// mapK: List[Int] = List(3, 4, 5, 6)
val mapV = List(0.3,0.4,0.5,0.6)
// mapV: List[Double] = List(0.3, 0.4, 0.5, 0.6)
val pairs = mapK zip mapV
// pairs: List[(Int, Double)] = List((3,0.3),
// (4,0.4), (5,0.5), (6,0.6))
val map3 = pairs.toMap
// map3: Map[Int,Double] = Map(3 -> 0.3, 4 -> 0.4,
// 5 -> 0.5, 6 -> 0.6)
val stream1 = Stream(1,2,3)
// stream1: Stream[Int] = Stream(1, ?)
val stream2 = 0 #:: stream1
// stream2: Stream[Int] = Stream(0, ?)
stream2.toList
// res35: List[Int] = List(0, 1, 2, 3)
stream2.foldLeft(0)(_+_)
// res36: Int = 6
def fibFrom(a: Int,b: Int): Stream[Int] =
a #:: fibFrom(b,a+b)
// fibFrom: (a: Int, b: Int)Stream[Int]
val fib = fibFrom(1,1)
// fib: Stream[Int] = Stream(1, ?)
fib.take(8).toList
// res37: List[Int] = List(1, 1, 2, 3, 5, 8, 13, 21)
val naturals = Stream.iterate(1)(_+1)
// naturals: Stream[Int] = Stream(1, ?)
naturals.take(5).toList
// res38: List[Int] = List(1, 2, 3, 4, 5)
val evens = naturals map (_*2)
// evens: Stream[Int] = Stream(2, ?)
evens.take(6).toList
// res39: List[Int] = List(2, 4, 6, 8, 10, 12)
val triangular = naturals.scanLeft(0)(_+_).drop(1)
// triangular: Stream[Int] = Stream(1, ?)
triangular.take(8).toList
// res40: List[Int] = List(1, 3, 6, 10, 15, 21, 28, 36)
1 to 5
// res41: Range.Inclusive = Range(1, 2, 3, 4, 5)
0 to 5
// res42: Range.Inclusive = Range(0, 1, 2, 3, 4, 5)
0 until 5
// res43: Range = Range(0, 1, 2, 3, 4)
0 to 10 by 2
// res44: Range = Range(0, 2, 4, 6, 8, 10)
val range = 0 to 10 by 2
// range: Range = Range(0, 2, 4, 6, 8, 10)
range.toList
// res45: List[Int] = List(0, 2, 4, 6, 8, 10)
(0 to 5) map (_*2)
// res46: IndexedSeq[Int] = Vector(0, 2, 4, 6, 8, 10)
(0 to 5).sum
// res47: Int = 15
def meanList(ld: List[Double]): Double = ld.sum/ld.length
// meanList: (ld: List[Double])Double
meanList(List(1,2,3,4))
// res0: Double = 2.5
def mean(sq: Seq[Double]): Double = sq.sum/sq.length
// mean: (sq: Seq[Double])Double
mean(List(2,3,4))
// res1: Double = 3.0
mean(Vector(1,2,3))
// res2: Double = 2.0
def repD(ld: List[Double], n: Int): List[Double] =
if (n <= 1) ld else ld ++ repD(ld,n-1)
// repD: (ld: List[Double], n: Int)List[Double]
repD(List(1,2,3),3)
// res3: List[Double] = List(1.0,2.0,3.0,1.0,2.0,3.0,1.0,2.0,3.0)
def repl[T](l: List[T], n: Int): List[T] =
if (n <=1) l else l ++ repl(l,n-1)
// repl: [T](l: List[T], n: Int)List[T]
repl(List(1,2,3),3)
// res5: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3)
repl(List(1.0,2.0),3)
// res6: List[Double] = List(1.0,2.0,1.0,2.0,1.0,2.0)
repl(List("a","b","c"),2)
// res7: List[String] = List(a, b, c, a, b, c)
def reps[T](l: Seq[T], n: Int): Seq[T] =
if (n <=1) l else l ++ reps(l,n-1)
// reps: [T](l: Seq[T], n: Int)Seq[T]
reps(List(1,2,3),3)
// res8: Seq[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3)
reps(Vector(1.0,2.0),3)
// res9: Seq[Double] = Vector(1.0,2.0,1.0,2.0,1.0,2.0)
reps(Array("a","b","c"),2)
// res10: Seq[String] = ArrayBuffer(a, b, c, a, b, c)
import scala.collection.SeqLike
// import scala.collection.SeqLike
def rep[T, C <: Seq[T]](l: C with SeqLike[T,C], n: Int): C =
if (n <=1) l else (l ++ rep(l,n-1)).asInstanceOf[C]
// rep: [T, C <: Seq[T]](l: C with SeqLike[T,C], n: Int)C
rep(List(1,2,3),3)
// res11: List[Int] = List(1, 2, 3, 1, 2, 3, 1, 2, 3)
rep(Vector(1.0,2.0),3)
// res12: Vector[Double] = Vector(1.0,2.0,1.0,2.0,1.0,2.0)
rep(Array("a","b","c").seq,2)
// res13: IndexedSeq[String] = ArrayBuffer(a, b, c, a, b, c)
def ssd(sq: Seq[Double]): Double = (sq map (x => x*x)).sum
// ssd: (sq: Seq[Double])Double
ssd(List(2,3,4))
// res7: Double = 29.0
def ssg[T](sq: Seq[T]): T = (sq map (x => x*x)).sum
// <console>:7: error: value * is not a member of type parameter T
// def ssg[T](sq: Seq[T]): T = (sq map (x => x*x)).sum
def ss[T](sq: Seq[T])(implicit num: Numeric[T]): T = {
import num._
(sq map (x => x*x)).sum
}
// ss: [T](sq: Seq[T])(implicit num: Numeric[T])T
ss(List(2,3,4))
// res8: Int = 29
ss(Vector(1.0,2.0,3.0))
// res9: Double = 14.0
def ssg[T: Numeric](sq: Seq[T]): T = {
val num = implicitly[Numeric[T]]
import num._
(sq map (x => x*x)).sum
}
// ssg: [T](sq: Seq[T])(implicit evidence$1: Numeric[T])T
ssg(List(2,3,4))
// res10: Int = 29
ssg(Vector(1.0,2.0,3.0))
// res11: Double = 14.0
import spire.math._
import spire.implicits._
def ss[T: Numeric](sq: Seq[T]): T =
(sq map (x=>x*x)).reduce(_+_)
ss(List(1,2,3))
// res0: Int = 14
ss(Vector(2.0,3.0,4.0))
// res1: Double = 29.0
(1 to 12).par
// res0: ParRange = ParRange(1,2,3,4,5,6,7,8,9,10,11,12)
Vector(2,4,6,8).par
// res1: ParVector[Int] = ParVector(2, 4, 6, 8)
Array("a","b","c").par
// res2: ParArray[String] = ParArray(a, b, c)
List(1,2,3,4).par
// res3: ParSeq[Int] = ParVector(1, 2, 3, 4)
def isPrime(n: Int): Boolean = (2 until n) forall (
n % _ != 0)
// isPrime: (n: Int)Boolean
(2 to 20) filter isPrime
// res4: IndexedSeq[Int] = Vector(2,3,5,7,11,13,17,19)
((2 to 100000) filter isPrime).length
// res5: Int = 9592
((2 to 100000).par filter isPrime).length
// res6: Int = 9592
(100000000 to 100000100) filter isPrime
// res7: IndexedSeq[Int] = Vector(100000007, 100000037, 100000039, 100000049, 100000073, 100000081)
(100000000 to 100000100).par filter isPrime
// res8: ParSeq[Int] = ParVector(100000007, 100000037, 100000039, 100000049, 100000073, 100000081)
List().par.tasksupport.parallelismLevel
// res16: Int = 8
def sss(sq: Seq[Int]): Int = (sq map (x => x*x)).
reduce(_+_)
// sss: (sq: Seq[Int])Int
sss(List(2,3,4))
// res9: Int = 29
sss(Vector(2,3,4))
// res10: Int = 29
sss(List(2,3,4).par)
// <console>:9: error: type mismatch;
// found : scala.collection.parallel.immutable.ParSeq[Int]
// required: Seq[Int]
// sss(List(2,3,4).par)
// ^
import scala.collection.GenSeq
// import scala.collection.GenSeq
def ssp(sq: GenSeq[Int]): Int = (sq map (x => x*x)).
reduce(_+_)
// ssp: (sq: scala.collection.GenSeq[Int])Int
ssp(List(2,3,4))
// res12: Int = 29
ssp(List(2,3,4).par)
// res13: Int = 29
ssp(Vector(2,3,4).par)
// res14: Int = 29
ssp(Array(2,3,4).par)
// res15: Int = 29
val x = (0 to 4).toList
// x: List[Int] = List(0, 1, 2, 3, 4)
val x2 = x map { x => x * 3 }
// x2: List[Int] = List(0, 3, 6, 9, 12)
val x3 = x map { _ * 3 }
// x3: List[Int] = List(0, 3, 6, 9, 12)
val x4 = x map { _ * 0.1 }
// x4: List[Double] = List(0.0, 0.1, 0.2, 0.30000000000000004, 0.4)
val xv = x.toVector
// xv: Vector[Int] = Vector(0, 1, 2, 3, 4)
val xv2 = xv map { _ * 0.2 }
// xv2: Vector[Double] = Vector(0.0, 0.2, 0.4, 0.6000000000000001, 0.8)
val xv3 = for (xi <- xv) yield (xi * 0.2)
// xv3: Vector[Double] = Vector(0.0, 0.2, 0.4, 0.6000000000000001, 0.8)
trait F[T] {
def map(f: T => S): F[S]
}
val x5 = x map { x => List(x - 0.1, x + 0.1) }
// x5: List[List[Double]] = List(List(-0.1, 0.1),
// List(0.9, 1.1), List(1.9, 2.1), List(2.9, 3.1),
// List(3.9, 4.1))
val x6 = x flatMap { x => List(x - 0.1, x + 0.1) }
// x6: List[Double] = List(-0.1, 0.1, 0.9, 1.1, 1.9,
// 2.1, 2.9, 3.1, 3.9, 4.1)
val y = (0 to 12 by 2).toList
// y: List[Int] = List(0, 2, 4, 6, 8, 10, 12)
val xy = x flatMap { xi => y map { yi => xi * yi } }
// xy: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 6,
// 8, 10, 12, 0, 4, 8, 12, 16, 20, 24, 0, 6, 12, 18,
// 24, 30, 36, 0, 8, 16, 24, 32, 40, 48)
val xy2 = for {
xi <- x
yi <- y
} yield (xi * yi)
// xy2: List[Int] = List(0, 0, 0, 0, 0, 0, 0, 0, 2, 4,
// 6, 8, 10, 12, 0, 4, 8, 12, 16, 20, 24, 0, 6, 12,
// 18, 24, 30, 36, 0, 8, 16, 24, 32, 40, 48)
trait M[T] {
def map(f: T => S): M[S]
def flatMap(f: T => M[S]): M[S]
}
val three = Option(3)
// three: Option[Int] = Some(3)
val twelve = three map (_ * 4)
// twelve: Option[Int] = Some(12)
val four = Option(4)
// four: Option[Int] = Some(4)
val twelveB = three map (i => four map (i * _))
// twelveB: Option[Option[Int]] = Some(Some(12))
val twelveC = three flatMap (i => four map (i * _))
// twelveC: Option[Int] = Some(12)
val twelveD = for {
i <- three
j <- four
} yield (i * j)
// twelveD: Option[Int] = Some(12)
val oops: Option[Int] = None
// oops: Option[Int] = None
val oopsB = for {
i <- three
j <- oops
} yield (i * j)
// oopsB: Option[Int] = None
val oopsC = for {
i <- oops
j <- four
} yield (i * j)
// oopsC: Option[Int] = None
val nan = Double.NaN
3.0 * 4.0
// res0: Double = 12.0
3.0 * nan
// res1: Double = NaN
nan * 4.0
// res2: Double = NaN
val nanB = 0.0 / 0.0
// nanB: Double = NaN
val nanC=0/0
// This raises a runtime exception!
import breeze.linalg._
def safeChol(m: DenseMatrix[Double]): Option[DenseMatrix[Double]] =
scala.util.Try(cholesky(m)).toOption
val m = DenseMatrix((2.0, 1.0), (1.0, 3.0))
val c = safeChol(m)
// c: Option[breeze.linalg.DenseMatrix[Double]] =
// Some(1.4142135623730951 0.0
// 0.7071067811865475 1.5811388300841898 )
val m2 = DenseMatrix((1.0, 2.0), (2.0, 3.0))
val c2 = safeChol(m2)
// c2: Option[breeze.linalg.DenseMatrix[Double]] = None
import com.github.fommil.netlib.BLAS.{getInstance => blas}
def dangerousForwardSolve(A: DenseMatrix[Double],
y: DenseVector[Double]): DenseVector[Double] = {
val yc = y.copy
blas.dtrsv("L", "N", "N", A.cols, A.toArray, A.rows, yc.data, 1)
yc
}
def safeForwardSolve(A: DenseMatrix[Double],
y: DenseVector[Double]): Option[DenseVector[Double]] =
scala.util.Try(dangerousForwardSolve(A, y)).toOption
def safeStd(A: DenseMatrix[Double], y: DenseVector[Double]):
Option[DenseVector[Double]] = for {
L <- safeChol(A)
z <- safeForwardSolve(L, y)
} yield z
safeStd(m,DenseVector(1.0,2.0))
// res15: Option[breeze.linalg.DenseVector[Double]] =
// Some(DenseVector(0.7071067811865475, 0.9486832980505138))
import scala.concurrent.duration._
import scala.concurrent.{Future,ExecutionContext,Await}
import ExecutionContext.Implicits.global
val f1=Future{
Thread.sleep(10000)
1 }
val f2=Future{
Thread.sleep(10000)
2 }
val f3=for {
v1 <- f1
v2 <- f2
} yield (v1+v2)
println(Await.result(f3,30.second))