Golf.with{ Groovy(2) }
http://b.hatena.ne.jp/tarte/20080425#bookmark-8264934
…気付かなんだ。恥ずかしいので一応ゴルフ(とTips)。
- homeOnTheRange.groovy (106B)
a="$System.in".split()*.toLong() i=0 print~(a- -~a).collect{x=(a-~-a)[i++] it-x?it+'-'+x:x}.join(', ')+'.'
- ArrayList#(?:negate|bitwiseNegate)の妙な仕様を利用。
$ groovy -e 'print(-[1,2,3] + ~[3,4,5] + (~["hoge"])*.getClass())' [-1, -2, -3, -4, -5, -6, class java.util.regex.Pattern]
なぜかspreadされる。「 a*.next() 」「 a*.minus(1) 」とかやるより随分短い。
- String#bitwiseNegateで姑息に縮める。
groovy:000> print['hoge'] // プロパティアクセスとみなされる ERROR groovy.lang.MissingPropertyException: No such property: print for class: groovysh_evaluate groovy:000> print(['hoge']) // のでこう書かざるを得ないが ["hoge"]===> null groovy:000> print~['hoge'] // 「 ~ 」で括弧を外せる [hoge]===> null
無論,Patternとして正しくない文字列には使えない。
*1
*1:08-05-02 短縮・追加・整理