Exercises:
-
Write a function called
parsePhoneNumber
that takes as an argumentphoneNumber: String
and returns aOption[String]
representing a phone number. Why might we want to useString
here and notInt
? -
Write a better
max
function that returns anOption[Int]
given an argument of typeList[Int]
. How would you write this for a list ofString
? How about aList[Boolean]
? Would the implementations differ in any meaninful way? -
Define a case class
User
that represent a user in our imaginary app. AUser
should have anid
aname
and an optionalemail
. -
Create a list of
Users
- call itusers
- that contains 2User
s. Let's pretend that this is some database of users for our app. -
Write a function
findUser(id: String)
that searches for aUser
in our list and returns anOption[User]
, where it returnsNone
if ourusers
list doesn't contain aUser
with thatid
. -
Write a function
getEmailDomain(id: String)
that returns anOption[String]
. This function should look up the user in ourusers
list and, if there is anemail
defined, extract the domain (the part after the '@' in the email) and return that as aSome
, otherwise it should return aNone
. What is the return type of this? You may end up with anOption[Option[String]]
- why is this? Let's discuss.