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.util.concurrent.* | |
def execSingle = { String cmd, out, showTs -> | |
if (showTs) out << "${System.currentTimeMillis()}: " | |
out << cmd.execute().in | |
} | |
def exec = { cmdStrOrList, out, showTs -> | |
switch (cmdStrOrList) { | |
case String: |
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 extractPathOnly = { path -> | |
switch (path) { | |
case '/': | |
path = 'index.html' | |
break | |
case { it.contains('?') }: | |
path = path.split('\\?')[0] | |
break | |
case { it.contains('#') }: | |
path = path.split('#')[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
def (f1, f2) = [new File(args[0]), new File(args[1])].each { f -> | |
if (!f.exists() || f.isDirectory()) { | |
println "$f does not exist or is a directory" | |
System.exit(0); | |
} else { | |
f | |
} | |
} | |
println "$f1 and $f2 provided" |
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
function constructor(spec) { | |
let {member} = spec, | |
{other} = other_constructor(spec), | |
method = function () { | |
// member, other, method | |
}; | |
return Object.freeze({ | |
method, | |
other | |
}); |
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
var isWindowOrNode = function (obj, callback, callbackArg) { | |
var result = false; | |
if (obj != null) { | |
if (obj instanceof Window) { | |
callback(obj, callbackArg); | |
result = true; | |
} else if (obj instanceof HTMLDocument) { | |
callback(obj.defaultView, callbackArg); | |
result = true; | |
} else if (obj instanceof HTMLElement) { |
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.TupleConstructor | |
// approach 1. Custom subtype coercion | |
@TupleConstructor | |
class LastAwareIterator<T> implements Iterator<T> { | |
Iterator itr | |
boolean hasNext() { | |
itr.hasNext() | |
} | |
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
@Unroll | |
def "the string '#string' is #description"() { | |
expect: | |
string.isInteger() == expected | |
where: | |
string | expected | |
"ABC" | false | |
"123" | true | |
"1.2" | false |
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
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2') | |
import java.text.* | |
class SOTagCounter { | |
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser()) | |
int count(String tag) { | |
new URL("http://stackoverflow.com/questions/tagged/${new URI(tag).path}").withReader { page -> | |
def html = parser.parse(page) | |
def c = html.body.div.find { it.@class == 'container' }.div.find { it.@id == 'content' }.div.find { it.@id == 'sidebar' }.div.find { it.@class == 'module' }.div[0].text() |
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 prpsutil; | |
import java.io.*; | |
import java.util.*; | |
import org.apache.pdfbox.pdmodel.*; | |
import org.apache.pdfbox.pdmodel.font.*; | |
public class PDFScanner { | |
private static final byte[] expectedMagicNumber = "%PDF".getBytes(); |