forked from wjlow/intro-to-scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionExercises3.scala
46 lines (41 loc) · 1.38 KB
/
OptionExercises3.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fundamentals.level03
import fundamentals.level03.OptionExercises2.{HumanId, Job, JobId, findHumanById, findJobById}
/**
* These exercises mirror the ones from `OptionExercises2.scala`,
* they are for the purpose of teaching for-comprehension, which is very useful for working with `Option`.
*/
object OptionExercises3 {
/**
* scala> findJobIdByHumanIdUsingFor(1)
* = None
*
* scala> findJobIdByHumanIdUsingFor(2)
* = Some(1)
*/
def findJobIdByHumanIdUsingFor(humanId: HumanId): Option[JobId] = ???
// for {
// human <- ??? // Find a function of type HumanId => Option[Human]
// jobId <- ??? // Find a function of type Human => Option[JobId]
// } yield jobId
/**
* scala> findJobByHumanIdUsingFor(2)
* = Some(Job("Teacher", "Expert in their field"))
*
* Hint: Use findJobIdByHumanIdUsingFor
*/
def findJobByHumanIdUsingFor(humanId: HumanId): Option[Job] = ???
// for {
// jobId <- ??? // Find a function of type HumanId => Option[JobId]
// job <- ??? // Find a function of type JobId => Option[Job]
// } yield job
/**
* scala> findJobNameByHumanIdUsingFor(2)
* = Some("Teacher")
*
* scala> findJobNameByHumanIdUsingFor(1)
* = None
*
* Hint: Use `findJobByHumanIdUsingFor` and for comprehension
*/
def findJobNameByHumanIdUsingFor(humanId: HumanId): Option[String] = ???
}