Created
May 23, 2012 13:26
-
-
Save rirakkumya/2775230 to your computer and use it in GitHub Desktop.
[scala][play2] monadic fizzbuzz
This file contains 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._ | |
import EA._ | |
object Application extends Controller { | |
implicit def either2Bind(s:Either[String,Int]) = new Bind(s) | |
implicit def int2Bind(x:Int) = new Bind[String,Int](Right(x)) | |
def setup = (div:Int,str:String) => (i:Int) => if(i % div == 0) str 終了 else i 継続 | |
def fizz = setup(3,"Fizz") | |
def buzz = setup(5,"Buzz") | |
def fizzbuzz = (i:Int) => (fizz(i),buzz(i)) match { | |
case (Left(x),Left(y)) => x + y 終了 | |
case _ => i 継続 | |
} | |
def index(start:Int,end:Int) = EitherAction {_ => | |
val result = (start to end) map {_ >>= fizzbuzz >>= fizz >>= buzz merge} | |
Ok("%d => %d\n%s".format(start,end,result.mkString("[",",","]"))) 終了 | |
} | |
} |
This file contains 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
# Routes | |
# This file defines all application routes (Higher priority routes first) | |
# ~~~~ | |
# Home page | |
GET /:start/:end controllers.Application.index(start:Int,end:Int) | |
# Map static resources from the /public folder to the /assets URL path | |
GET /assets/*file controllers.Assets.at(path="/public", file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
play2 + scalaでmanadicなfizzbuzzを書いてみた。
をそれぞれ置き換えれば動きます。
EitherActionはこちらを参照してください。
https://gist.github.com/2626167
http://localhost:9000/1/100
で1から100までのfizzbuzzが表示されます。