Skip to content

Instantly share code, notes, and snippets.

View seratch's full-sized avatar

Kazuhiro Sera seratch

View GitHub Profile
@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 / 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 / common_followers.rb
Created May 2, 2012 15:58 — forked from yusuke/common_followers.rb
print common follower screen names of specified twitter accounts
#!/usr/bin/env ruby
require "net/https"
require "uri"
API_BASE_URL = "https://api.twitter.com/1"
def get(url)
uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port)
@seratch
seratch / Application.scala
Created April 27, 2012 03:09
ScalikeJDBC Play20 plugin prototype
package controllers
import play.api._
import play.api.mvc._
import scalikejdbc._
object Application extends Controller {
def index = Action {
@seratch
seratch / dos_64kb_limit.scala
Created April 26, 2012 08:35
java.io.DataOutputStream 64KB limitation
import java.io._
val bao = new ByteArrayOutputStream
val dos = new DataOutputStream(bao)
val chank64kMinus1 = new StringBuilder
val limit = 1024 * 64 - 1
1 to limit foreach { _ => chank64kMinus1.append("a") }
dos.writeUTF(chank64kMinus1.toString)
println(limit + " bytes is acceptable.")
@seratch
seratch / plugins.sbt
Created April 1, 2012 05:34
Play20's project/plugins.sbt for IntelliJ IDEA users
// Comment to get more information during initialization
logLevel := Level.Warn
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
addSbtPlugin("play" % "sbt-plugin" % "2.0")
resolvers += "sbt-idea-repo" at "http://mpeltonen.github.com/maven/"
@seratch
seratch / MontyHallProblem.scala
Created March 31, 2012 00:55
Monty Hall problem
// http://en.wikipedia.org/wiki/Monty_Hall_problem
// http://d.hatena.ne.jp/hiratara/20120330/1333122309
import scala.util.Random._
case class Door(index: Int, isCar: Boolean)
def chooseDoor(doors: Seq[Door]): Door = doors(nextInt(doors.size))
object MontyHall {
@seratch
seratch / ThreadLocalForm.java
Created March 29, 2012 03:41
Working with Jersey's Form objects at outside of Resource classes
package example.threadlocal;
import com.sun.jersey.api.representation.Form;
public class ThreadLocalForm {
private static final ThreadLocal<Form> THREAD_LOCAL = new ThreadLocal<Form>();
public static void set(Form value) {
THREAD_LOCAL.set(value);
@seratch
seratch / build.sbt
Created March 8, 2012 03:06
Using Anorm 2.0 without Play Framework
scalaVersion := "2.9.1"
resolvers += "typesafe" at "http://repo.typesafe.com/typesafe/releases"
libraryDependencies ++= Seq(
"play" %% "anorm" % "2.0-RC4",
"com.github.seratch" %% "scalikejdbc" % "[0.5,)",
"org.hsqldb" % "hsqldb" % "[2,)"
)
@seratch
seratch / gist:1768947
Created February 8, 2012 12:24
type AnyVal cannot be used in a type pattern or isInstanceOf test
scala> "ss".isInstanceOf[Any]
res3: Boolean = true
scala> "ss".isInstanceOf[AnyVal]
<console>:8: error: type AnyVal cannot be used in a type pattern or isInstanceOf test
"ss".isInstanceOf[AnyVal]
^
scala>