Skip to content

Instantly share code, notes, and snippets.

@rrgroovy
rrgroovy / toUpperOrLowerCase.groovy
Created October 9, 2011 09:46
大文字・小文字に揃える
assert 'HI, GROOVYIST' == 'Hi, Groovyist'.toUpperCase()
assert 'hi, groovyist' == 'Hi, Groovyist'.toLowerCase()
@rrgroovy
rrgroovy / substring.groovy
Created October 9, 2011 09:49
部分文字列を取り出す
// 1. String#substringを使う。
assert '0123456'.substring(2, 4) == '23'
// 2. Object#getAtで範囲を使う。 (GDK)
assert '0123456'[2..3] == '23'
assert '0123456'[2..<4] == '23'
@rrgroovy
rrgroovy / concatenateString.groovy
Created October 9, 2011 09:55
文字列を結合する
assert 'foobar' == 'foo' + 'bar'
@rrgroovy
rrgroovy / abs.groovy
Created October 9, 2011 10:02
絶対値を求める
assert 100 == Math.abs(100)
assert 100 == Math.abs(-100)
assert 0 == Math.abs(0)
@rrgroovy
rrgroovy / eachString.groovy
Created October 10, 2011 21:23
文字列を一文字ずつ処理する
'abcdef'.each {
println it
}
for (s in '123456') {
println s
}
@rrgroovy
rrgroovy / uriCodec.groovy
Created October 12, 2011 11:53
URIエンコード/デコード
def origin = 'あいうえお'
def encoded = '%E3%81%82%E3%81%84%E3%81%86%E3%81%88%E3%81%8A'
assert URLEncoder.encode(origin, 'UTF-8') == encoded
assert URLDecoder.decode(encoded, 'UTF-8') == origin
@rrgroovy
rrgroovy / bubbleSort.groovy
Created November 8, 2011 03:59
Reinvention of the Wheel Series, "Bubble Sort in Groovy".
class BubbleSort {
def list
def sort() {
def eachPairIndex = { Collection l ->
def range = 0..<l.size()
[range, range.tail()].transpose()
}
while (! eachPairIndex(this.list).inject(true) { rslt, idx ->
if (! (this.list[idx[0]] < this.list[idx[1]]) ) {
use(Collections){ this.list.swap(idx[0], idx[1]) }