Determining the HTTP status code to return for a request
You should learn more about partial functions. Everything you need to know is written in the documentation.

It is a structure that allows you to do a basic check on something and if it succeed, it does something. It is composed of two methods :


apply(v1: A): B
isDefinedAt(x: A): Boolean


It applies the apply method on your input parameter only if the isDefinedAt method returned true when called with your input parameter.

The great thing about partial functions is that you can chain them easily with the andThen, compose and applyOrElse operators. Also, they can be written very easily by using the case syntax : 

val divideByTwo: PartialFunction[Int, Int] = { case d: Int if d%2 == 0 =&gt; d/2 }


But there is another way to write it :

val divideByTwo = new PartialFunction[Int, Int] {
  def apply(d: Int) = d / 2
  def isDefinedAt(d: Int) = (d%2 == 0)
}


Here is a great article about it.