Compare commits

...

3 Commits

Author SHA1 Message Date
5365eabadd
updated README 2025-05-06 14:37:16 +02:00
43e0694d9d
added assignment 9 ex 1 2025-05-06 14:32:03 +02:00
c13f9851e3
added lesson 9 2025-05-06 14:31:51 +02:00
4 changed files with 93 additions and 1 deletions

View File

@ -17,12 +17,20 @@
* [Lesson 5 - Advanced lists and High order functions](#lesson-5---advanced-lists-and-high-order-functions)
* [Midterm preparation](#midterm-preparation)
* [Midterm](#midterm)
* [Lesson 6 - Tuples and comprehensions](#lesson-6---tuples-and-comprehensions)
* [Lesson 7 - Advanced typing and infinite lists](#lesson-7---advanced-typing-and-infinite-lists)
* [Lesson 8 - Futures and parallel collections](#lesson-8---futures-and-parallel-collections)
* [Lesson 9 - DSLs](#lesson-9---dsls)
* [Assignments](#assignments)
* [Assignment 1 - Square root](#assignment-1---square-root)
* [Assignment 2 - Map-reduce](#assignment-2---map-reduce)
* [Assignment 3 - Binary tree](#assignment-3---binary-tree)
* [Assignment 4 - Lists and pattern matching](#assignment-4---lists-and-pattern-matching)
* [Assignment 5 - High-order functions on lists](#assignment-5---high-order-functions-on-lists)
* [Assignment 6 - Sequence comprehension and tuples](#assignment-6---sequence-comprehension-and-tuples)
* [Assignment 7 - Advanced typing and infinite lists](#assignment-7---advanced-typing-and-infinite-lists)
* [Assignment 8 - Advanced typing and infinite lists](#assignment-8---advanced-typing-and-infinite-lists)
* [Assignment 9 - DSLs](#assignment-9---dsls)
<!-- TOC -->
@ -79,6 +87,16 @@
- Variance, covariance and contra-variance
- Infinite sequences
### Lesson 8 - Futures and parallel collections
[Files](src/Lesson8)
- Futures
- Actors
- Parallel collections
### Lesson 9 - DSLs
[Files](src/Lesson9)
- DSL
## Assignments
### Assignment 1 - Square root
@ -117,4 +135,18 @@
### Assignment 6 - Sequence comprehension and tuples
[Files](src/Assignment6)
- Tuples
- `for` comprehension
- `for` comprehension
### Assignment 7 - Advanced typing and infinite lists
[Files](src/Assignment7)
- Genericity
- Infinite lazy lists
### Assignment 8 - Advanced typing and infinite lists
[Files](src/Assignment8)
- Parallel collections
- Futures
### Assignment 9 - DSLs
[Files](src/Assignment9)
- DSL

View File

@ -0,0 +1,29 @@
import Assignment9.Kelvin.kel2cel
import scala.language.implicitConversions
package object Assignment9 {
sealed trait Temperature {
}
object Temperature {
implicit def cel2kel(celsius: Celsius): Kelvin = new Kelvin(celsius.value + 273.15)
implicit def kel2cel(kelvin: Kelvin): Celsius = new Celsius(kelvin.value - 273.15)
}
case class Celsius(value: Double) extends Temperature {
override def toString: String = s"$value°C"
}
object Celsius {
implicit def val2cel(value: Double): Celsius = new Celsius(value)
}
case class Kelvin(value: Double) extends Temperature {
override def toString: String = s"$value K"
}
object Kelvin {
implicit def kel2cel(value: Double): Kelvin = new Kelvin(value)
}
}

16
src/Assignment9/Ex1.scala Normal file
View File

@ -0,0 +1,16 @@
package Assignment9
import scala.language.implicitConversions
object Ex1 extends App {
val a: Celsius = 30
val b: Kelvin = 30
val c: Kelvin = Celsius(10)
val d: Celsius = c
val e: Temperature = d
println(a) // Should print "30°C"
println(b) // Should print "30 K"
println()
}

View File

@ -0,0 +1,15 @@
package Lesson9
import scala.language.{implicitConversions, postfixOps}
object PimpMyLibrary extends App{
/*class PimpedString(s: String) {
def increment: String = new String(s.toCharArray.map(_ + 1))
}
implicit def str2Pimped(s: String): PimpedString = new PimpedString(s)
println("Hal" increment)*/
}