Created
March 17, 2017 19:50
-
-
Save botekchristophe/720179697f019e65e5df0b07beeec230 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class User(firstName: String, | |
nickName: Option[String] = None, | |
lastName: String, | |
happiness: Option[Boolean] = None, | |
age : Int) | |
val allUsers: List[User] = List( | |
User("Calvin", Some("Murican"), "Ference", happiness = Some(false), age = 78), | |
User("Max", Some("Mad"), "Millette-Coulombe", happiness = Some(true), age = 12), | |
User("Johann", lastName = "David", age = 26), | |
User("Dom", Some("Pink Scarf"), "Caron", Some(true), age = 32) | |
) | |
/** | |
* Check on user age. | |
*/ | |
val isOldEnough: User => Boolean = { _.age >= 18 } | |
/** | |
* Return full name for a user | |
*/ | |
val getFullName: User => String = {user => | |
s"${user.firstName}" + | |
s"${user.nickName.fold("")(nick => s" $nick")}" + | |
s" ${user.lastName}" | |
} | |
/** | |
* Get hello message according to user's mood | |
*/ | |
val getHelloMessage: Option[Boolean] => String = { | |
case Some(happy) => | |
if(happy) "We are happy too! have a good day." | |
else "Have a bad day ! you suck !" | |
case _ => "We have no clue if you are happy or not but have a good day !" | |
} | |
/** | |
* print hello message for specific user. | |
* | |
* @param authz authorization filter. True is not provided. | |
*/ | |
def printHelloMessage(authz: User => Boolean = User => true): List[User] => Unit = { | |
users => | |
users.filter(authz) | |
.map(user => (user.happiness, getFullName(user))) | |
.map { case (hapiness, name) => s"Hi $name ! ${getHelloMessage(hapiness)}" } | |
.foreach(println) | |
} | |
//call with filter on age | |
printHelloMessage(isOldEnough){ allUsers } | |
//call without filter | |
printHelloMessage(){ allUsers } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment