Skip to content

Instantly share code, notes, and snippets.

View gakuzzzz's full-sized avatar

Manabu Nakamura gakuzzzz

View GitHub Profile
@gakuzzzz
gakuzzzz / gist:2426126
Created April 20, 2012 04:59
scala の ConcurrentMap と withDefault
scala> import scala.collection._
import scala.collection._
scala> import scala.collection.JavaConverters._
import scala.collection.JavaConverters._
scala> val m: mutable.ConcurrentMap[Int, Int] = new java.util.concurrent.ConcurrentHashMap().asScala
m: scala.collection.mutable.ConcurrentMap[Int,Int] = Map()
scala> val m2 = m.withDefault(2*)
case class Area(code: String, name: String)
object Area {
val Japan: Set[Area] = Set(
Area("01", "北海道"),
Area("02", "青森県")
// ...
)
val Us: Set[Area] = Set(
Area("AK", "Alaska")
@gakuzzzz
gakuzzzz / gist:2497761
Created April 26, 2012 08:49
scala-io0.4.0test
name := "scala-io-test"
version := "0.1"
scalaVersion := "2.9.2"
resolvers ++= Seq(
"typesafe-repo" at "http://repo.typesafe.com/typesafe/releases/",
"scala-tools" at "https://oss.sonatype.org/content/groups/scala-tools/"
)
@gakuzzzz
gakuzzzz / gist:2730028
Created May 19, 2012 08:09
finagle_hack

Twitter-Util Futureでのトランザクションを考える

やりたいこと

def findUser(screenName: String): Future[User] = ... 
def findFollowers(user: User): Future[Set[User]] = ...
def unfollowAtRandom(user: User, followers: Set[User]): Future[Unit] = ...
@gakuzzzz
gakuzzzz / 1.scala
Created May 20, 2012 03:03
中断可能なfold
sealed trait FoldResult[+A]
case class Continue[+A](a: A) extends FoldResult[A]
case class End[+A](a: A) extends FoldResult[A]
class LimitFoldable[+A](underlying: Traversable[A]) {
def limitFoldLeft[B](init: B)(f: (B, A) => FoldResult[B]): B = {
@tailrec
def fold(sentinel: B, rest: Traversable[A]): B = {
if (rest.isEmpty) return sentinel
f(sentinel, rest.head) match {
@gakuzzzz
gakuzzzz / readme.md
Created July 4, 2012 17:31 — forked from xuwei-k/not_tailrec.scala
リトライハンドラーの殴り書き
  • 1秒後, 5秒後, 15秒後, 60秒後 みたいなリトライがしたかったので interval に Stream[Int] を渡せるようにしてみた。
  • 無限 Stream 渡したら無限 retry もできるね。
@gakuzzzz
gakuzzzz / 1.java
Created August 22, 2012 16:18
EntityContext
public interface EntityContext {
Object get();
void set(Object entity);
void remove();
}
@gakuzzzz
gakuzzzz / 1.scala
Created September 11, 2012 07:33
Case クラス固有のメソッドをトレイトで使いたい
case class User(id: String, name: String, options: Seq[String]) {
def options(os: String*): User = copy(options = options ++ os)
}
case class Group(id: String, name: String, subgroups: Seq[Group], options: Seq[String]) {
def options(os: String*): Group = copy(options = options ++ os)
}
@gakuzzzz
gakuzzzz / 1.java
Created September 13, 2012 05:11
オブジェクト指向エクササイズのアクセサ禁止
// これはNG
public class User {
private String name;
private int age;
public User(String name, int age) {
this.name = name;
this.age = age;
@gakuzzzz
gakuzzzz / gist:3752885
Created September 19, 2012 22:56
メソッド定義のパターンマッチ
scala> val hoge: (Int, Int, Int) => Int = {
| case (0, _, 1) => 2
| case (_, _, _) => 3
| }
hoge: (Int, Int, Int) => Int = <function3>
scala> hoge(0, 2, 1)
res0: Int = 2