Skip to content

Instantly share code, notes, and snippets.

@LordRaydenMK
LordRaydenMK / sum_types_cardinality.kt
Created January 29, 2021 21:32
Cardinality of sum types
sealed class NotificationSetting
object Disabled : NotificationSettings() // an object has one element -> cardinality = 1
data class Enabled(val pushEnabled: Boolean, val emailEnabled: Boolean) : NotificationSettings()
// cardinality = cardinality (Disabled) + cardinality(Enabled)
// cardinality = 1 + (2 * 2)
// cardinality = 1 + 4 = 5
sealed class Location
object Unknown : Location()
@LordRaydenMK
LordRaydenMK / utc_tests_2.kt
Created January 9, 2021 09:46
Tests part 2
@Test
fun `execute - first call succeeds, one failed call to #2 - one full item, one item with default value`() {
val items = listOf("Item 1" to "https://item1.jpg", "Item 2" to "invalid url")
val service = successService(items)
val viewModel = ViewModel(service)
val expected = listOf(
ViewEntity("Item 1", "https://item1.jpg".toHttpUrl()),
ViewEntity("Item 2", null)
@LordRaydenMK
LordRaydenMK / utc_tests_1.kt
Created January 9, 2021 09:45
Tests part v1
@Test
fun `execute - first call succeeds, one failed call to #2 - one full item, one item with default value`() {
val items = listOf("Item 1" to "https://item1.jpg", "Item 2" to "invalid url")
val service = successService(items) // Fake implementation of a retrofit service, always returns success
val viewModel = ViewModel(service)
val expected = listOf(
ViewEntity("Item 1", "https://item1.jpg".toHttpUrl()),
ViewEntity("Item 2", null)
@LordRaydenMK
LordRaydenMK / utc_1.kt
Last active January 9, 2021 09:43
Unit tests and concurrency 1
// Emits Loading then Content or Problem
private fun requestData(): Observable<ViewState> =
service.firstApiCall()
.observeOn(Schedulers.computation())
.map { it.message }
.flatMap(this::secondApiCall)
.map<ViewState> { ViewState.Content(it) }
.startWith(Single.just(ViewState.Loading))
.onErrorReturn { ViewState.Problem }
.toObservable()
@LordRaydenMK
LordRaydenMK / Parser.kt
Last active June 1, 2020 08:57
Kotlin parser combinator
sealed class Result<out A> {
data class Success<A>(val value: A) : Result<A>()
data class Failure(val msg: String) : Result<Nothing>()
companion object {
fun <A> success(value: A): Result<A> = Success(value)
fun <A> failure(msg: String): Result<A> = Failure(msg)
}
@LordRaydenMK
LordRaydenMK / Hangman.kt
Created March 26, 2020 18:34
Functional Hangman with IO<E, A> from Arrow
package io.github.lordraydenmk.hangman
import arrow.fx.IO
import arrow.fx.extensions.fx
import arrow.fx.flatMap
import java.io.IOException
import kotlin.random.Random
import kotlin.streams.toList
// region utils
@LordRaydenMK
LordRaydenMK / ValueClass.kt
Created February 2, 2020 22:09
Kotlin compiler plugin built with arrow-meta. Generates equals, hashCode and toString for classes annotated with @valueclass
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
annotation class ValueClass
@LordRaydenMK
LordRaydenMK / PolyHangman.kt
Last active November 17, 2018 20:09
Functional Hangman abstracted over the container type
package io.github.lordraydenmk.tf
import arrow.Kind
import arrow.core.getOrElse
import arrow.core.right
import arrow.core.toOption
import arrow.effects.IO
import arrow.effects.SingleK
import arrow.effects.fix
import arrow.effects.instances.io.monad.flatMap
@LordRaydenMK
LordRaydenMK / NetworkModule.java
Created January 16, 2018 20:46
Dagger dependency injection - network module
@Module
public class NetworkModule {
@Provides
OkHttpClient provideOkHttpClient() {
return new OkHttpClient.Builder()
.build();
}
}
public class MyAwesomeClass {
private final MyClass myClass;
@Inject
public MyAwesomeClass(MyClass myClass) {
this.myClass = myClass;
}
}