Skip to content

Instantly share code, notes, and snippets.

View bdavisx's full-sized avatar

Bill Davis bdavisx

  • You Are Here...
View GitHub Profile
@bdavisx
bdavisx / DirectCallVerticle.kt
Created August 6, 2018 10:57
Vert.x vertx Verticle that can be called Directly
package com.tartner.vertx
import io.vertx.core.eventbus.*
import io.vertx.kotlin.coroutines.*
import kotlinx.coroutines.experimental.*
import java.util.*
sealed class CodeMessage<T: Any>(val block: suspend (DirectCallVerticle) -> T)
class ReturnValueCodeMessage<T: Any>(block: suspend (DirectCallVerticle) -> T): CodeMessage<T>(block)
class UnitCodeMessage(block: suspend (DirectCallVerticle) -> Unit): CodeMessage<Unit>(block)
@bdavisx
bdavisx / DirectCallVerticle.kt
Last active July 14, 2018 18:35
Example Code for a verticle you can call directly, but the code will run on the event loop thread.
package com.tartner.vertx
import io.vertx.core.eventbus.*
import io.vertx.kotlin.coroutines.*
import kotlinx.coroutines.experimental.*
import java.util.*
sealed class CodeMessage<T: Any>(val block: suspend () -> T)
class ReturnValueCodeMessage<T: Any>(block: suspend () -> T): CodeMessage<T>(block)
class UnitCodeMessage(block: suspend () -> Unit): CodeMessage<Unit>(block)
@bdavisx
bdavisx / Uncurry.kt
Last active November 1, 2016 23:45
import org.jetbrains.spek.api.Spek 2 import org.jetbrains.spek.api.dsl.describe 3 import org.jetbrains.spek.api.dsl.it 4 import org.junit.Assert.assertEquals 5 ​ 6 fun <A,B,C> uncurry(function: (A) -> ((B) -> C)): (A,B) -> C { 7 return fun(a: A, b: B): C { 8 return function(a)(b) 9 } 10 } 11 ​ 12 class UncurryTest: Spek({ 13 describe("uncurry fu…
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.junit.Assert.assertEquals
fun <A,B,C> uncurry(function: (A) -> ((B) -> C)): (A,B) -> C {
return fun(a: A, b: B): C {
return function(a)(b)
}
}
@bdavisx
bdavisx / Curry.kt
Last active November 1, 2016 23:45
import org.jetbrains.spek.api.Spek 2 import org.jetbrains.spek.api.dsl.describe 3 import org.jetbrains.spek.api.dsl.it 4 import org.junit.Assert.assertEquals 5 ​ 6 fun <A,B,C> curry(function: (A,B) -> C): (A) -> ((B) -> C) { 7 return fun (a: A): (B) -> C { 8 return fun (b: B): C { 9 return function(a, b) 10 } 11 } 12 } 13 ​ 14 class CurryTest: S…
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.junit.Assert.assertEquals
fun <A,B,C> curry(function: (A,B) -> C): (A) -> ((B) -> C) {
return fun (a: A): (B) -> C {
return fun (b: B): C {
return function(a, b)
}
class Ch02Test : Spek({
val expectedValues = arrayOf(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610,
987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811)
fun runFibonacciTests(function: (Int) -> Int) {
it("should return the correct first value") {
assertEquals(0, function(0))
}
it("should return the correct values in expectedValues") {
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import kotlin.test.assertFalse
import kotlin.test.assertTrue
fun <A> isSorted(array: Array<A>, ordered: (A,A) -> Boolean): Boolean {
tailrec fun loop(current: Int): Boolean {
when(current) {
array.size -> return true
fun fibonacci2(n: Int): Int {
tailrec fun loop(n: Int, previous: Int, current: Int): Int {
return if( n == 0 ) {
previous
} else {
loop(n-1, current, previous+current)
}
}
return loop(n, 0, 1)
import org.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.it
import org.junit.Assert
fun fibonacci(n: Int): Int {
tailrec fun loop(n: Int, previous: Int, current: Int): Int {
return when(n) {
0 -> previous
else -> loop(n-1, current, previous+current)
@bdavisx
bdavisx / Curry.kt
Created October 29, 2016 14:57
Kotlin Curry Function
fun <A,B,C> curry(function: (A,B) -> C): (A) -> (B) -> C {
return fun (a: A): (B) -> C {
return fun (b: B): C {
return function(a, b)
}
}
}
using System;
namespace EventRecorderExample {
public class Customer {
private IEventRecorder eventRecorder = EventRecorder.CreateEventRecorder();
// rest of implementation...
}
public class EventRecorder : IEventRecorder {