Skip to content

Instantly share code, notes, and snippets.

View robfletcher's full-sized avatar

Rob Fletcher robfletcher

View GitHub Profile
@robfletcher
robfletcher / gist:44b115286fd9ef9c8a8c
Last active August 29, 2015 14:11
Difference in static & dynamic code dispatching to field or getter
import groovy.transform.*
//@CompileStatic
class Person {
String name
private List<Person> friends
String friendNames() {
friends.collect { it.name }.join(", ")
}
@robfletcher
robfletcher / gist:b596b90d651370da281c
Created December 12, 2014 22:13
Using Groovy inject to iteratively add things to a fluent builder
JobParameters fromMap(Map<String, String> parameters) {
parameters.inject(new JobParametersBuilder()) { JobParametersBuilder builder, entry ->
builder.addParameter(entry.key, new JobParameter(entry.value))
} toJobParameters()
}
@robfletcher
robfletcher / gist:fb3546d248bf0398d741
Created March 17, 2015 18:53
Test for backpressure when using Observable.interval
@Grab("io.reactivex:rxjava:1.0.8")
import rx.*
import static java.util.concurrent.TimeUnit.*
import static java.time.LocalTime.*
def sub = Observable.interval(1, SECONDS)
.doOnNext { tick->
println "tick $tick at ${now()}"
}
.subscribe { tick ->
@robfletcher
robfletcher / Event.scala
Created September 15, 2015 01:06
Using companion object apply method from outside of Scala
package example
import java.time._
case class Event(description: String, time: Option[ZonedDateTime])
object Event {
def apply(description: String) = new Event(description, None)
def apply(description: String, time: ZonedDateTime) = new Event(description, Some(time))
}
import com.fasterxml.jackson.annotation.JsonSubTypes.Type
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id
import com.fasterxml.jackson.annotation.{JsonSubTypes, JsonTypeInfo}
@JsonTypeInfo(use = Id.NAME, property = "name")
@JsonSubTypes(Array(
new Type(classOf[Vodka.type]),
new Type(classOf[Whiskey])
))
trait Spirit
ctx
.byMethod(spec ->
spec
.get(() -> ctx.render("GET"))
.post(() -> ctx.render("POST"))
);
@robfletcher
robfletcher / Diamond.kt
Last active February 18, 2016 16:09
The diamond kata in Kotlin
fun diamond(c: Char): String {
if (c !in 'A'..'Z') throw IllegalArgumentException()
return ('A'..c)
.map(row(c))
.mirrorDown()
.joinToString("\n")
}
private fun row(max: Char) = { next: Char ->
"$next"
@robfletcher
robfletcher / InteropNullabilitySpec.kt
Last active February 24, 2016 18:26
Example of how Kotlin treats nullability of Java types
import org.jetbrains.spek.api.*
class InteropNullabilitySpec : Spek() {
init {
given("a java object with null state") {
val obj = JavaStringWrapper(null)
on("calling a getter that will return null") {
it("allows me to assign the result to a nullable type") {
val s: String? = obj.value
shouldBeNull(s)
import groovy.transform.*
@CompileStatic
Map<String, ?> makeMap() {
def map = [string: "string"]
map.number = 1
return map
}
def map = makeMap()
import org.jetbrains.spek.api.Spek
class TableSpec : Spek({
describe("using a table") {
table { a: String, b: Int ->
describe("$a and $b are parameters") {
it("should work") {
assert(a is String)
assert(b is Int)
}