新しく 2.10 から(?) dependent method types っていう機能が Scala に入るらしいので、最新版をゴニョゴニョしてみた。
2.9.1で同じことやろうとすると def withFoo(foo: Foo): foo.Bar = foo.f という部分がコンパイル通らないはず。fooという 引数 の抽象型であるBarに依存した型だからっていう感じ?
しかし、機能の概念はなんとなくぼんやりと、把握はできたが、まだどういったときに役に立つのかわかっていない(・ω・`)
| object RetryUtil { | |
| case class RetryException(throwables: List[Throwable]) extends Exception | |
| def retry[T](retryLimit: Int, retryInterval: Int, shouldCatch: Throwable => Boolean)(f: => T): T = { | |
| // @annotation.tailrec | |
| def _retry( errors: List[Throwable], f: => T):T = { | |
| try { | |
| f | |
| } catch { |
| import com.twitter.finagle.http.path._ | |
| import com.twitter.finagle.http.service.RoutingService | |
| import com.twitter.finagle.http.{Request, Response, RichHttp, Http} | |
| import com.twitter.finagle.{Service, SimpleFilter} | |
| import org.jboss.netty.handler.codec.http._ | |
| import org.jboss.netty.handler.codec.http.HttpResponseStatus._ | |
| import org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1 | |
| import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer | |
| import org.jboss.netty.util.CharsetUtil.UTF_8 | |
| import com.twitter.util.Future |
| class Hoge() {}; var hoge = List(new Hoge()); hoge foldLeft(Map[String, Hoge]()) ( (map:Map[String,Hoge], hoge:Hoge) => map + ("hoge" -> hoge) ); |
| 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) = { |
| class Respond extends Service[Request, Response] with Logger { | |
| def apply(request: Request) = { | |
| try { | |
| request.method -> Path(request.path) match { | |
| case GET -> Root / "todos" => Future.value { | |
| val data = Todos.allAsJson | |
| debug("data: %s" format data) | |
| Responses.json(data, acceptsGzip(request)) | |
| } | |
| case GET -> Root / "todos" / id => Future.value { |
| def connect = { | |
| log("connect") { // connect start | |
| // ... 何かの処理 ... | |
| log("login") { // connect : login start | |
| // ... 何かの処理 ... | |
| } // connect : login end | |
| // ... 何かの処理 ... | |
| } // connect end | |
| } |
| def eith[R](arg: Either[Throwable, Any] ) : R = { | |
| arg match { | |
| case Left(e) => throw e | |
| case Right(o:Either[Throwable, Any]) => eith(o) | |
| case Right(o:R) => o | |
| } | |
| } |
| List(1,2,3,1).zip(List(1,1,1,1)).foldLeft(Map[Int,Int]()) { (x, y) => x.get(y._1) match { case Some(v) => x + Pair(y._1, (v + y._2)) case None => x + y }} |
新しく 2.10 から(?) dependent method types っていう機能が Scala に入るらしいので、最新版をゴニョゴニョしてみた。
2.9.1で同じことやろうとすると def withFoo(foo: Foo): foo.Bar = foo.f という部分がコンパイル通らないはず。fooという 引数 の抽象型であるBarに依存した型だからっていう感じ?
しかし、機能の概念はなんとなくぼんやりと、把握はできたが、まだどういったときに役に立つのかわかっていない(・ω・`)
| class Respond extends Service[Request, Response] with Logger { | |
| def apply(request: Request) = { | |
| try { | |
| request.method -> Path(request.path) match { | |
| case GET -> Root / "todos" => Future.value { | |
| val data = Todos.allAsJson | |
| debug("data: %s" format data) | |
| Responses.json(data, acceptsGzip(request)) | |
| } | |
| case GET -> Root / "todos" / id => Future.value { |