fork download
  1. object Main extends App {
  2.  
  3. type MapFunc[T] = String => Option[T]
  4.  
  5. trait HttpMethod
  6. case object GET extends HttpMethod
  7. case object POST extends HttpMethod
  8.  
  9. def httpMethod: MapFunc[HttpMethod] = {
  10. case "GET" => Some(GET)
  11. case "POST" => Some(POST)
  12. case _ => None
  13. }
  14.  
  15. def string: MapFunc[String] = (v: String) => Some(v)
  16.  
  17. def bool: MapFunc[Boolean] = {
  18. case "1" => Some(true)
  19. case "0" => Some(false)
  20. case _ => None
  21. }
  22.  
  23. implicit class FuncTuple3[F,T1,T2,T3](tup: (F => T1,F => T2,F => T3)) {
  24. def mapList(vs: List[F]) = {
  25. vs match {
  26. case List(v1,v2,v3) => Some(tup._1(v1),tup._2(v2),tup._3(v3))
  27. case _ => None
  28. }
  29. }
  30. }
  31.  
  32. case class Request(method: HttpMethod, keepAlive: Boolean, url: String)
  33.  
  34.  
  35. println {
  36. (httpMethod, bool, string).mapList(List("GET", "1", "http://2...content-available-to-author-only...h.hk")) map {
  37. case (method, Some(keepAlive), Some(url)) =>Request(method getOrElse GET, keepAlive, url)
  38. case _ => None
  39. }
  40. }
  41.  
  42.  
  43. }
Success #stdin #stdout 0.4s 382080KB
stdin
Standard input is empty
stdout
Some(Request(GET,true,http://2...content-available-to-author-only...h.hk))