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
def timeIt = { resultFormatter = { obj -> obj?.toString() }, closure -> | |
def start = System.nanoTime() | |
def result = null | |
try { | |
result = closure() | |
} catch (Exception e) { | |
result = e | |
} | |
def end = System.nanoTime() | |
def ms = (end - start) / 1e6 |
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 java.nio.ByteBuffer; | |
int integer = 8; | |
byte[] bytes = ByteBuffer.allocate(4).putInt(integer).array(); | |
for (byte t : bytes) { | |
System.out.format("0x%x ", t); | |
} |
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
// We can loop from today to next week and invoke a closure for each day. | |
def today = new Date().clearTime() | |
def nextWeek = today + 7 | |
today.upto(nextWeek) { | |
// Print day of the week. | |
println it.format('EEEE') | |
} | |
println() |
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
def printClassPath(classLoader) { | |
println "$classLoader" | |
classLoader.getURLs().each { url-> | |
println "- ${url.toString()}" | |
} | |
if (classLoader.parent) { | |
printClassPath(classLoader.parent) | |
} | |
} | |
printClassPath this.class.classLoader |
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
/** | |
* Breaks apart a line localhost_access_logs and provides properties to access the various components of that line | |
* | |
* Assumes that the format is | |
* Time Taken: 0.006 10.113.110.28 - - [12/Jan/2014:00:02:29 -0500] GET /path/ HTTP/1.1 http-bsd-dfw-1.xyz.com%2F10.113.110.25-8443-7 302 - E1518C5CABA9A32394BE209727824D3D.profile | |
* 0 1 2=et 3=ip 4 5 6=timestamp 7=to 8=h 9=up 10=pr 11=hostname%2Fhostip-port-thread 12=rc 13 14=??? | |
*/ | |
class Entry { | |
def elapsedTimeIdx = 2 | |
def ipIdx = 3 |
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
package bdkosher.datetime; | |
import java.util.Calendar; | |
import static java.util.Calendar.*; | |
import java.util.Date; | |
import java.util.Locale; | |
import java.util.SortedSet; | |
import java.util.TimeZone; | |
import java.util.TreeSet; |