Skip to content

Instantly share code, notes, and snippets.

@fumokmm
fumokmm / QuickSort.groovy
Created October 16, 2009 07:23
I'm trying to write Quick Sort in Groovy. /update 2010-10-10 (using #g100pon No.47)
// g100pon #47 QuickSort
def quickSort(lambda, list) {
if (!list || list.size() < 2) return list
def pivot = list[0] // とりあえず先頭のものを基準にする
def middle = list.findAll{ it == pivot }
def left = list.findAll{ lambda(it, pivot) } - middle
def right = list.findAll{ !lambda(it, pivot) } - middle
quickSort(lambda, left) + middle + quickSort(lambda, right)
}
@fumokmm
fumokmm / GetKeywordList.groovy
Created October 18, 2009 01:53
はてなハイクキーワードサーチ用
// 正規表現版で実行
getKeywordList1('', 3000).with {
println "${it.size()}件"
it.each { li ->
println "[${li.title}](${li.entryCount}) ${li.url}"
}
}
// XmlSlurper版で実行
getKeywordList2('', 3000).with {
interface ReduceOp<E, T> {
void add(E e);
T value();
}
class CounterOp<E> implements ReduceOp<E, Integer> {
private int v;
public void add(E e){++v;}
public Integer value(){return v;}
}
(-128..127).each {
printf('%02x\n', it as byte)
}
@fumokmm
fumokmm / DigestSHA1.groovy
Created October 20, 2009 05:27
groovy(java)にて、SHA-1ハッシュを求める
import java.security.*
MessageDigest md = MessageDigest.getInstance('SHA-1')
md.update('1234'.getBytes('UTF-8'))
def result = md.digest().collect{ String.format('%02x', it) }.join()
assert result == '7110eda4d09e062aa5e4a390b0a572ac0d2c0220'
# 同フォルダにhatenahaiku4j-1.1.1.jarを読み込む
require 'hatenahaiku4j-1.1.1.jar'
# キーワードリストの1ページ目を取得しタイトルを出力
api = Java::hatenahaiku4j.HatenaHaikuAPILight.new
api.get_keyword_list.each do |k|
puts k.get_title
end
@fumokmm
fumokmm / UUIDTest.java
Created October 26, 2009 14:13
UUID !!
java.util.UUID.randomUUID().toString()
@fumokmm
fumokmm / RandomAccessTest.groovy
Created October 26, 2009 14:13
リストにハッシュをつけてランダムアクセスするデモ
String getSHA1(String source) {
java.security.MessageDigest md = java.security.MessageDigest.getInstance('SHA-1')
md.update(source.getBytes('UTF-8'))
md.digest().collect{ String.format('%02x', it) }.join()
}
def list = (1..10).collect{
[it, getSHA1(it.toString())]
}
println list
@fumokmm
fumokmm / _vimrc
Created October 27, 2009 01:14
vimの設定
set backupdir=~/vimfiles/tmp
set clipboard=unnamed
@fumokmm
fumokmm / RandomAccessTest.groovy
Created October 27, 2009 08:35
ハッシュによるランダムアクセスのテスト
String getSHA1(String source) {
java.security.MessageDigest md = java.security.MessageDigest.getInstance('SHA-1')
md.update(source.getBytes('UTF-8'))
md.digest().collect{ String.format('%02x', it) }.join()
}
def list = (1..10).collect{
[it, getSHA1(it.toString())]
}
println list