This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.transform.* | |
/* | |
* Configuration: | |
* | |
* @twoDigit - if false, problems will be single digit operands and answers; if true, one or two digits | |
* @independentProblems - if false, the answer to the previous problem is the first operand of the | |
* subsequent problem, and a finalResult value must be specified | |
* @additionOnly - if true, all problems generated will be addition; if false, it'll be a mix of | |
* addition and subtraction | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// store Groovy script in directory where ZIP was extracted | |
// run vertx run FlappyBird.groovy | |
def root = /FlappyBirdRL-master/ | |
vertx.createHttpServer().requestHandler { req -> | |
def file = java.net.URLDecoder.decode(req.uri == "/" ? "index.html" : req.uri, 'UTF-8') | |
req.response.sendFile "$root/$file" | |
}.listen(8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EasyDateCategory extends groovy.time.TimeCategory { | |
static asType(String str, Class clazz) { | |
if (clazz == Date) { | |
new java.text.SimpleDateFormat('yyyy-MM-dd').parse(str) | |
} else { | |
str.asType(clazz) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Processes file as a CSV, row by row, using the provided closure. An optional starting | |
* row number may be provided to this method, defaulting to 1 (the first row). | |
* | |
* The provided closure can accept one or two arguments, either: | |
* - the row values (String[]) | |
* - the row values (String[]), the current row number (int) | |
* | |
* Currently, only commas are recognized as the separator. Quoted values are handled |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@groovy.transform.Field String pattern = 'A__S__E' | |
class XWordCategory { | |
static boolean matchesPattern(String self, String pattern) { | |
pattern == null ? false : self ==~ pattern.replace('_', /\w{1}/) | |
} | |
} | |
use(XWordCategory) { | |
assert 'KEPI'.matchesPattern('____') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import groovy.time.TimeCategory | |
[Date, Integer].each { it.mixin(TimeCategory) } | |
println Date.parse('yyyy-MM-dd', '2014-04-04') + 1.months |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert String.format('%tA', Date.parse('yyyy-MM-dd', '2013-02-04')) == 'Monday' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
InputStream.metaClass.eachChunk << { int preferredChunkSize, Closure closure -> | |
delegate.eachByte(preferredChunkSize) { buffer, bytesRead -> | |
if (bytesRead == preferredChunkSize) { | |
closure(buffer) | |
} else if (bytesRead > 0) { | |
byte[] data = new byte[bytesRead] | |
System.arraycopy(buffer, 0, data, 0, bytesRead) | |
closure(data) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For moving jars in order to take advantage of Groovy's indy feature | |
def groovyHome = args.length == 0 ? new File('.') : new File(args[0]) | |
def libDir = new File(groovyHome, 'lib') | |
def indyDir = new File(groovyHome, 'indy') | |
if (!groovyHome.exists() || !libDir.exists() || !indyDir.exists() || !new File(groovyHome, 'bin').exists()) { | |
println "$groovyHome does not appear to be a valid Groovy Home" | |
System.exit(0) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int colToIndex(String col) { | |
col.length() == 1 ? col.chars[0] - 65 : (col.chars[0] - 64) * Math.pow(26, col.length() - 1) + colToIndex(col.substring(1)) | |
} | |
[A:0, B:1, Y:24, Z:25, AA:26, AB:27, AY:50, AZ:51, BA:52, BB:53, | |
BY:76, BZ:77, CA:78, CB:79, CY:102, CZ:103, DA:104, DB:105, | |
ZY:700, ZZ:701, AAA:702, AAB: 703, AAZ: 727, ABA: 728, AZZ: 1377, | |
BAA:1378 ].each { k, v -> | |
assert colToIndex(k) == v | |
} |
OlderNewer