Skip to content
Snippets Groups Projects
Commit 6d7cb1e9 authored by Felix Herrmann's avatar Felix Herrmann
Browse files

add either/option exercises

parent 3cb9f067
No related branches found
No related tags found
No related merge requests found
package errors
sealed trait Either[+E, +A] {
def map[B](f: A => B): Either[E, B] = this match {
case Left(e) => Left(e)
case Right(a) => Right(f(a))
}
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = this match{
case Left(e) => Left(e)
case Right(a) => f(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] {
def map[B](f: A => B): Option[B] = this match {
case None => None
case Some(a) => Some(f(a))
}
def getOrElse[B >: A](default: => B): B = ???
def flatMap[B](f: A => Option[B]): Option[B] = ???
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"),
)
type Team = (Person, Person)
def lookup(name: String): Option[Person] =
persons.find(_.name == name).fold(None: Option[Person])(a => Some(a))
def getTeam(name1: String, name2: String): Option[Team] = ???
}
final case class Person(
name: String,
department: String,
)
package errors
import org.scalatest._
import testutil.PendingIfUnimplemented
class EitherSpec extends FlatSpec 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)
"Either" should "have a map2 method which correctly combines two values" in {
l1.map2(r1)((a, b) => a+b) shouldBe l1
r1.map2(l1)((a, b) => a+b) shouldBe l1
r1.map2(r1)((a, b) => a+b) shouldBe r2
l1.map2(l2)((a, b) => a+b) shouldBe l1
}
}
package errors
import org.scalatest._
import testutil.PendingIfUnimplemented
class OptionSpec extends FlatSpec 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)
}
it should "have a getOrElse method which returns its content or a default value when it's empty" in {
Some(3).getOrElse(4) shouldBe 3
None.getOrElse(4) shouldBe 4
}
it should "have a flatMap which works according to the monad laws" in {
Some(3).flatMap(x => Some(x + 3)) shouldBe Some(6)
(None: Option[Int]).flatMap(x => Some(x + 3)) shouldBe None
}
it should "have a filter method which removes its content according to a give predicate" in {
Some(3).filter(_ % 2 == 0) shouldBe None
Some(4).filter(_ % 2 == 0) shouldBe Some(4)
}
it should "have a sequence method" in {
Option.sequence(List.empty[Option[Int]]) shouldBe Some(Nil)
Option.sequence(List(Some(1), Some(2))) shouldBe Some(List(1,2))
Option.sequence(List(Some(1), None)) shouldBe None
}
}
package errors
import org.scalatest._
import testutil.PendingIfUnimplemented
class TeamSpec extends FlatSpec 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)))
}
}
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