Skip to content

Instantly share code, notes, and snippets.

@aeg
aeg / CalcPower.groovy
Created May 11, 2013 17:14
累乗を計算する
// Case #1
// 2乗を計算する
// 5^2 = 25
// 引数も結果も Double
assert Math.pow(5, 2) == 25
// Case #2
// 累乗(N の M 乗)を計算する
@aeg
aeg / RadixNumber.groovy
Created May 4, 2013 17:11
10進数を2進数、8進数、16進数、n進数に変換する。
// Case #1
// 10進数を16進数にする
assert Integer.toHexString(129) == '81'
// Case #2
// 10進数を2進数にする
assert Integer.toBinaryString(129) == '10000001'
@aeg
aeg / ExpandURL.groovy
Created May 4, 2013 16:35
短縮URLをGroovyで展開する
// Case #1
// 短縮URLを展開する
// 短縮URLが存在しない場合には、groovyx.net.http.HttpResponseException をthrowする
import groovyx.net.http.HttpURLClient
assert 'https://gyakubiki-groovy.rhcloud.com/' == expandURL("http://bit.ly/gyakubikigroovy")
public String expandURL(String urlStr) {
HttpURLClient http = new HttpURLClient(followRedirects:false)
@aeg
aeg / URIEscape.groovy
Created May 4, 2013 04:50
URI Escapeする。
// Case #1
// URIエスケープする(URLエンコードとかURIエスケープとか言われたりする)
def str = 'http://www.asahi.com/q="test aaa&1344"'
def result = new URLEncoder().encode(str)
assert result == 'http%3A%2F%2Fwww.asahi.com%2Fq%3D%22test+aaa%261344%22'
// Case #2
@aeg
aeg / URLExpander.groovy
Last active December 16, 2015 22:49
短縮URLの展開。
println expandURL("http://j.mp/dankogai")
public String expandURL(String urlStr) {
HttpURLClient http = new HttpURLClient(followRedirects:false)
def params = [ url:urlStr,
headers:['User-Agent':'Mozilla/5.0'] ]
def resp = http.request( params )
return resp.headers.'Location'
}
@aeg
aeg / ClearList.groovy
Last active December 16, 2015 15:09
リストを空にする
package org.eiji.list
// Case #1
// リストを空にする
def list1 = ['a', 'b', 'c']
list1.clear()
assert list1 == []
Last Updated: February 28, 2013
Yammer Privacy Statement
Microsoft and the Yammer team are committed to protecting your privacy. This privacy statement applies to the data collected by Microsoft through use of Yammer services (the “Service” or “Services”); it does not apply to other online or offline Microsoft sites, products, or services.
Yammer Networks
A “Yammer Network” is the network for using the Services defined by your organization’s email domain. For example, all users who sign up with a “contoso.com” email address would be part of the Contoso Yammer Network. In addition, a company may invite users with different email domain names to their Yammer Network as “guest users” or as participants in external networks associated with that Yammer Network.
@aeg
aeg / AddMapEntry.groovy
Created February 19, 2013 01:38
マップに要素を追加する。
// Case #1
// マップに要素を追加する
def map1 = [:]
map1.put('犬1', 'taro')
map1.put('猫1', 'tama')
map1['犬2'] = 'jiro'
println map1
@aeg
aeg / PermutationsOfItems.groovy
Last active December 13, 2015 22:08
List要素から順列組合せを得る
// Case #1
// リスト要素の順列組合せを得る
def list1 = ['a', 'b', 'c']
def list2 = list1.permutations()
// Collections.permutations() の結果は HashSet になるので、比較のために ArrayListに変換している
assert list2 as ArrayList == [['c', 'b', 'a'], ['a', 'c', 'b'], ['c', 'a', 'b'],
['b', 'c', 'a'], ['b', 'a', 'c'], ['a', 'b', 'c']]
@aeg
aeg / RandomSelect.groovy
Last active December 13, 2015 22:08
リストからランダムに1つだけ要素を選ぶ
// Case #1
// リストからランダムに1つだけ要素を選ぶ
def list1 = ['a', 'b', 'c', 'd']
def item = list1[new Random().nextInt(list1.size())]
println item
// 実行結果例(実行する度に結果が異なる)
// c