Skip to content

Instantly share code, notes, and snippets.

@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
// 2, 5, 6, 8の式の組み合わせを生成
def rslt = []
combiAll('+', '-', '*').collect { opes ->
combi(2, 5, 6, 8).collect { nums ->
rslt << [nums, opes + [null]].transpose().flatten()[0..-2]
}
}
// 答えが10になるリストを出力
rslt.collect { toExp(it) }.findAll{ it.ans == 10 }.each {
def prop = new Properties()
prop.setProperty 'ONE', '1'
prop.setProperty 'TWO', '2'
prop.setProperty 'THREE', '3'
new File('/temp.properties.xml').withOutputStream{ outStream ->
prop.storeToXML(outStream, 'Temporary Properties')
}
def prop = new Properties()
new File('/temp.properties.xml').withInputStream{ inStream ->
prop.loadFromXML(inStream)
}
prop.each {
println "${it.key} => ${it.value}"
}
/*
* 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