Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / obvious-questions.txt
Created June 2, 2017 14:03
Some questions with obvious answers.
Side view mirrors are located on the sides of a car. Where is the rear view mirror located?
Which US state is an island? Hawaii or Rhode Island?
Which US state is farther north? South Daktoa or North Carolina?
@bdkosher
bdkosher / math_with_X.groovy
Created April 10, 2017 20:56
Math with numbers is faster than math with Strings
def mathWithNumbers(String str) {
double secondsPlusRemainder = Double.valueOf(str)
long seconds = (long) Math.floor(secondsPlusRemainder);
long nanoSeconds = ((secondsPlusRemainder - seconds) * 1_000_000_000) as long
[seconds, nanoSeconds]
}
def mathWithStrings(String str) {
def (left, right) = str.split(/\./)
long seconds = left as long
@bdkosher
bdkosher / ExtractAppNumbersFromPBD.groovy
Last active April 10, 2017 14:51
Extracts application numbers from XML downloads from https://pairbulkdata.uspto.gov
if (args.length < 1) {
println 'Please provide an argument pointing to a bulk data XML file.'
System.exit(1)
}
import java.nio.file.*
import java.nio.charset.Charset
import javax.xml.stream.*
import javax.xml.namespace.QName
@bdkosher
bdkosher / mapWeirdness.groovy
Created March 28, 2017 02:19
Some odd behavior with maps.
def map1 = [foo: 1, bar: 2, baz: 3]
def map2 = [foo: 10, bar: 10, baz: 10]
def combined = map1.collectEntries(map2) { k, v -> [(k): v] }
println combined.size() // prints 3
def combined2 = map1.collectEntries(map2) { k, v -> ["$k": v] }
println combined2.size() // prints 6
println combined.size() // prints 6!
@bdkosher
bdkosher / ajax_from_devtools_console.js
Created March 7, 2017 15:23
Snippet for making AJAX request from Chrome DevTools console
new XMLHttpRequest()
.addEventListener("load", function() { console.log(this.responseText); })
.open("GET", "/persons/1464126/addresses")
.send();
@bdkosher
bdkosher / blog20170113.md
Last active January 14, 2017 18:08
Groovy Operator Overloading Gotcha

The Twisted Sisters of Overloadable Groovy Operators

Groovy supports operator overloading for a limited set of operators. Each supported operator corresponds to a particular method signature. If a type implements that method, then that operator is effectively overloaded for instances of that type.

The + operator, for example, corresponds to the plus method, enabling a.plus(b) to be substituted with a + b. And thanks to the wonders of polymorphism, a type can define multiple plus methods to allow + to behave differently depending on the type on the right hand side of the operator (e.g. [1] + 2 inserts 2 at the end of the list while [1] + [2] joins the two lists together, resulting in [1, 2] for both).

This approach to overloading seemed fairly straightforward until I recently discovered two sister operators that had some interesting twists: ++ and --, which correspond to the next() and previous() methods, respectively.

Twist 1: Implicit Reassignment

The first twist is that

@bdkosher
bdkosher / EvenOddMethod.groovy
Created January 7, 2017 02:19
Should be part of Groovy JDK
[Integer, Long, BigInteger].each { clazz ->
clazz.metaClass.isOdd = { -> delegate % 2 == 1}
clazz.metaClass.isEven = { -> !delegate.isOdd()}
}
(1..10).each { n -> println "$n is odd? ${n.isOdd()}. $n is even? ${n.isEven()}" }
@bdkosher
bdkosher / pretty-print.cmd
Created September 27, 2016 18:25
CLI to pretty print JSON using Groovy
groovy -e "println groovy.json.JsonOutput.prettyPrint(new File(args[0]).text)" %1

Why Wouldn't You Write Unit Tests?!

Unit tests are a great way to get quick feedback about your code. They're like an extenion of the compiler that's business logic-aware. A comprehensive suite of unit tests can prevent the introduction of stupid bugs, speed up your release pipeline, and are a boon when you or some other developer has to make a change to code you've written long ago. The maturity and ubiquity of JUnit and supporting tooling have made the barrier to unit test entry extremely low. Heck, you have to explicitly tell Maven NOT to run unit tests.

So with all of these benefits and conveniences, why would a developer not want to write unit tests? Forget TDD, even; I'm talking about not writing unit tests at all.

Here are my theories:

1. You're a Body Shop Developer

@bdkosher
bdkosher / batchdl.groovy
Last active August 2, 2016 13:46
Command line utility for batch downloading.
def cli = new CliBuilder(usage:'batchdl [options] <inputfile>')
cli.with {
start(args:1, argName:'int', 'starting index (1-based) of input file to download')
size(args:1, argName:'int', 'number of URLs to download')
dest(args:1, argName:'dir', 'where the files should be downloaded to')
log(args:1, argName: 'logfile', 'where output should be logged')
}
def options = cli.parse(args)
if (!options) {