Skip to content

Instantly share code, notes, and snippets.

View nobeans's full-sized avatar

Yasuharu Nakano nobeans

  • Yokohama, Japan
View GitHub Profile
def worker = { latch, num ->
println "ready: $num"
latch.countDown()
latch.await()
Thread.sleep(num * 10)
println num
}
def latch = new java.util.concurrent.CountDownLatch(args.size())
args.each {
final COUNT_TO_WRITE = 5
def lineNo = 1
def writableCount = 0
System.in.eachLine {
if (it =~ /hoge/) {
writableCount = COUNT_TO_WRITE
}
if (writableCount > 0) {
String.metaClass.replace << { int i, String s ->
assert s.size() == 1
delegate.value[i] = s as char
}
def x = 'groovy'
x.replace(3, 'X')
assert x == 'groXvy'
#!/bin/sh
echo $*
echo 1: $1
echo 2: $2
echo 3: $3
echo 4: $4
echo 5: $5
@nobeans
nobeans / lottery.groovy
Created July 15, 2011 07:27
lottery by groovy and twitter4j
@GrabConfig(systemClassLoader=true)
@Grab('org.twitter4j:twitter4j-core:[2.1,)')
import twitter4j.*
if (!args) {
System.err.println "usage: groovy lottery.groovy <KEYWORDS>..."
System.exit 1
}
// 強制除外する人を列挙する
// http://golf.shinh.org/
// 46 byte: $ cat input.txt | groovy -n arecibo.groovy
line.findAll('.'*23){println it.tr('01','.#')}
// 56 byte: $ cat input.txt | groovy arecibo.groovy
System.in.text.findAll('.'*23){println it.tr('01','.#')}
// http://d.hatena.ne.jp/fumokmm/20110815/1313405510
def fizzBuzz(List<Integer> nums, Map<Integer, String> dict) {
nums.each { int n ->
println dict.findAll{ n % it.key == 0 }.collect{ dict[it.key] }.join() ?: n
}
}
def convertDict(List dict) {
assert dict.size() % 2 == 0
(0..<dict.size()).step(2).collectEntries{ [dict[it], dict[++it]] }
}
class PathResolver {
String relativePath(String path1, String path2) {
[path1, path2].each{ checkPath(it) }
def (from, to) = cutCommonBaseDir(path1, path2)
(baseDir(from).replaceAll('[^/]+/', '../') ?: './') + to
}
private checkPath(path) {
@nobeans
nobeans / StringListMultiply
Created September 12, 2011 07:09 — forked from kyonmm/StringListMultiply
文字列リストを辞書的に網羅する。
def l = ["", *"A".."Z"]
def combinations = [l, l].combinations().collect{ it.join() }.unique() - ''
combinations.each {
println it
}
@nobeans
nobeans / gist:1213424
Created September 13, 2011 08:41
PMD/Gradle script
#!/bin/sh
GRADLE_FILE_NAME=pmd.gradle
RULE_FILE_NAME=pmd-rules.xml
if [ -f ./$GRADLE_FILE_NAME ]; then
echo "WARN: $GRADLE_FILE_NAME already exists"
exit 1
fi
if [ -f ./$RULE_FILE_NAME ]; then