Skip to content

Instantly share code, notes, and snippets.

@wataru420
wataru420 / Main.java
Created September 25, 2012 11:09
チェーンメソッド風な実装
public class Main {
public static void main(String[] args) {
Timeline timeline = new TimelineImpl();
//チェーンメソッドで色々できます。
timeline.postList(777).limit(15).offset(100).get();
timeline.postList(55).get();
}
}
trait Queue[T]{
def head:T
def tail:Queue[T]
def append(x:T):Queue[T]
}
// クラス実装自体を格納するオブジェクト
object Queue{
// 待ち行列クラスの実装を行う非公開クラス
// 実装内容はこれまでのQueueと同じ
object Queue {
// 初期要素xsを利用して待ち行列を構築しますね
def apply[T](xs:T*) = new Queue[T](xs.toList, Nil)
}
class Queue[T] private (
// 待ち行列の正順リスト
private val leading:List[T],
// 待ち行列の逆順リスト
private val trailing:List[T]
class Queue[T] private (
// 待ち行列の正順リスト
private val leading:List[T],
// 待ち行列の逆順リスト
private val trailing:List[T]
){
// 連続パラメータを初期要素とする補助コンストラクタ
def this(elems:T*) = this(elems.toList, Nil)
class Queue[T](
// 待ち行列の正順リスト
private val leading:List[T],
// 待ち行列の逆順リスト
private val trailing:List[T]
){
// 2つのリストの同期
private def mirror = {
if(leading.isEmpty) new Queue(trailing.reverse, Nil)
else this
class SlowHeadQueue[T](elems:List[T]){
// 末尾を取ってくれば先頭要素
def head = smele.last
// 末尾以外を取ればtailの動作になる
def tail = new SlowHeadQueue(smele.init)
// 連結処理をつかって高速にできる
def append(x:T) = new SlowHeadQueue( x :: smele )
}
class SlowAppendQueue[T](elems:List[T]){
// headはList.headを利用
def head = elem.head
// tailもList.tailから生成
def tail = new SlowAppendQueue(elems.tail)
// ::: メソッドで結合
def append(x:T) = new SlowAppendQueue(elems ::: List(x))
@wataru420
wataru420 / ThriftClient.java
Created September 3, 2012 03:34
finagle-thriftをJavaで動かすSample
public class ThriftClient {
public static void main(String[] args) {
new com.twitter.finagle.thrift.thrift.ClientId();
//Thrift プロトコルベースのリクエストを送信するクライアントを生成
Service<ThriftClientRequest, byte[]> service = ClientBuilder
.safeBuild(ClientBuilder.get()
.hosts(new InetSocketAddress(8080))
.codec(ThriftClientFramedCodec.get())
.hostConnectionLimit(1));
@wataru420
wataru420 / thriftfinagle.rb
Created August 31, 2012 10:33
Homebrew用のthrift-0.8.0-finagleのformula
require 'formula'
# Documentation: https://github.com/mxcl/homebrew/wiki/Formula-Cookbook
class Thriftfinagle < Formula
homepage 'https://github.com/benjyw/thrift-0.8.0-finagle'
version '0.8.0'
url 'https://github.com/benjyw/thrift-0.8.0-finagle/tarball/master'
sha1 'd5d87bc8b658c81b5095b701512085017f58564e'
@wataru420
wataru420 / ddl.sql
Created August 23, 2012 06:31
MySQLのTRIGGERとLAST_INSERT_IDについて
USE last_insert_test;
CREATE TABLE table1 (
id1 int
) ENGINE=InnoDB;
CREATE TABLE table2 (
id2 int
) ENGINE=InnoDB;