Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created May 23, 2012 13:26
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 rirakkumya/2775230 to your computer and use it in GitHub Desktop.
Save rirakkumya/2775230 to your computer and use it in GitHub Desktop.
[scala][play2] monadic fizzbuzz
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("[",",","]"))) 終了
}
}
# 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)
@rirakkumya
Copy link
Author

play2 + scalaでmanadicなfizzbuzzを書いてみた。

  • app/controller/Application.scala
  • app/controller/EitherAction.scala
  • conf/routes

をそれぞれ置き換えれば動きます。

EitherActionはこちらを参照してください。

https://gist.github.com/2626167

http://localhost:9000/1/100

で1から100までのfizzbuzzが表示されます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment