progs/lecture1.scala
changeset 444 7a0735db4788
parent 367 e6ae724255a8
child 447 f51e593903ac
equal deleted inserted replaced
443:80c691a4caab 444:7a0735db4788
   434 // Remember:
   434 // Remember:
   435 // - no vars, no ++i, no +=
   435 // - no vars, no ++i, no +=
   436 // - no mutable data-structures (no Arrays, no ListBuffers)
   436 // - no mutable data-structures (no Arrays, no ListBuffers)
   437 
   437 
   438 // But what the heck....lets try to count to 1 Mio in parallel
   438 // But what the heck....lets try to count to 1 Mio in parallel
       
   439 import scala.collection.parallel.CollectionConverters._
   439 
   440 
   440 var cnt = 0
   441 var cnt = 0
   441 
   442 
   442 for(i <- (1 to 1000000).par) cnt += 1
   443 for(i <- (1 to 1000000).par) cnt += 1
   443 
   444 
   450 //    two sets?
   451 //    two sets?
   451 // A; IMPROPER WAY (mutable counter)
   452 // A; IMPROPER WAY (mutable counter)
   452 
   453 
   453 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   454 def count_intersection(A: Set[Int], B: Set[Int]) : Int = {
   454   var count = 0
   455   var count = 0
   455   for (x <- A.par; if (B contains x)) count += 1 
   456   for (x <- A; if (B contains x)) count += 1 
   456   count
   457   count
   457 }
   458 }
   458 
   459 
   459 val A = (0 to 999).toSet
   460 val A = (0 to 999).toSet
   460 val B = (0 to 999 by 4).toSet
   461 val B = (0 to 999 by 4).toSet