Skip to content

Instantly share code, notes, and snippets.

View brunkb's full-sized avatar

Ben Brunk brunkb

  • Minnesota
View GitHub Profile
@brunkb
brunkb / dynamic.groovy
Created June 3, 2017 04:25
Groovy Dynamic Method Invocation
def addOne( a ) { a + 1 }
def addTwo ( b ) { b + 2 }
def tellMeWhatToRun = { what, val ->
"$what"(val)
}
println addOne(1)
println addTwo(5)
@brunkb
brunkb / noconditional.groovy
Created June 2, 2017 16:49
Removing conditional logic
def addOne = { a ->
return a + 1
}
def addTwo = { b ->
return b + 2
}
def decideWhatToRun = [0: addOne, 1: addTwo]
@brunkb
brunkb / FizzBuzz.groovy
Created February 17, 2017 05:51
My version of FizzBuzz in Groovy (Console)
1.upto(100, { i -> println "${i % 3 ? '' : 'Fizz'}${i % 5 ? '' : 'Buzz'}" ?: i })
@brunkb
brunkb / ScannerRecursive.groovy
Created February 1, 2016 17:24
Scanner as a recursive function
String testString = "SOSSOSSDSSKS"
def countSOS = { index = 0, count = 0 ->
println "${testString}, ${index}, ${count}"
index > testString.size() - 1 ?
count : curry(index + 1, count += (testString[index] != "SOS"[index % 3] ? 1 : 0)).call()
}.trampoline()
@brunkb
brunkb / Scanner.groovy
Created February 1, 2016 17:22
Scans a particular input string and counts differences from "SOS"
"SOSSOSSDSSKS"
.toList()
.withIndex()
.collect { val, index ->(val != "SOS"[index % 3] ? 1 : 0) }
.sum()
@brunkb
brunkb / scopes.groovy
Created January 11, 2016 19:43
Gmail API scopes
credential.serviceAccountScopes = [GmailScopes.MAIL_GOOGLE_COM,
GmailScopes.GMAIL_LABELS,
GmailScopes.GMAIL_READONLY,
GmailScopes.GMAIL_MODIFY]
@brunkb
brunkb / ServiceCall.groovy
Created January 11, 2016 19:26
Gmail service call
def contents = service.users().messages().get(userId, message.id).execute()
@brunkb
brunkb / CodeFlow.groovy
Created January 11, 2016 19:14
Google Authorization Code Flow
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(dataStoreFactory)
.setAccessType("offline")
.build()
Map insertTest = [(Book.BOOK.ID): 6,
(Book.BOOK.AUTHOR_ID): 2,
(Book.BOOK.TITLE): "Learn JOOQ in 24 Hours"]
create.insertInto(Book.BOOK).set(insertTest).execute()
// An inner join that is painless
def result = create.select()
.from(Author.AUTHOR.join(Book.BOOK)
.on(Book.BOOK.AUTHOR_ID.equal(Author.AUTHOR.ID)))
.fetch()