Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created July 28, 2015 14:00
Show Gist options
  • Save Centaur/ff7374dcddc0da4ea629 to your computer and use it in GitHub Desktop.
Save Centaur/ff7374dcddc0da4ea629 to your computer and use it in GitHub Desktop.
options2map
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