added lesson 6
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/Lesson6/Comprehension.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								src/Lesson6/Comprehension.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,18 @@ | ||||
| (2 to 10).foreach(i => | ||||
|   (2 to 10) | ||||
|     .filter(j => i % j == 0) | ||||
|     .foreach(j => | ||||
|       println(s"$i is an integer multiple of $j") | ||||
|     ) | ||||
| ) | ||||
|  | ||||
| val positions = for ( | ||||
|   col <- 'a' to 'h'; | ||||
|   row <- 1 to 8 | ||||
| ) yield (col, row) | ||||
|  | ||||
| val whites = for ( | ||||
|   col <- 'a' to 'h'; | ||||
|   row <- 1 to 8; | ||||
|   if ((col - 'a' + row) % 2 == 0) | ||||
| ) yield (col, row) | ||||
							
								
								
									
										40
									
								
								src/Lesson6/Ex_6.1.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/Lesson6/Ex_6.1.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | ||||
| def filter[T](func: T => Boolean, list: List[T]): List[T] = { | ||||
|   list match { | ||||
|     case Nil => Nil | ||||
|     case e::rest if func(e) => e::filter(func, rest) | ||||
|     case _::rest => filter(func, rest) | ||||
|   } | ||||
| } | ||||
|  | ||||
| def partition[T](func: T => Boolean, list: List[T]): (List[T], List[T]) = { | ||||
|   list match { | ||||
|     case Nil => (List.empty[T], List.empty[T]) | ||||
|     case e::rest => { | ||||
|       val part = partition(func, rest) | ||||
|       if (func(e)) { | ||||
|         (e::part._1, part._2) | ||||
|       } else { | ||||
|         (part._1, e::part._2) | ||||
|       } | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| def partition[T](func: T => Boolean, list: List[T]): (List[T], List[T]) = { | ||||
|   list.foldRight((List.empty[T], List.empty[T]))( | ||||
|     (e: T, prev: (List[T], List[T])) => { | ||||
|       if (func(e)) { | ||||
|         (e::prev._1, prev._2) | ||||
|       } else { | ||||
|         (prev._1, e::prev._2) | ||||
|       } | ||||
|     } | ||||
|   ) | ||||
| } | ||||
|  | ||||
|  | ||||
| val b = (1 to 10).toList | ||||
|  | ||||
| filter((x: Int) => x % 2 == 0, b) | ||||
|  | ||||
| partition((x: Int) => x % 2 == 0, b) | ||||
							
								
								
									
										29
									
								
								src/Lesson6/Primes.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								src/Lesson6/Primes.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,29 @@ | ||||
| def time(f: => Unit => Any): Long = { | ||||
|   val start = System.currentTimeMillis() | ||||
|   f() | ||||
|   val end = System.currentTimeMillis() | ||||
|   end - start | ||||
| } | ||||
|  | ||||
| def timeVerbose(s: String)(f: => Unit => Any): Unit = { | ||||
|   println(s"[$s] Measuring time...") | ||||
|   val duration = time(f) | ||||
|   println(s"[$s] It took ${duration}ms") | ||||
| } | ||||
|  | ||||
| def isPrime(i: Int): Boolean = | ||||
|   i match { | ||||
|     case i if i <= 1 => false | ||||
|     case 2 => true | ||||
|     case _ => !(2 until i).exists(x => i%x == 0) | ||||
|   } | ||||
|  | ||||
| def primeSum(max: Int)(isPrimeFunc: Int => Boolean): List[(Int, Int)] = { | ||||
|   for (i <- (1 to max).toList; | ||||
|        j <- (1 to max).toList; | ||||
|        if isPrimeFunc(i + j)) yield (i, j) | ||||
| } | ||||
|  | ||||
| timeVerbose("Normal Prime"){ | ||||
|   primeSum(1000)(isPrime) | ||||
| } | ||||
							
								
								
									
										25
									
								
								src/Lesson6/Translations.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/Lesson6/Translations.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,25 @@ | ||||
| val q: List[Int] = List(1, 2, 3) | ||||
| val p: List[Int] = List(4, 5, 6) | ||||
|  | ||||
| // 1. | ||||
| for (x <- q) yield x * 2 | ||||
| q.map(x => x * 2) | ||||
|  | ||||
| // 2. | ||||
| for (x <- q if x != 2) yield x * 2 | ||||
| q.filter(x => x !=2).map(x => x * 2) | ||||
|  | ||||
| // 3. | ||||
| for (x <- q; | ||||
|      y <- p) yield (x, y) | ||||
| q.flatMap(x => p.map(y => (x, y))) | ||||
|  | ||||
| // 4. | ||||
| for (x <- q if (x < 2); | ||||
|      y <- p) yield (x, y) | ||||
| q.filter(x => x < 2).flatMap(x => p.map(y => (x, y))) | ||||
|  | ||||
|  | ||||
| for (x <- 1 to 2; | ||||
|      y <- 'a' to 'b') yield (x, y) | ||||
| (1 to 2).flatMap(x => ('a' to 'b').map(y => (x, y))) | ||||
							
								
								
									
										13
									
								
								src/Lesson6/Tuples.sc
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								src/Lesson6/Tuples.sc
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,13 @@ | ||||
| def merge(x: List[Int], y: List[Int]): List[Int] = { | ||||
|   (x, y) match { | ||||
|     case (list, Nil) => list | ||||
|     case (Nil, list) => list | ||||
|     case (e1::rest1, e2::rest2) => | ||||
|       if (e1 < e2) e1::merge(rest1, y) | ||||
|       else e2::merge(x, rest2) | ||||
|   } | ||||
| } | ||||
|  | ||||
| val n1 = List(0, 2, 4, 6) | ||||
| val n2 = List(1, 3, 5, 7) | ||||
| merge(n1, n2) | ||||
		Reference in New Issue
	
	Block a user