Skip to content

Instantly share code, notes, and snippets.

@aeg
aeg / RandomListElementGet.groovy
Created February 18, 2013 16:12
リストの要素をランダムに並び替える
// Case #1
// リストの要素をランダムに並び替える
def list1 = ['a', 'b', 'c', 'd']
def list2 = []
while(list1.size() > 0) {
index = new Random().nextInt(list1.size())
list2.add(list1[index])
list1.remove(index)
}
@aeg
aeg / RemoveList.groovy
Created February 18, 2013 15:49
指定した条件を満たす要素をリストから削除する。
// Case #1
// 指定した条件を満たす要素を削除したリストを作成する
def list1 = [1, 3, 7, 10, 13]
assert list1.findAll{!(it < 10)} == [10, 13]
// Case #2
// 指定した条件を満たす要素をリストから削除する
@aeg
aeg / SelectList.groovy
Created February 17, 2013 15:36
リストから指定条件を満たす要素を抽出する
// Case #1
// リストの要素を条件で抽出する。
def list1 = ['a', 'b', 'c', 'd']
assert list1.grep{ it > 'b' } == ['c', 'd']
// Case #2
// リストの要素をリストの条件で抽出する。
@aeg
aeg / EnclosedTest.groovy
Created February 17, 2013 13:17
SpockとJUnitが混在したテストクラス。
import org.junit.Test
import org.junit.experimental.runners.Enclosed
import org.junit.runner.RunWith;
import spock.lang.Specification
@RunWith(Enclosed.class)
class SpockJUnitMixTest {
static class Spockテストグループ1 extends Specification{
def "Spockテスト1-1"() {
when:
@aeg
aeg / NumberLiteral.groovy
Last active December 10, 2015 14:58
n進数表現
// Case #1
// 数値の2進数、8進数、16進数、n進数表現(n進数以外はリテラル)
// 2進数(00001111)
assert 0b00001111 == 15
// 8進数(0100)
assert 0100 == 64
// 16進数(ff)
@aeg
aeg / NumberLiteral.groovy
Created January 4, 2013 09:06
n進数表現
// Case #1
// 数値の2進数、8進数、16進数、n進数表現
// 2進数(00001111)
assert 0b00001111 == 15
// 8進数(0100)
assert 0100 == 64
// 16進数(ff)
@aeg
aeg / CalcLog.groovy
Created January 3, 2013 18:39
対数を計算する
// Case #1
// 自然対数を計算する
assert Math.log(72) ==4.276666119016055
assert Math.log(Math.E) == 1
// Case #2
// 10を底とした対数を計算する
@aeg
aeg / DeleteList.groovy
Last active December 10, 2015 14:08
リストの要素を削除する
// Case #1
// リストの任意の要素をインデックス指定で削除する
def list1 = ['a', 'b', 'c', 'd']
list1.remove(2)
assert list1 == ['a', 'b', 'd']
// Case #2
@aeg
aeg / FlattenList.groovy
Created December 31, 2012 16:30
リストのリストをフラットなリストにする
// Case #1
// リストのリストをフラットなリストにする
assert ['a', ['b', 'c',['d', 'e']]].flatten() == ['a', 'b', 'c', 'd', 'e']
// Case #2
// リスト展開演算子を用いて、リストを展開する
assert ['a', 'b', *['c', 'd']] == ['a', 'b', 'c', 'd']
@aeg
aeg / ListSumAndProduct.groovy
Last active December 10, 2015 10:18
リストの和と積をとる
// Case #1
// リストの集合演算的な和をとる
def list1 = ['a' ,'b', 'c']
def list2 = ['b', 'c', 'd', 'e']
assert (list1 + list2).unique() == ['a', 'b', 'c', 'd', 'e']
// Case #2
// リストの集合演算的な積をとる(リストの共通部分をとる)