Skip to content

Instantly share code, notes, and snippets.

View j5ik2o's full-sized avatar

Junichi Kato j5ik2o

View GitHub Profile
import scalaz._, Scalaz._
sealed trait MyOption[+A]
case class MySome[A](value: A) extends MyOption[A]
case object MyNone extends MyOption[Nothing]
object MyOption {
implicit object MyOptionFunctor extends Functor[MyOption] {
@j5ik2o
j5ik2o / not_tailrec.scala
Created June 26, 2012 21:02 — forked from xuwei-k/not_tailrec.scala
リトライハンドラーの殴り書き
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 {
@j5ik2o
j5ik2o / rest.scala
Created February 24, 2012 15:54 — forked from stonegao/rest.scala
Basic RESTful service with Finagle
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 {
@j5ik2o
j5ik2o / gist:1476354
Created December 14, 2011 12:11 — forked from makotan/gist:1476310
Either[L,Either[L,R]]のように多段になる場合にRだけを取得する
object Loan {
def using[A <: { def close() }, B](resource: A)(func: A => B): Either[Exception, B] =
try {
Right(func(resource))
} catch {
case e: Exception => Left(e)
} finally {
if (resource != null) resource.close()
}
@j5ik2o
j5ik2o / gist:1429210
Created December 4, 2011 04:50 — forked from tyano/gist:1429205
List(1, 2, 3, 1) を Map(1 -> 2, 2 -> 1, 3 -> 1)
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 }}