Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@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 / java8lambdas.java
Created April 17, 2014 18:29
Java 8 Lambda examples from Simon Ritter's QCon presentation: http://www.infoq.com/presentations/lambda-streams-java-8
// Ex 1: Convert words in list to upper case
List<String> output = wordList.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
// Ex 2: Finds words in list with even length
List<String> output = wordList.stream()
.filter(w -> w.length() & 1 == 0)
.collect(Collectors.toList());
@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
}
@bdkosher
bdkosher / Combinations.groovy
Created August 8, 2014 14:24
Closure for returning the combinations of the input list of the provided size, r
// returns combinations of the input list of the provided size, r
def combinationsOf = { List list, int r ->
assert (0..<list.size()).contains(r)
def combs = [] as Set
list.eachPermutation {
combs << it.subList(0, r).sort { a, b -> a <=> b }
}
combs as List
}
@bdkosher
bdkosher / Ant2MavenLibAnalyzer.groovy
Created August 20, 2014 19:58
Utility script for scanning a lib directory full of jar files and generate the Maven dependency list from it via screen scraping mvnrepository.com
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
@Grab(group='org.springframework', module='spring-web', version='3.2.10.RELEASE')
import static org.springframework.web.util.UriUtils.encodeQuery
import groovy.transform.TupleConstructor
class MvnRepository {
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
String findGroupId(String artifactId) {
String groupId = null
@bdkosher
bdkosher / show-chrome-mem.bat
Last active August 29, 2015 14:05
Shows the total mem usage (in KB) and count of chrome.exe instances (assumes Groovy shell being on PATH)
@tasklist /NH /FI "IMAGENAME eq chrome.exe" | groovy -e "int tm = 0, ci = 0; new BufferedReader(new InputStreamReader(System.in)).text.eachLine { def p = it.split(/\s{2,}/); if (p.length == 4) {tm += (p[-1] - ',' - ' K') as int; ci++} }; println tm + ' (' + ci + ' instances)'"