Skip to content

Instantly share code, notes, and snippets.

View seratch's full-sized avatar

Kazuhiro Sera seratch

View GitHub Profile
@seratch
seratch / AppInjector.scala
Created May 10, 2012 06:22 — forked from yamashiro/AppInjector.scala
ScalaでDIというかServiceLocator的な名状しがたい何か
trait ApiInjector {
//var twitter : TwitterApi = new TwitterApiImpl;
val twitter : TwitterApi = new TwitterApiImpl;
//他にもいろいろなサービス
}
@seratch
seratch / loanpattern.scala
Created May 14, 2012 15:25
Yet another loan pattern
import util.control.Exception._
object LoanPattern {
def using[R <: { def close() }, A](resource: R)(f: R => A): A = ultimately {
ignoring(classOf[Throwable]) apply resource.close()
} apply f(resource)
}
@seratch
seratch / readme.md
Created May 18, 2012 00:51
Finagle Developer Guide (December 15, 2011 Draft) の適当な和訳

細かい点で間違いなどあるかと思います。fork して直していただけると助かります。

Finagle is built using sbt. We've included a bootstrap script to ensure the correct version of sbt is used. To build:

Finagle は sbt を使ってビルドしています。私たちは確実に適切なバージョンの sbt を使うようにブートストラップ用のスクリプトを含めています。このように実行します。

    $ ./sbt test

import org.scalatest._
import org.scalatest.matchers._
import org.joda.time.DateTime
import java.util.concurrent.Executors
import akka.dispatch._
import akka.util.Duration
import akka.util.duration._
class RendezvousSpec extends FlatSpec with ShouldMatchers {
@seratch
seratch / iris_reader.scala
Created June 1, 2012 01:59
Iris data set reader example
import util.control.Exception._
def using[R <: { def close() }, A](resource: R)(f: R => A): A = ultimately {
ignoring(classOf[Throwable]) apply resource.close()
} apply f(resource)
case class Sepal(length: Double, width: Double)
case class Petal(length: Double, width: Double)
case class Species(name: String)
case class Iris(sepal: Sepal, petal: Petal, species: Species)
@seratch
seratch / build.sbt
Created June 10, 2012 09:54
Generalized type constraints in ScalikeJDBC
libraryDependencies ++= Seq(
"com.github.seratch" %% "scalikejdbc" % "1.3.0",
"org.slf4j" % "slf4j-simple" % "1.6.4",
"com.h2database" % "h2" % "[1.3,)"
)
initialCommands := """import scalikejdbc._
|Class.forName("org.h2.Driver")
|ConnectionPool.singleton("jdbc:h2:mem:tmp", "", "")
|implicit val session = DB.autoCommitSession
@seratch
seratch / sample.scala
Created June 11, 2012 00:22
Improved error message for generalized type constraints
class Foo[A, B <: Option[A]] {
@annotation.implicitNotFound(msg = "Should be Some!")
sealed abstract class =:=[From, To] extends (From => To) with Serializable
private[this] final val singleton_=:= = new =:=[Any,Any] { def apply(x: Any): Any = x }
object =:= {
implicit def tpEquals[A]: A =:= A = singleton_=:=.asInstanceOf[A =:= A]
}
type This = Foo[A, B]
@seratch
seratch / ThymeleafSupport.scala
Created June 28, 2012 09:19
ThymeleafSupport for Scalatra
import org.scalatra.ScalatraKernel
import org.thymeleaf.templateresolver._
import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.WebContext
trait ThymeleafSupport {
self: ScalatraKernel =>
lazy val thymeleafResolver: TemplateResolver = {
@seratch
seratch / KeyczarService.scala
Created July 10, 2012 13:06
KeyczarService.scala
import java.net.URLDecoder
import java.io.File
import org.keyczar.Crypter
case class KeyczarService(keypath: String) {
lazy val keyLocation = URLDecoder.decode(
new File(classOf[KeyczarService].getClassLoader.getResource(keypath).getFile).getAbsolutePath,
"UTF-8"
)
@seratch
seratch / Gemfile
Created August 10, 2012 11:14
Scaruby compose/and_then example
source "http://rubygems.org/"
gem 'scaruby'