This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface View { | |
| fun textChanged(): Single<String> | |
| fun getText(): String | |
| } | |
| interface Model { | |
| fun validateText(text: String) Boolean | |
| } | |
| class Presenter(view: View, model: Model) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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>() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface View { | |
| fun clicks(): Observable<Unit> | |
| } | |
| interface Model { | |
| fun submits(): Observable<Unit> | |
| } | |
| class Presenter(val view: View, val model: Model) { | |
| fun setUp() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |