Created
October 18, 2012 22:33
-
-
Save AndreaCrotti/3915169 to your computer and use it in GitHub Desktop.
scala-play solution to scala-coding dojo 18-10-2012
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
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