Skip to content

Instantly share code, notes, and snippets.

@exallium
exallium / verifier.kt
Created January 17, 2017 19:34
Simple Monoid Verifier
// Our Monoid interface, which provides an "append" and "empty"
interface Monoid<M> {
fun append(l: M, r: M): M
fun empty(): M
}
sealed class ValidationResult {
companion object : Monoid<ValidationResult> {
// Appends two validation results. If both are failures, append messages with newline
package com.exallium.librarytestbed
sealed class Interactor<out T> {
class Tell<out T>(val msg: String, val t: T) : Interactor<T>() {
override fun <B> flatMap(f: (T) -> B): Interactor<B> {
return Tell(msg, f(t))
}
}
class Ask<out T>(val k: (String) -> T) : Interactor<T>() {
package com.exallium.librarytestbed
/**
* Represents actions with which we can interact with a user.
*
* @param T Should be an instance of Interactor<*>?. Due to type-system limitations,
* this is simply left without an upper bounds. Adding this type restriction
* violates the Finite Bounds Restriction and is a compilation warning.
*/
sealed class Interactor<out T> {
// In Kotlin, Functions are "first class" members. This means that you can pass them around.
// For example, a function can be expressed utilizing lambda syntax like so:
val f1: (Int, Int) -> Int = { a, b -> a + b }
// this can be read as follows
// the value (val) f1 is a function which takes two ints (Int, Int) and returns (->) an Int.
// the funciton body has two parameters a, b which it adds together.
// In a lambda expression like this, the result of the last line is also the return statement
// The exception is if the return type is Unit, in which case there is no return statement.
@exallium
exallium / example.kt
Created March 6, 2017 15:31
simple textChanged example
interface View {
fun textChanged(): Single<String>
fun getText(): String
}
interface Model {
fun validateText(text: String) Boolean
}
class Presenter(view: View, model: Model) {
@exallium
exallium / debouncetest.kt
Last active March 16, 2017 14:04
unit testing a debounce
@Test
fun debounce_test() {
// TestScheduler lets us control time ;)
val testScheduler = TestScheduler()
// This basically represents a button.
val testClicks = PublishSubject<Long>()
// This'll let us verify our interactions
val testSubscriber = TestSubscriber<Long>()
@exallium
exallium / debounceEx2.kt
Last active March 17, 2017 15:13
debounceEx2
interface View {
fun clicks(): Observable<Unit>
}
interface Model {
fun submits(): Observable<Unit>
}
class Presenter(val view: View, val model: Model) {
fun setUp() {
@exallium
exallium / firebaseRepo.kt
Last active June 20, 2017 17:21
Firebase Callback to Rx Observable
private class ValueEventListenerToEmitter(private val emitter: Emitter<DataSnapshot>) : ValueEventListener {
override fun onCancelled(error: DatabaseError) {
emitter.onError(error.toException())
}
override fun onDataChange(snapshot: DataSnapshot) {
emitter.onNext(snapshot)
}
}
@exallium
exallium / scan.kt
Created August 10, 2017 15:13
Quick and Dirty Scan function in Kotlin
fun <T> List<T>.scan(fn: (T, T) -> T): List<T> {
return if (this.isEmpty()) this
else this.drop(1).fold(listOf(this.first()), { acc, item ->
acc.plus(fn(acc.last(), item))
})
}
println((1..5).toList().scan { x, y -> x + y })
data Parser = WebParser | DbParser
class ParseStrategy p where
parse :: p -> String -> String
instance ParseStrategy Parser where
parse WebParser s = "code for parsing web stuff goes here"
parse DbParser s = "code for parsing db stuff goes here"
fromDB = parse DbParser