added lesson 7
This commit is contained in:
parent
9ad00e6182
commit
d8b22157c5
26
README.md
26
README.md
@ -57,13 +57,6 @@
|
|||||||
[Files](src/Lesson5)
|
[Files](src/Lesson5)
|
||||||
- Lists
|
- Lists
|
||||||
- High order functions
|
- High order functions
|
||||||
-
|
|
||||||
### Lesson 6 - Tuples and comprehensions
|
|
||||||
[Files](src/Lesson6)
|
|
||||||
- Tuples
|
|
||||||
- For-comprehension
|
|
||||||
- Yield
|
|
||||||
- Flatmap
|
|
||||||
|
|
||||||
### Midterm preparation
|
### Midterm preparation
|
||||||
[Files](src/MidTermPrep1)
|
[Files](src/MidTermPrep1)
|
||||||
@ -71,6 +64,20 @@
|
|||||||
### Midterm
|
### Midterm
|
||||||
[Files](src/MidTerm1)
|
[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
|
## Assignments
|
||||||
|
|
||||||
@ -106,3 +113,8 @@
|
|||||||
- Map
|
- Map
|
||||||
- Fold
|
- Fold
|
||||||
- Zip
|
- Zip
|
||||||
|
|
||||||
|
### Assignment 6 - Sequence comprehension and tuples
|
||||||
|
[Files](src/Assignment6)
|
||||||
|
- Tuples
|
||||||
|
- `for` comprehension
|
12
src/Lesson7/Daltons.sc
Normal file
12
src/Lesson7/Daltons.sc
Normal 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
|
6
src/Lesson7/LazyEvaluation.sc
Normal file
6
src/Lesson7/LazyEvaluation.sc
Normal 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
11
src/Lesson7/LazyList.sc
Normal 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
|
16
src/Lesson7/LoggingFactory.sc
Normal file
16
src/Lesson7/LoggingFactory.sc
Normal 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
25
src/Lesson7/Variance.sc
Normal 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
|
8
src/MidTermPrep1/Exercices.sc
Normal file
8
src/MidTermPrep1/Exercices.sc
Normal 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'))
|
Loading…
x
Reference in New Issue
Block a user