Skip to content

Instantly share code, notes, and snippets.

View j5ik2o's full-sized avatar

Junichi Kato j5ik2o

View GitHub Profile
@j5ik2o
j5ik2o / MapReduce.scala
Created December 10, 2011 06:53
MapReduceサンプル
package mapreduce
import akka.actor.Actor
import akka.actor.ActorRef
import scala.collection.mutable.ListBuffer
import scala.concurrent.ops._
import scala.io.Source
import scala.annotation.tailrec
import scala.io.Codec
import scala.xml.XML
@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 / Loan.scala
Created December 16, 2011 12:59
ベタなLoanパターン
object Loan {
def using[A <: { def close() }, B](resource: A)(func: A => B): B =
try {
func(resource)
} finally {
if (resource != null) resource.close()
}
}
@j5ik2o
j5ik2o / gist:1516496
Created December 24, 2011 06:00
NullPointerExceptionでた
jp.sf.amateras.scala.sbt_0.0.3.201112241216.jar
でプロジェクトを新規作成して、Update Dependency を実行したらNullPointerExceptionが発生した。
libraryDependencies ++= Seq(
"junit" % "junit" % "4.8.1" % "test"
)
などと追記してもDependencyが追加されていない模様。
@j5ik2o
j5ik2o / gist:1518723
Created December 25, 2011 04:23
こんな構文で階層型のログ出力をしたい
def connect = {
log("connect") { // connect start
// ... 何かの処理 ...
log("login") { // connect : login start
// ... 何かの処理 ...
} // connect : login end
// ... 何かの処理 ...
} // connect end
}
@j5ik2o
j5ik2o / gist:1520836
Last active January 3, 2017 17:09
Scalaで使えそうなWeb系フレームワーク&ライブラリ
@j5ik2o
j5ik2o / gist:1536703
Created December 29, 2011 23:33
例外を定義する便利なパターン
case class EntityNotFoundException(message: Option[String] = None, cause: Option[Throwable] = None)
extends Exception(message.orNull, cause.orNull)
@j5ik2o
j5ik2o / gist:1546494
Created January 1, 2012 06:33
エンティティ・トレイトに対して Scalazの EqualとShowを実装するにはどうするべきか
package base
import scalaz._
import Scalaz._
/**
* エンティティを表すトレイト。
*
* @author j5ik2o
*/
@j5ik2o
j5ik2o / gist:1569199
Created January 6, 2012 05:41
JIRA:初期見積もりが登録されているチケットの作業時間が未登録かどうかを判定するスクリプト
// サブタスクの作業時間を計算する
def subTasksTimeSpent(target) {
long result = 0
subTasks = target.getSubTaskObjects()
for(subTask in subTasks){
result += (subTask.getTimeSpent() == null ? 0 : subTask.getTimeSpent().floatValue())
}
return result
}
// 初期見積もりが登録されている場合
@j5ik2o
j5ik2o / gist:1611868
Created January 14, 2012 15:58
AOPを使わなくても処理の始まりと終わりにログを出力するtrait
import scala.util.DynamicVariable
import scala.collection.immutable
import grizzled.slf4j.Logging
trait LoggingEx extends Logging {
private val msgs = new DynamicVariable[Seq[String]](immutable.Queue.empty)
private def withScope[T](msg: String, logger: (=> Any, => Throwable) => Unit, f: => T): T = {
val newMsgs = msgs.value :+ msg