Skip to content

Instantly share code, notes, and snippets.

@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) {
// 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.
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> {
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>() {
@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
@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
@exallium
exallium / rx_infinite.kt
Created January 5, 2017 19:57
Infinite Rx Sequence Usage
@Test
fun rx_infinite() {
val testSub = TestSubscriber<Int>()
var start = 0
fun inc(): Int {
start += 1
return start
}
@exallium
exallium / rxmvp.kt
Created January 4, 2017 17:45
rxmvp
interface Model {
fun first10Users(): Observable<List<Data>>
}
interface View {
fun displayUserList(users: List<Data>)
fun backButtonClicks(): Observable<ClickEvent>
}
class Presenter {
@exallium
exallium / Either.kt
Last active November 21, 2016 14:42
sealed class Either<L, R> {
class Left<L, R>(val l: L) : Either<L, R>() {
override fun toString(): String = "Left $l"
}
class Right<L, R>(val r: R) : Either<L, R>() {
override fun toString(): String = "Right $r"
}
infix fun <Rp> bind(f: (R) -> (Either<L, Rp>)): Either<L, Rp> {
// Database interface
interface Database {
fun getUserById(id: String): Observable<User>
fun getAllUsers(): Observable<List<User>>
fun getUserChangeEvents(): Observable<ChangeEvent<User>>
}
// Describes what happened to what item
data class ChangeEvent<T>(val eventType: ChangeEventType, val t: T)