diff --git a/README.md b/README.md index 640b947..e5201e4 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,13 @@ [Files](src/Lesson5) - Lists - High order functions +- +### Lesson 6 - Tuples and comprehensions +[Files](src/Lesson6) +- Tuples +- For-comprehension +- Yield +- Flatmap ### Midterm preparation [Files](src/MidTermPrep1) diff --git a/src/Lesson6/Comprehension.sc b/src/Lesson6/Comprehension.sc new file mode 100644 index 0000000..9bc922e --- /dev/null +++ b/src/Lesson6/Comprehension.sc @@ -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) diff --git a/src/Lesson6/Ex_6.1.sc b/src/Lesson6/Ex_6.1.sc new file mode 100644 index 0000000..6f71fee --- /dev/null +++ b/src/Lesson6/Ex_6.1.sc @@ -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) diff --git a/src/Lesson6/Primes.sc b/src/Lesson6/Primes.sc new file mode 100644 index 0000000..d66b616 --- /dev/null +++ b/src/Lesson6/Primes.sc @@ -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) +} \ No newline at end of file diff --git a/src/Lesson6/Translations.sc b/src/Lesson6/Translations.sc new file mode 100644 index 0000000..812bfae --- /dev/null +++ b/src/Lesson6/Translations.sc @@ -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))) \ No newline at end of file diff --git a/src/Lesson6/Tuples.sc b/src/Lesson6/Tuples.sc new file mode 100644 index 0000000..14fc661 --- /dev/null +++ b/src/Lesson6/Tuples.sc @@ -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) \ No newline at end of file