Created
July 28, 2015 14:00
-
-
Save Centaur/ff7374dcddc0da4ea629 to your computer and use it in GitHub Desktop.
options2map
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
object Constants { | |
val MaterialSep = "," | |
} | |
case class Client(need: Option[String], product: Option[String]) | |
def optionsIntoMap(x1: (String, Option[String]), | |
x2: (String, Option[String])): Map[String, List[String]] = { | |
List(x1, x2).collect { | |
case (name, Some(str)) => name -> str.split(Constants.MaterialSep).toList | |
} toMap | |
} | |
def handleClient(client: Client): Map[String, List[String]] = | |
optionsIntoMap("material" -> client.need, "product" -> client.product) | |
assert(handleClient(Client(Some("need,need2"), None)) == | |
Map("material" -> List("need", "need2"))) | |
assert(handleClient(Client(None, Some("product,product2"))) == | |
Map("product" -> List("product", "product2"))) | |
assert(handleClient(Client(Some("need,need2"), Some("product,product2"))) == | |
Map( | |
"material" -> List("need", "need2"), | |
"product" -> List("product", "product2") | |
)) | |
assert(handleClient(Client(None, None)) == Map()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment