Skip to content

Instantly share code, notes, and snippets.

@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
@fumokmm
fumokmm / translate_demo.groovy
Created October 30, 2009 00:32
テンプレートを置き換えるデモです。
def template = '○○と●●●は違う'
def list = ['○○', '●●●']
def translator = list.inject([:]){ result, item -> result[item] = getList(item); result }
// こうなる↓
//def translator = [
// '○○' : ['1', '2', '3', '4', '5'],
// '●●●' : ['あああ', 'いいい', 'ううう', 'えええ', 'おおお']
//]
@fumokmm
fumokmm / GroovyShellDemo.groovy
Created November 14, 2009 10:39
文字列をスクリプトとして実行する。これは便利。
import hatenahaiku4j.*
api = new HatenaHaikuAPILight()
str = """
def konna = api.getHotKeywordList().first()
def konnaUsers = konna.api.getTimeline()*.user
def konnaKeyword = konnaUsers.collect{
it.api.getTimeline()*.keyword
}.flatten().unique()
@fumokmm
fumokmm / RandomWordByWikipedia.groovy
Created November 15, 2009 16:32
辞書サイトからランダムにキーワードを取得する(Wikipedia版)
// http://ja.wikipedia.org/wiki/特別:おまかせ表示
def url = 'http://ja.wikipedia.org/wiki/%E7%89%B9%E5%88%A5:%E3%81%8A%E3%81%BE%E3%81%8B%E3%81%9B%E8%A1%A8%E7%A4%BA'
def html = new URL(url).getText('UTF-8')
def word = html.replaceAll(/(?s)^.+<title>| - Wikipedia<\/title>.+$/, '')
println word
object HelloWorld {
def main(args: Array[String]): Unit = {
System.out.println("Hello, scala!")
}
}
scala> val list = List(1, 2, 3)
list: List[Int] = List(1, 2, 3)
scala> val List(a, b, c) = list
a: Int = 1
b: Int = 2
c: Int = 3
scala> a
res1: Int = 1
def prop = new Properties()
new File('/temp.properties.xml').withInputStream{ inStream ->
prop.loadFromXML(inStream)
}
prop.each {
println "${it.key} => ${it.value}"
}
def getBytes(str, encode) {
str.getBytes(encode).collect{
"0x${Integer.toHexString(it & 0xff)}"
}
}
assert getBytes('\uff5e', 'Windows-31J') == ['0x81', '0x60']
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software