Skip to content

Instantly share code, notes, and snippets.

Avatar

Ben Brunk brunkb

  • Minnesota
View GitHub Profile
@brunkb
brunkb / dynamic.groovy
Created Jun 3, 2017
Groovy Dynamic Method Invocation
View dynamic.groovy
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 Jun 2, 2017
Removing conditional logic
View noconditional.groovy
def addOne = { a ->
return a + 1
}
def addTwo = { b ->
return b + 2
}
def decideWhatToRun = [0: addOne, 1: addTwo]
@brunkb
brunkb / FizzBuzz.groovy
Created Feb 17, 2017
My version of FizzBuzz in Groovy (Console)
View FizzBuzz.groovy
1.upto(100, { i -> println "${i % 3 ? '' : 'Fizz'}${i % 5 ? '' : 'Buzz'}" ?: i })
@brunkb
brunkb / ScannerRecursive.groovy
Created Feb 1, 2016
Scanner as a recursive function
View ScannerRecursive.groovy
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 Feb 1, 2016
Scans a particular input string and counts differences from "SOS"
View Scanner.groovy
"SOSSOSSDSSKS"
.toList()
.withIndex()
.collect { val, index ->(val != "SOS"[index % 3] ? 1 : 0) }
.sum()
@brunkb
brunkb / scopes.groovy
Created Jan 11, 2016
Gmail API scopes
View scopes.groovy
credential.serviceAccountScopes = [GmailScopes.MAIL_GOOGLE_COM,
GmailScopes.GMAIL_LABELS,
GmailScopes.GMAIL_READONLY,
GmailScopes.GMAIL_MODIFY]
View ServiceCall.groovy
def contents = service.users().messages().get(userId, message.id).execute()
@brunkb
brunkb / CodeFlow.groovy
Created Jan 11, 2016
Google Authorization Code Flow
View CodeFlow.groovy
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(dataStoreFactory)
.setAccessType("offline")
.build()
View jooq_insert_with_map.groovy
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()
View jooq_inner_join.groovy
// 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()