Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / GenerateMathProblems.groovy
Last active August 29, 2015 13:55
Generates addition and subtraction math problems.
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
*
// 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)
@bdkosher
bdkosher / EasyDateCategory.groovy
Created February 25, 2014 22:46
Extends groovy.time.TimeCategory with the ability to cast Strings into Dates.
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)
}
}
}
@bdkosher
bdkosher / File_eachCsvRow.groovy
Created February 27, 2014 17:24
java.io.File extension method for parsing CSV files row-by-row.
/*
* 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
@bdkosher
bdkosher / xwordFinder.groovy
Created March 23, 2014 22:32
Provide a pattern to a crossword puzzle, (e.g. T _ A _) and find words matching that pattern.
@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('____')
import groovy.time.TimeCategory
[Date, Integer].each { it.mixin(TimeCategory) }
println Date.parse('yyyy-MM-dd', '2014-04-04') + 1.months
@bdkosher
bdkosher / dayOfWeekUsingStringFormat.groovy
Created April 9, 2014 15:47
String.format provides an easy way to get the day of the week of a given Date.
assert String.format('%tA', Date.parse('yyyy-MM-dd', '2013-02-04')) == 'Monday'
@bdkosher
bdkosher / inputStreamEachChunkMethod.groovy
Created May 15, 2014 20:54
A friendlier version of InputStream's eachByte method that provides you a correctly-sized byte[] for the last invokation of the provided closure.
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)
}
}
@bdkosher
bdkosher / PrepareIndyDist.groovy
Last active August 29, 2015 14:01
A Groovy script to indy-enable a Groovy SDK, sans auto-applying the --indy flag in the GROOVY_OPTS.
// 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)
}
@bdkosher
bdkosher / ExcelColumnToIndex.groovy
Last active August 29, 2015 14:04
Converts an Excel column name, e.g. 'A', 'B',...'Z', 'AA', 'AB', etc. to a integer index (0-based)
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
}