Skip to content
Snippets Groups Projects
Commit 5e173897 authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[lec03] Scala 3 migration

parent 8a9bca1f
Branches readerwriter
No related tags found
No related merge requests found
package errors
sealed trait Either[+E, +A]:
enum Either[+E, +A]:
case Left(value: E)
case Right(value: A)
def map[B](f: A => B): Either[E, B] = this match
case Left(e) => Left(e)
case Right(a) => Right(f(a))
......@@ -11,5 +14,3 @@ sealed trait Either[+E, +A]:
def map2[EE >: E, B, C](other: Either[EE, B])(f: (A, B) => C): Either[EE, C] = ???
final case class Left[+E](value: E) extends Either[E, Nothing]
final case class Right[+A](value: A) extends Either[Nothing, A]
package errors
sealed trait Option[+A]:
enum Option[+A]:
case Some(get: A)
case None
def map[B](f: A => B): Option[B] = ???
def getOrElse[B >: A](default: => B): B = ???
......@@ -9,8 +12,6 @@ sealed trait Option[+A]:
def filter[B](f: A => Boolean): Option[A] = ???
final case class Some[+A](get: A) extends Option[A]
case object None extends Option[Nothing]
object Option:
def sequence[A](list: List[Option[A]]): Option[List[A]] = ???
package errors
object Team:
val persons = List(
Person("Dagobert", "Finanzabteilung"),
Person("Donald", "Spassabteilung"),
Person("Daniel", "R&D"),
)
// This allows us to write Some and None without prefix, as we would inside the Option enum
import Option.{None, Some}
type Team = (Person, Person)
val persons = List(
Person("Dagobert", "Finanzabteilung"),
Person("Donald", "Spassabteilung"),
Person("Daniel", "R&D"),
)
type Team = (Person, Person)
def lookup(name: String): Option[Person] =
persons.find(_.name == name).fold(None: Option[Person])(a => Some(a))
def lookup(name: String): Option[Person] =
persons.find(_.name == name).fold(None)(a => Some(a))
def getTeam(name1: String, name2: String): Option[Team] = ???
def getTeam(name1: String, name2: String): Option[Team] = ???
final case class Person(
name: String,
......
......@@ -6,10 +6,10 @@ import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class EitherSpec extends AnyFlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
val l1: Either[Int, Int] = Left(1)
val l2: Either[Int, Int] = Left(2)
val r1: Either[Int, Int] = Right(1)
val r2: Either[Int, Int] = Right(2)
val l1: Either[Int, Int] = Either.Left(1)
val l2: Either[Int, Int] = Either.Left(2)
val r1: Either[Int, Int] = Either.Right(1)
val r2: Either[Int, Int] = Either.Right(2)
"Either" should "have a map2 method which correctly combines two values" in {
l1.map2(r1)((a, b) => a+b) shouldBe l1
......
......@@ -5,6 +5,8 @@ import testutil.PendingIfUnimplemented
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import Option.{Some, None}
class OptionSpec extends AnyFlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
"An option" should "have a map function which transforms it's content" in {
Some(3).map(_ + 1) shouldBe Some(4)
......
......@@ -7,7 +7,7 @@ import org.scalatest.matchers.should.Matchers
class TeamSpec extends AnyFlatSpec with Matchers with AppendedClues with PendingIfUnimplemented {
"getTeam" should "return both persons ore none" in {
Team.getTeam("blar", "Dagobert") shouldBe None
Team.getTeam("Daniel", "Dagobert") shouldBe Some((Team.persons(2), Team.persons(0)))
getTeam("blar", "Dagobert") shouldBe None
getTeam("Daniel", "Dagobert") shouldBe Some((persons(2), persons(0)))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment