added lesson 7

This commit is contained in:
Louis Heredero 2025-04-15 15:07:26 +02:00
parent 9ad00e6182
commit d8b22157c5
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
7 changed files with 98 additions and 8 deletions

View File

@ -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
@ -106,3 +113,8 @@
- Map
- Fold
- Zip
### Assignment 6 - Sequence comprehension and tuples
[Files](src/Assignment6)
- Tuples
- `for` comprehension

12
src/Lesson7/Daltons.sc Normal file
View File

@ -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

View File

@ -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
}

11
src/Lesson7/LazyList.sc Normal file
View File

@ -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

View File

@ -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

25
src/Lesson7/Variance.sc Normal file
View File

@ -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

View File

@ -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'))