Skip to content

Commit

Permalink
try exercises
Browse files Browse the repository at this point in the history
  • Loading branch information
ssahayam-zd committed Oct 11, 2023
1 parent 51a9bc2 commit 8747b19
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
30 changes: 19 additions & 11 deletions src/main/scala/introcourse/level06/TryExercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object TryExercises {
*
* Hint: Use `Try` and `parseInt`
*/
def parseIntSafe(str: String): Try[Int] = ???
def parseIntSafe(str: String): Try[Int] = Try(parseInt(str))

/**
* scala> parseBooleanSafe("true")
Expand All @@ -56,7 +56,7 @@ object TryExercises {
*
* Hint: Use .toBoolean to convert a String to a Boolean
**/
def parseBooleanSafe(str: String): Try[Boolean] = ???
def parseBooleanSafe(str: String): Try[Boolean] = Try(str.toBoolean)


/**
Expand All @@ -71,7 +71,7 @@ object TryExercises {

def increment(str: String): Try[Int] = {
val errorOrInt: Try[Int] = parseIntSafe(str)
???
errorOrInt.map(n => n + 1)
}

/**
Expand All @@ -85,8 +85,8 @@ object TryExercises {

def tryToEither[A](tryA: Try[A]): Either[TryError, A] =
tryA match {
case Success(a) => ???
case Failure(throwable) => ???
case Success(a) => Right(a)
case Failure(throwable) => Left(TryError(throwable.getMessage))
}

/**
Expand All @@ -99,8 +99,8 @@ object TryExercises {
*/
def tryToOption[A](tryA: Try[A]): Option[A] =
tryA match {
case Success(a) => ???
case Failure(throwable) => ???
case Success(a) => Some(a)
case Failure(_) => None
}

/**
Expand All @@ -110,7 +110,7 @@ object TryExercises {
* 3. hasDirectReports: Boolean
*/

case class Employee()
case class Employee(name: String, age: Int, hasDirectReports: Boolean)

/**
* Now remove `import TryTestTypes.*` from `TryExercisesTest.scala`
Expand All @@ -135,8 +135,16 @@ object TryExercises {
*/
def mkEmployee(csv: String): Either[TryError, Employee] =
csv.split(",") match {
case Array(nameStr, ageStr, hasDirectReportsStr) => ???
case _ => ???
case Array(nameStr, ageStr, hasDirectReportsStr) =>
val tryEmployee: Try[Employee] =
for {
age <- parseIntSafe(ageStr)
hasDirectReports <- parseBooleanSafe(hasDirectReportsStr)
} yield Employee(nameStr, age, hasDirectReports)

tryToEither(tryEmployee)

case _ => Left(TryError("CSV has wrong number of fields. Expected 3."))
}

/**
Expand All @@ -147,7 +155,7 @@ object TryExercises {
*/
def fileToEmployees(filename: String): List[Either[TryError, Employee]] = {
val lines: List[String] = io.Source.fromFile(filename).getLines().toList
???
lines.map(mkEmployee)
}

}
3 changes: 1 addition & 2 deletions src/test/scala/introcourse/level06/TryExercisesTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import org.scalatest.funspec.AnyFunSpec
import scala.util.{Failure, Success}

class TryExercisesTest extends AnyFunSpec with TypeCheckedTripleEquals {
// TODO: Remove this import once you've defined Employee data type in TryExercuses
import TryTestTypes.*


describe("parseIntSafe") {

Expand Down

0 comments on commit 8747b19

Please sign in to comment.