Skip to content

Instantly share code, notes, and snippets.

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 / build.sbt
Created February 29, 2012 01:45
scala-ioとscalacheckでランダムデータを作っちゃうコード
resolvers += "Scala Tools Snapshots" at "http://scala-tools.org/repo-snapshots/"
libraryDependencies += "org.scalaz" %% "scalaz-core" % "6.0.4"
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-core" % "0.3.0"
libraryDependencies += "com.github.scala-incubator.io" %% "scala-io-file" % "0.3.0"
libraryDependencies += "org.scala-tools.testing" %% "scalacheck" % "1.9"
@rirakkumya
rirakkumya / gist:2724922
Created May 18, 2012 12:06
List[Either[Throwable,_]]のリスト中にLeftが1個以上あったら最初のLeftを返し、Leftが1個もなければ、Rightを1個のリストにまとめて返すコード
def f[a] = (d:List[Either[Throwable,a]]) =>
d.collect{case Left(x) => x}.headOption.toLeft(d.map{case Right(x) => x})
scala> Some(List(Right("4"),Right("3"))) map f
res11: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Right(List(4, 3)))
scala> Some(List(Right("4"),Left(new RuntimeException),Right("3"))) map f
res12: Option[Product with Either[Throwable,List[java.lang.String]] with Serializable] = Some(Left(java.lang.RuntimeException))
scala> Some(List(Right("4"),Left(new RuntimeException("a")),Right("3"),Left(new RuntimeException("b")))) map f
Addr.zip(Address("100-0000")) //-> Addr.郵便番号 = 100-0000
import TrainOps._
import TrainService._
object TrainService {
type ANSWER = Either[String, List[TrainStatus]]
trait Train[A] {
def status(x: A): ANSWER
}
case class TrainStatus(name: String, Status: String)
case class 運行状況(area: String) {
case class Address(zip:List[String])
object Addr {
type 住所 = Address
type 郵便番号 = String
def zip:住所 => List[郵便番号] = _.zip
}
List(Address(List("100-0001","100-0002"))) map Addr.zip
//->List[List[Addr.郵便番号]] = List(List(100-0001, 100-0002))
List(Address(List("100-0001","100-0002"))) flatMap Addr.zip
// ->List[Addr.郵便番号] = List(100-0001, 100-0002)
import Addr._
def lift(f: 住所 => 郵便番号): Option[住所] => Option[郵便番号] = _ map f
lift(Addr.zip)(Some(Address("100-1000")))
// -> Option[Addr.郵便番号] = Some(100-1000)
List(Address("100-0000"),Address("200-0000")) map Addr.zip 
//-> List[Addr.郵便番号] = List(100-0000, 200-0000)
Some(Address("100-0000")) map Addr.zip
//-> Option[Addr.郵便番号] = Some(100-0000)