This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Employee(val name:String) | |
object EmployeeTable { | |
def insert(employee:Employee):Unit = { /* DBに保存 */ } | |
def selectAll:List[Employee] = { /* DBから検索 */ } | |
} | |
// EmployeeTableがインスタンスそのもので、クラスは見えないところにある。 | |
EmployeeTable.insert(new Employee("Junichi Kato")) | |
val employees = EmployeeTable.selectAll |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package money | |
import java.util.Currency | |
import java.util.Locale | |
class Money(private val amount:BigDecimal, val currency: Currency){ | |
def this(amount:BigDecimal) = this(amount, DEFAULT) // オブジェクトの定数が参照できる | |
def plus(other:Money) = { /*略*/ } | |
} | |
// クラスと同名のオブジェクトを定義できます。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://gist.github.com/1297952 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.CountDownLatch; | |
public class ReOrderingTest { | |
static int x = 0; | |
static int y = 0; | |
static int a = 0; | |
static int b = 0; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.TimeUnit; | |
public class StopThreadTest { | |
private static boolean stopRequested; | |
public static void main(String[] args) { | |
Thread t = new Thread(new Runnable() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package actor | |
import java.util.concurrent.CountDownLatch | |
import scala.collection.mutable.HashSet | |
import akka.stm._ | |
class Sequence { | |
var value = 0 | |
def getValue() = value | |
def getAndIncrement() = synchronized { | |
value += 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.collection.immutable.NumericRange | |
object Util { | |
// nまでの階乗を計算するメソッド | |
def fac(n: BigInt) = | |
NumericRange(BigInt(1), n, BigInt(1)). | |
foldLeft(BigInt(1)){(cur, next) => | |
cur * next | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List(1,2,3,1).zip(List(1,1,1,1)).foldLeft(Map[Int,Int]()) { (x, y) => x.get(y._1) match { case Some(v) => x + Pair(y._1, (v + y._2)) case None => x + y }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object BenchUtil { | |
private def avg(xs:List[BigDecimal]):BigDecimal = | |
xs.sum / xs.size | |
private def std(avg:BigDecimal, xs:List[BigDecimal]):BigDecimal = | |
Math.sqrt((xs.foldLeft(BigDecimal(0))((s,c) => s + (c-avg) * (c+avg)) / xs.size).toDouble) | |
private def center(xs:List[BigDecimal]) = xs.toSet.toList.sortWith(_ < _) match { | |
case n :: Nil => n |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env xsbtscript | |
!# | |
/*** | |
scalaVersion := "2.9.1" | |
resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" | |
libraryDependencies ++= Seq( | |
"se.scalablesolutions.akka" % "akka-stm" % "1.2" |