From d8b22157c56f5743b8446abee6a03e04ec196394 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 15 Apr 2025 15:07:26 +0200 Subject: [PATCH] added lesson 7 --- README.md | 28 ++++++++++++++++++++-------- src/Lesson7/Daltons.sc | 12 ++++++++++++ src/Lesson7/LazyEvaluation.sc | 6 ++++++ src/Lesson7/LazyList.sc | 11 +++++++++++ src/Lesson7/LoggingFactory.sc | 16 ++++++++++++++++ src/Lesson7/Variance.sc | 25 +++++++++++++++++++++++++ src/MidTermPrep1/Exercices.sc | 8 ++++++++ 7 files changed, 98 insertions(+), 8 deletions(-) create mode 100644 src/Lesson7/Daltons.sc create mode 100644 src/Lesson7/LazyEvaluation.sc create mode 100644 src/Lesson7/LazyList.sc create mode 100644 src/Lesson7/LoggingFactory.sc create mode 100644 src/Lesson7/Variance.sc create mode 100644 src/MidTermPrep1/Exercices.sc diff --git a/README.md b/README.md index e5201e4..7331c4d 100644 --- a/README.md +++ b/README.md @@ -57,13 +57,6 @@ [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) @@ -71,6 +64,20 @@ ### Midterm [Files](src/MidTerm1) +### Lesson 6 - Tuples and comprehensions +[Files](src/Lesson6) +- Tuples +- For-comprehension +- Yield +- Flatmap + +### Lesson 7 - Advanced typing and infinite lists +[Files](src/Lesson7) +- Types +- Bounds +- Traits +- Variance, covariance and contra-variance +- Infinite sequences ## Assignments @@ -105,4 +112,9 @@ - Lists - Map - Fold -- Zip \ No newline at end of file +- Zip + +### Assignment 6 - Sequence comprehension and tuples +[Files](src/Assignment6) +- Tuples +- `for` comprehension \ No newline at end of file diff --git a/src/Lesson7/Daltons.sc b/src/Lesson7/Daltons.sc new file mode 100644 index 0000000..1d81183 --- /dev/null +++ b/src/Lesson7/Daltons.sc @@ -0,0 +1,12 @@ +case class Dalton(n: Int) extends Ordered[Dalton] { + //override def compare(that: Dalton): Int = this.n compare that.n + override def compare(that: Dalton): Int = this.n - that.n +} + +val a = Dalton(2); val b = Dalton(3); val c = Dalton(2) + +a < b +a > b +a == b +a == c +a >= c \ No newline at end of file diff --git a/src/Lesson7/LazyEvaluation.sc b/src/Lesson7/LazyEvaluation.sc new file mode 100644 index 0000000..7b5deb2 --- /dev/null +++ b/src/Lesson7/LazyEvaluation.sc @@ -0,0 +1,6 @@ +val expr = { + val x = {print("x"); 1} + lazy val y = {print("y"); 2} + def z = {print("z"); 3} + z + y + x + z + y + x +} \ No newline at end of file diff --git a/src/Lesson7/LazyList.sc b/src/Lesson7/LazyList.sc new file mode 100644 index 0000000..9bc7e9d --- /dev/null +++ b/src/Lesson7/LazyList.sc @@ -0,0 +1,11 @@ +def range(low: Int, high: Int): LazyList[Int] = { + println(s"Calling range with low=$low / high=$high") + if (low >= high) LazyList.empty[Int] + else low #:: range(low + 1, high) +} + +range(1, 10)(0) + +range(1, 10) map (x => x + 1) + +(range(1, 10) map (x => x + 1)).toList diff --git a/src/Lesson7/LoggingFactory.sc b/src/Lesson7/LoggingFactory.sc new file mode 100644 index 0000000..31fd85a --- /dev/null +++ b/src/Lesson7/LoggingFactory.sc @@ -0,0 +1,16 @@ +trait Logged { + def log(msg: String) +} + +trait ConsoleLogger extends Logged { + override def log(msg: String) = println("[LOG] " + msg) +} + +abstract class Person(name: String) + +class Customer(n: String) extends Person(n) with Logged { + log(s"Person $n created") +} + +val a = new Customer("Patrick Jane") with ConsoleLogger + diff --git a/src/Lesson7/Variance.sc b/src/Lesson7/Variance.sc new file mode 100644 index 0000000..9da3a87 --- /dev/null +++ b/src/Lesson7/Variance.sc @@ -0,0 +1,25 @@ +class Animal(val name: String, val kind: String) +class Cat(name: String) extends Animal(name, "Cat") +class Dog(name: String) extends Animal(name, "Dog") + +val anim1: Animal = new Animal("Booboo", "Baboon") +val cat1 = new Cat("Miaou") + +// Standard polymorphism +val anim2: Animal = cat1 + +val dog1: Dog = new Dog("Choucroute") +val anim3: Animal = dog1 + +class Container[+A](val elems: List[A]) { + def get(i: Int): A = elems(i) + def put[B >: A](elem: B) = new Container(elem::elems) +} + +val animalCollection = new Container[Animal](Nil).put(anim1) + +val catCollection = new Container[Cat](Nil).put(cat1).put(new Cat("Garfield")) + +animalCollection.put(cat1) + +val animalCollection2: Container[Animal] = catCollection \ No newline at end of file diff --git a/src/MidTermPrep1/Exercices.sc b/src/MidTermPrep1/Exercices.sc new file mode 100644 index 0000000..133ebf4 --- /dev/null +++ b/src/MidTermPrep1/Exercices.sc @@ -0,0 +1,8 @@ +def compress[T](list: List[T]): List[T] = { + list.foldRight(List.empty[T])((elmt: T, res: List[T]) => res match { + case head::_ if head == elmt => res + case _ => elmt::res + }) +} + +compress(List('a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e')) \ No newline at end of file