Skip to content

Instantly share code, notes, and snippets.

@Stefan-Wagner
Created February 24, 2012 19:55
Show Gist options
  • Save Stefan-Wagner/1903307 to your computer and use it in GitHub Desktop.
Save Stefan-Wagner/1903307 to your computer and use it in GitHub Desktop.
Spray service, for Scala meetup competition Feb-2012
package com.example
import cc.spray._
trait HelloService extends Directives {
def prime (x: Int): Boolean = {
if ((x < 2) || (x % 2 == 0)) false
((3 to math.sqrt(x).toInt by 2).find (n => (x % n == 0)) == None)
}
def cubeNsquare (x: Int) = {
// incomplete:
math.sqrt (x) * math.sqrt (x) == x
}
def keywordSearch (words: List[String]): String = {
if (words.contains ("Spain"))
"Pesetas"
else if (words.contains ("banana"))
"yellow"
else if (words.contains ("Minister"))
"Cameron"
else if (words.contains ("Foo"))
"Bar"
else "Paris"
}
def mapWordsNnumbers (words: List[String], nums: List[Int]): String = {
if (words.contains ("largest:"))
"" + nums.max
else if (words.contains ("cube:"))
"" + nums.find (cubeNsquare).getOrElse (nums(0))
else if (words.contains ("primes:"))
"" + nums.find (prime).getOrElse (42)
else if (words.contains ("multiplied"))
"" + nums.product
else if (words.contains ("plus"))
"" + nums.sum
else "44"
}
def analyze (s: String) : String = {
println ("\t\ts:\t" + s)
val postColon = s.replace ("[^:]*:", "")
val words = postColon.split ("[, ]")
println ("\t\tpostColon:\t" + postColon)
println ("\t\twords:\t" + words.mkString (" - "))
// val numbers = words.filter (_.matches ("[0-9]\\+"))
val numbers = words.filter (_.matches ("[0-9][0-9]*"))
println ("\t\tnums:\t" + numbers.mkString (" # "))
val res = if (numbers.isEmpty) keywordSearch (words.toList)
else mapWordsNnumbers (words.toList, numbers.map (_.toInt).toList)
println (res + "\n")
res
}
// later added for testing without server app:
// val requests = io.Source.fromFile ("requests.txt").getLines
// val results = requests.toList.map (analyze)
val helloService = {
(path ("") & parameter ("q")) {
q => {
get {
println("-----> " + q)
val result = analyze (q)
println("=====> " + result)
println ()
_.complete (result)
}
}
}
}
}
/* later added for testing without testframework app:
object Driver extends App {
val requests = io.Source.fromFile ("requests.txt").getLines
val results = requests.toList.map (analyze)
*/
}
c8b859a0: which of the following numbers is both a square and a cube: 81, 502, 1936, 579
d4acac10: what is 19 multiplied by 10
e09e9580: what is 18 multiplied by 9
ec8f46b0: what is 16 multiplied by 17
f8812810: which of the following numbers is both a square and a cube: 46656, 960
04848200: which of the following numbers is the largest: 84, 285
10770d60: which of the following numbers is both a square and a cube: 606, 744, 1089, 961
1c759270: what is 11 multiplied by 6
28679220: which of the following numbers is both a square and a cube: 630, 784
34579620: which of the following numbers is both a square and a cube: 819, 25, 900, 722
404a9d80: which of the following numbers is the largest: 964, 1, 406, 20
4c395510: which of the following numbers is the largest: 373, 68
5828ee70: which of the following numbers is the largest: 58, 5
641830c0: which of the following numbers is both a square and a cube: 64, 866
70093070: what is 10 multiplied by 4
7bfe3810: which of the following numbers is both a square and a cube: 576, 459, 117649, 618
87ef4920: what is 14 multiplied by 5
35ce66f0: which of the following numbers is the largest: 464, 37, 213, 17
42a55120: which of the following numbers is the largest: 82, 78
4e973110: what is 9 multiplied by 7
5a899a20: what is 0 plus 6
66808c90: which of the following numbers is the largest: 663, 9, 77, 10
7286aa40: which of the following numbers is both a square and a cube: 625, 948, 441, 758
7e7b4c40: which of the following numbers is the largest: 998, 901, 86, 97
8a925000: which of the following numbers are primes: 61, 929
969cfd20: who is the Prime Minister of Great Britain
a3b725a0: which of the following numbers is both a square and a cube: 46656, 946
afae18b0: what is 13 multiplied by 2
bba370c0: which of the following numbers is the largest: 8, 696
c7960fb0: which of the following numbers is the largest: 12, 96, 47, 248
d38c4850: what currency did Spain use before the Euro
df8583c0: which of the following numbers is the largest: 363, 710, 3, 12
eb7adbe0: which of the following numbers are primes: 163, 383, 676, 750
f76e06f0: which of the following numbers are primes: 450, 769, 5, 263
035ffc70: which of the following numbers are primes: 23, 607, 991, 167
0f539cc0: who is the Prime Minister of Great Britain
1b43f010: what is 10 multiplied by 6
273b3940: which of the following numbers are primes: 868, 337
33303830: which of the following numbers is the largest: 59, 219, 951, 3
3f262370: who is the Prime Minister of Great Britain
container:start-----> 4b16d2e0: what colour is a banana
62f9ffe0: who is the Prime Minister of Great Britain
6f2d2f00: which of the following numbers is both a square and a cube: 36, 641
7b5506d0: who is the Prime Minister of Great Britain
874a0c30: which of the following numbers is the largest: 61, 931
934bd5d0: what is 7 multiplied by 6
9f3a42a0: which of the following numbers is the largest: 696, 10
ab334a10: who is the Prime Minister of Great Britain
b7270550: what colour is a banana
c4e3ecc0: which of the following numbers are primes: 419, 372
d0d41e60: what colour is a banana
0c9ff390: which of the following numbers is both a square and a cube: 2304, 539
1908d280: what currency did Spain use before the Euro
250350d0: which of the following numbers is both a square and a cube: 213, 81
310822c0: which of the following numbers is the largest: 73, 863
3cfc87f0: which of the following numbers is both a square and a cube: 1296, 643
49058670: which of the following numbers is both a square and a cube: 72, 729
5502b1f0: which of the following numbers is both a square and a cube: 646, 169, 841, 44
60f5e630: which of the following numbers are primes: 487, 359, 462, 907
6ce79470: which of the following numbers is the largest: 800, 690, 80, 16
78dfae40: which of the following numbers is both a square and a cube: 32, 16
84d47f90: which of the following numbers is both a square and a cube: 625, 559
929f3f80: which of the following numbers is both a square and a cube: 1, 685
9e9b1f10: which of the following numbers are primes: 151, 183
ac6c6c80: what colour is a banana
b85dcd00: which of the following numbers are primes: 127, 638
c44e7be0: what is 10 multiplied by 10
d0409950: what is 6 multiplied by 9
dc33ec60: what is 16 multiplied by 7
e82567d0: who is the Prime Minister of Great Britain
f41e1cd0: what colour is a banana
00175470: which of the following numbers are primes: 229, 650
ibmux:~/proj/mini/scala/zeuchs/competition/spray-template > umts
[sudo] password for stefan:
Looking for target devices ...
No devices in target mode or class found
Looking for default devices ...
No default device found. Is it connected? Bye.
ibmux:~/proj/mini/scala/zeuchs/competition/spray-template > umts
Looking for target devices ...
No devices in target mode or class found
Looking for default devices ...
No default device found. Is it connected? Bye.
ibmux:~/proj/mini/scala/zeuchs/competition/spray-template > cat requests.txt
c8b859a0: which of the following numbers is both a square and a cube: 81, 502, 1936, 579
d4acac10: what is 19 multiplied by 10
e09e9580: what is 18 multiplied by 9
ec8f46b0: what is 16 multiplied by 17
f8812810: which of the following numbers is both a square and a cube: 46656, 960
04848200: which of the following numbers is the largest: 84, 285
10770d60: which of the following numbers is both a square and a cube: 606, 744, 1089, 961
1c759270: what is 11 multiplied by 6
28679220: which of the following numbers is both a square and a cube: 630, 784
34579620: which of the following numbers is both a square and a cube: 819, 25, 900, 722
404a9d80: which of the following numbers is the largest: 964, 1, 406, 20
4c395510: which of the following numbers is the largest: 373, 68
5828ee70: which of the following numbers is the largest: 58, 5
641830c0: which of the following numbers is both a square and a cube: 64, 866
70093070: what is 10 multiplied by 4
7bfe3810: which of the following numbers is both a square and a cube: 576, 459, 117649, 618
87ef4920: what is 14 multiplied by 5
35ce66f0: which of the following numbers is the largest: 464, 37, 213, 17
42a55120: which of the following numbers is the largest: 82, 78
4e973110: what is 9 multiplied by 7
5a899a20: what is 0 plus 6
66808c90: which of the following numbers is the largest: 663, 9, 77, 10
7286aa40: which of the following numbers is both a square and a cube: 625, 948, 441, 758
7e7b4c40: which of the following numbers is the largest: 998, 901, 86, 97
8a925000: which of the following numbers are primes: 61, 929
969cfd20: who is the Prime Minister of Great Britain
a3b725a0: which of the following numbers is both a square and a cube: 46656, 946
afae18b0: what is 13 multiplied by 2
bba370c0: which of the following numbers is the largest: 8, 696
c7960fb0: which of the following numbers is the largest: 12, 96, 47, 248
d38c4850: what currency did Spain use before the Euro
df8583c0: which of the following numbers is the largest: 363, 710, 3, 12
eb7adbe0: which of the following numbers are primes: 163, 383, 676, 750
f76e06f0: which of the following numbers are primes: 450, 769, 5, 263
035ffc70: which of the following numbers are primes: 23, 607, 991, 167
0f539cc0: who is the Prime Minister of Great Britain
1b43f010: what is 10 multiplied by 6
273b3940: which of the following numbers are primes: 868, 337
33303830: which of the following numbers is the largest: 59, 219, 951, 3
3f262370: who is the Prime Minister of Great Britain
4b16d2e0: what colour is a banana
62f9ffe0: who is the Prime Minister of Great Britain
6f2d2f00: which of the following numbers is both a square and a cube: 36, 641
7b5506d0: who is the Prime Minister of Great Britain
874a0c30: which of the following numbers is the largest: 61, 931
934bd5d0: what is 7 multiplied by 6
9f3a42a0: which of the following numbers is the largest: 696, 10
ab334a10: who is the Prime Minister of Great Britain
b7270550: what colour is a banana
c4e3ecc0: which of the following numbers are primes: 419, 372
d0d41e60: what colour is a banana
0c9ff390: which of the following numbers is both a square and a cube: 2304, 539
1908d280: what currency did Spain use before the Euro
250350d0: which of the following numbers is both a square and a cube: 213, 81
310822c0: which of the following numbers is the largest: 73, 863
3cfc87f0: which of the following numbers is both a square and a cube: 1296, 643
49058670: which of the following numbers is both a square and a cube: 72, 729
5502b1f0: which of the following numbers is both a square and a cube: 646, 169, 841, 44
60f5e630: which of the following numbers are primes: 487, 359, 462, 907
6ce79470: which of the following numbers is the largest: 800, 690, 80, 16
78dfae40: which of the following numbers is both a square and a cube: 32, 16
84d47f90: which of the following numbers is both a square and a cube: 625, 559
929f3f80: which of the following numbers is both a square and a cube: 1, 685
9e9b1f10: which of the following numbers are primes: 151, 183
ac6c6c80: what colour is a banana
b85dcd00: which of the following numbers are primes: 127, 638
c44e7be0: what is 10 multiplied by 10
d0409950: what is 6 multiplied by 9
dc33ec60: what is 16 multiplied by 7
e82567d0: who is the Prime Minister of Great Britain
f41e1cd0: what colour is a banana
00175470: which of the following numbers are primes: 229, 650
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment