Skip to content

Instantly share code, notes, and snippets.

@ogatatsu
ogatatsu / ProxyT.scala
Created December 7, 2011 08:26
ProxyT
abstract class ProxyT[T <: ProxyT[T]](implicit cm: ClassManifest[T]) {
val underlying: Any
override def hashCode: Int = underlying.hashCode
override def equals(other: Any): Boolean = other match {
case that: T if(cm.erasure.isInstance(that)) => underlying.equals(that.underlying)
case _ => false
}
}
@ogatatsu
ogatatsu / RichEither.scala
Created November 21, 2011 11:29
RichEither
class RichEither[A](e: Either[Throwable, A]) {
def success: A = e match {
case Right(r) => r
case Left(l) => throw l
}
}
implicit def eitherWrapper[A](e: Either[Throwable, A]) = new RichEither(e)
@ogatatsu
ogatatsu / HomLangConverter.scala
Created November 2, 2011 17:12
ほむ語変換機
abstract class LangConverter {
val `0`: String
val `1`: String
val sep: String
private def binaryStringToLang(s: String): String = {
s.map {
case '0' => `0`
case '1' => `1`
@ogatatsu
ogatatsu / CoffeeScript.scala
Created August 27, 2011 11:58
Rhinoを利用したcoffeescriptコンパイラ
import java.io._
import java.net._
import org.mozilla.javascript._
class CoffeeScript private(reader: Reader) {
private val _compile = {
val cx = Context.enter()
//generated bytecode for method exceeds 64K limit.回避
/*
import scala.collection.JavaConversions.JMapWrapperLike
import scala.collection.generic.{SortedMapFactory, CanBuildFrom}
import scala.collection
class TreeMap[A, B](implicit val ordering: Ordering[A])
extends JMapWrapperLike[A, B, TreeMap[A, B]]
with collection.SortedMap[A, B]
with collection.SortedMapLike[A, B, TreeMap[A, B]] {
@ogatatsu
ogatatsu / Or.scala
Created January 4, 2011 12:22 — forked from kmizu/Or.scala
implicit def l[A,B]: Either[A =:= A, A =:= B] = Left(=:=.tpEquals[A])
implicit def r[A,B]: Either[B =:= A, B =:= B] = Right(=:=.tpEquals[B])
def valueIsIntOrString[T](value: T)(implicit param: Either[T =:= String, T =:= Int]) = param match {
case Left(t2String) =>
println("value is String")
println(t2String(value))
case Right(t2Int) =>
println("value is Int")