Skip to content

Instantly share code, notes, and snippets.

@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")
/*
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 / 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.回避
@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 / 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 / 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 / joinRight
Created December 13, 2011 17:02
joinRightサンプル
scala> val a: Either[Exception, Either[Exception, Int]] = Right(Right(10))
a: Either[Exception,Either[Exception,Int]] = Right(Right(10))
scala> a.joinRight
res0: Either[Exception,Int] = Right(10)
scala> val b: Either[Exception, Either[Exception, Int]] = Right(Left(new Exception("a")))
b: Either[Exception,Either[Exception,Int]] = Right(Left(java.lang.Exception: a))
scala> b.joinRight
@ogatatsu
ogatatsu / gist:1518754
Created December 25, 2011 05:10 — forked from j5ik2o/gist:1518723
こんな構文で階層型のログ出力をしたい
def connect = {
log("connect") { // connect start
// ... 何かの処理 ...
log("login") { // connect : login start
// ... 何かの処理 ...
} // connect : login end
// ... 何かの処理 ...
} // connect end
}
@ogatatsu
ogatatsu / readme.md
Created June 27, 2012 11:10 — forked from j5ik2o/gist:2996293
リトライハンドラー 魔改造
object RetryUtil {
import scala.util.control.Exception.allCatch
case class RetryResult[T](e: Either[List[Throwable], T]) {
def `catch`(f: List[Throwable] => T): T = e match {
case Right(r) => r
case Left(l) => f(l)
}
}
def test(): Unit = {
for(i <- 1 to 10) {
println(i);
if(i == 5) return;
}
println("test"); //実行されたら気持ち悪い
}