Skip to content

Instantly share code, notes, and snippets.

@AndreaCrotti
Created October 18, 2012 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreaCrotti/3915169 to your computer and use it in GitHub Desktop.
Save AndreaCrotti/3915169 to your computer and use it in GitHub Desktop.
scala-play solution to scala-coding dojo 18-10-2012
package controllers
import play.api._
import play.api.mvc._
object Application extends Controller {
def is_cube(a: Int): Boolean = {
(1 to (a / 2)).exists(x => x * x * x == a)
}
def is_square(a: Int): Boolean = {
(1 to (a / 2)).exists(x => x * x == a)
}
def extract_square_and_cube(xs: List[Int]): String = {
xs.filter(x => (is_square(x) && is_cube(x))).headOption.mkString
}
def number_list_to_ints(xs: String): List[Int] = {
xs.split(", ").toList map (_.toInt)
}
// implicit parameter, something magically passed
def index = Action { request =>
val plus_reg = """.*?(\d+) plus (\d+)""".r
val largest = """.*?largest: (\d+.*)""".r
val mult = """.*?(\d+) multiplied by (\d+)""".r
val sq_cube = """.*?square and a cube: (\d+.*)""".r
val resp = request.queryString.get("q") map {
seq => seq.head match {
case plus_reg(a, b) => a.toInt + b.toInt
case largest(numbers) => number_list_to_ints(numbers).max
case mult(a, b) => a.toInt * b.toInt
case sq_cube(numbers) => extract_square_and_cube(number_list_to_ints(numbers))
case _ => Logger.info("Not handled thing" + request.queryString.get("q")); 0
}
}
Logger.info("Query " + request.queryString.get("q") + "\tResponse " + resp.get)
Ok("" + resp.get)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment