Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / yCombinator.groovy
Last active August 29, 2015 14:13
Trying to understand Y Combinators using Groovy syntax.
/*
* Follows http://www.righto.com/2009/03/y-combinator-in-arc-and-java.html blog
*/
// standard factorial closure
def fact = { n -> n == 0 ? 1 : n * call(n - 1) }
// factorial closure generator, which requires the factorial closure as an arg (mind blown already)
def factGen = { factFn ->
{ n -> n == 0 ? 1 : n * factFn(n - 1) }
@bdkosher
bdkosher / doethWhilst.groovy
Last active August 29, 2015 14:13
Faking a do-while construct using Groovy (which does not support the do keyword currently)
def _ = [do: { body ->
[while: { condition ->
body()
if (condition()) call(condition)
}]
}]
int i = 0; _.
@bdkosher
bdkosher / rtf_to_html.groovy
Created January 26, 2015 16:35
RTF to HTML with Apache Tika
@Grab(group='org.apache.tika', module='tika-core', version='1.7')
@Grab(group='org.apache.tika', module='tika-parsers', version='1.7')
import org.apache.tika.metadata.*
import org.apache.tika.parser.*
import org.apache.tika.parsers.*
import org.apache.tika.sax.*
import javax.xml.transform.*
import javax.xml.transform.sax.*
import javax.xml.transform.stream.*
import org.xml.sax.*
@bdkosher
bdkosher / SpockMockingExample.groovy
Created January 28, 2015 14:56
Canonical Spock mocking example.
// http://www.infoq.com/presentations/groovy-test-java-spock @ 17:00
import spock.lang.Specification
class Publisher {
def subscribers = []
def send(event) {
subscribers.each {
try {
it.receive(event)
@bdkosher
bdkosher / word.html
Created January 29, 2015 16:00
Launch Word from HTML link
<html>
<body>
<a href="ms-word:ofe|u|http://localhost:8080/doc.docx">Click me</a>
</body>
</html>
@bdkosher
bdkosher / SearchActiveDriectory.groovy
Created February 5, 2015 16:49
Sample script for running queries against Active Directory
@Grab(group='org.springframework.security', module='spring-security-core', version='3.2.5.RELEASE')
@Grab(group='org.springframework.security', module='spring-security-ldap', version='3.2.5.RELEASE')
import groovy.transform.*
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import org.springframework.ldap.core.*
import org.springframework.security.ldap.*
@bdkosher
bdkosher / ListToHierarchy.groovy
Created February 16, 2015 21:22
Convert a List of Nodes to a single Node where the subsequent nodes in the List are the children of the predecessor.
class Node {
String name
Node child
static Node asHierarchy(List<Node> nodes) {
nodes.reverse().inject(null) { prev, curr ->
curr.child = prev
curr
}
}
@bdkosher
bdkosher / blog20150206.md
Last active August 29, 2015 14:16
Blog post about adding methods to anonymous types in Java

Methods in Anonymous Java Object Types

Java syntax allows you to create instances of anonymous types without much boilerplate. This approach is commonly use to implement single-use instance of Single Abstract Method-style interfaces such as Comparable and Runnable:

new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("Hello, world!");
    }

}).start();

@bdkosher
bdkosher / PromptToPause.groovy
Created March 4, 2015 22:04
Prompt to pause script in Groovy console
import javax.swing.*
void pause() {
JOptionPane.showMessageDialog(new JFrame(), 'Click OK to continue.', 'Script Execution Paused', JOptionPane.INFORMATION_MESSAGE)
}
println 1
pause()
println 2
pause()
@bdkosher
bdkosher / HTTPRequestInspectionServer.groovy
Last active August 29, 2015 14:16
A vert.x script to printout information of an HTTP request.
vertx.createHttpServer().requestHandler { req ->
req.bodyHandler { body ->
def headerInfo = req.headers.entries.collect({ e -> "$e.key : $e.value" }).join('\n')
println """
$req.version $req.method $req.absoluteURI
$headerInfo
"""
if (body) {