Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
rirakkumya / gist:2694235
Created May 14, 2012 14:15
モナドはリラックマだ in scala

モナドはリラックマだ

モナドはコンテナだ。リラックマもコンテナ(中身入ってるし)だ。
だったら、モナドはリラックマだ。

と言う訳で、リラックマでモナド則書いてみた。

まず、リラックマの実装。リラックマは、皮と中身がある。そもそもいない時もある。
まさにMaybeだ。なので、Optionにしてみる。
えぇ手抜きですとも。

@rirakkumya
rirakkumya / Application.scala
Created May 7, 2012 05:54
play2 monadic + DSL programming
package controllers
import play.api._
import play.api.mvc._
import play.api.cache._
import play.api.Play.current
object Application extends Controller {
import EA._
def index(id:String) = EitherAction {req =>
@rirakkumya
rirakkumya / Application.scala
Created May 5, 2012 09:09
play2でAction内がどんどん入れ子になっていく問題を解決する方法。Action compositionにしてみた
package controllers
import play.api._
import play.api.mvc._
import play.api.cache._
import play.api.Play.current
object EA {
case class Bind[a1,a2](x:Either[a1,a2]) {
def #>>[b](f:a2 => Either[a1,b]):Either[a1,b] = x.right flatMap f
@rirakkumya
rirakkumya / sheep.scala
Created May 2, 2012 08:48
羊さんコード
case class Sheep(name:String,father:Option[Sheep],mother:Option[Sheep])
object SampleBase {
def father(sheep:Sheep):Option[Sheep] = sheep match {
case Sheep(_, s, _) => s
case _ => None
}
def mother(sheep:Sheep):Option[Sheep] = sheep match {
case Sheep(_, _, s) => s
case _ => None
@rirakkumya
rirakkumya / build.sbt
Created April 27, 2012 05:43
iTextとJFreeChartとscala-ioでpdfにグラフを出力しちゃうコード(日本語対応)
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-core" % "0.3.0"
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.3.0"
def foo[A,B](in:Option[A])(valid:PartialFunction[Option[A],Either[B,A]])(invalid:PartialFunction[Option[A],Either[B,A]])(other: =>Either[B,A]) = {
if(!valid.isDefinedAt(in) && !invalid.isDefinedAt(in)) {
other
}else{
(valid orElse invalid)(in)
}
}
def getFirstData(id:String) = {
foo(Cache.get(id)){
@rirakkumya
rirakkumya / build.sbt
Created April 18, 2012 02:12
iTextとscala-ioでpdfを出力しちゃうコード
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-core" % "0.3.0"
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.3.0"
scala> for{(x,y) <- Right((3,4)).right} yield (x,y)
<console>:10: error: constructor cannot be instantiated to expected type;
found : (T1, T2)
required: Either[Nothing,(Int, Int)]
for{(x,y) <- Right((3,4)).right} yield (x,y)
scala> Right((3,4)).right map {case (x,y) => (x,y)}
res118: Product with Either[Nothing,(Int, Int)] with Serializable = Right((3,4))
def index(id:String) = Action {
getFirstData(id)
}
private def getFirstData(id:String) = {
Cache.get(id) match {
case Some(id2) => getSecondData(id2)
case None => NotFound
}
}
private def getSecondData(id2:String) = {
@rirakkumya
rirakkumya / hexToStr.scala
Created April 13, 2012 01:35
16進文字列をAscii文字列に変換
import scala.annotation.tailrec
def hexToStr(s:String):String = {
@tailrec def r(target:String,result:String):String = {
target splitAt 2 match {
case (xx,xs) if !xx.isEmpty => r(xs,result + Integer.parseInt(xx,16).toChar)
case _ => result
}
}
r(s,"")
}