Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
@nomisRev
nomisRev / HeliosConverterFactory.kt
Last active August 4, 2018 19:36
Retrofit Call Adapter
import arrow.InstanceParametrizedType
import arrow.instance
import retrofit2.Converter
import okhttp3.ResponseBody
import helios.core.Json
import retrofit2.Retrofit
import java.lang.reflect.Type
import okhttp3.RequestBody
import helios.typeclasses.Encoder
import okhttp3.MediaType
@nomisRev
nomisRev / freemonads.scala
Created July 19, 2018 11:41 — forked from kciesielski/freemonads.scala
Free Monads example
package com.softwaremill.freemonads
import cats.free.Free
import cats.~>
import cats._, cats.std.all._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
sealed trait External[A]
case class Tickets(count: Int) extends AnyVal
@nomisRev
nomisRev / RefExample.kt
Created October 15, 2018 15:40
Cats example ported to Arrow
import arrow.Kind
import arrow.effects.typeclasses.MonadDefer
import arrow.typeclasses.binding
import kotlinx.coroutines.experimental.*
class Counter<F>(private val id: Int,
private val ref: Ref<F, Int>,
private val MD: MonadDefer<F>) {
private fun log(value: String): Kind<F, Unit> = MD { println(value) }
package com.github.nomisRev.androidarchitecture.espresso.rx;
import android.support.test.espresso.idling.CountingIdlingResource;
import java.util.concurrent.TimeUnit;
import io.reactivex.Scheduler;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
@nomisRev
nomisRev / example.kt
Created November 9, 2019 09:44
Impure Pics - Shared state in fp
fun add1(myState: Ref<ForIO, List<String>>): IO<Unit> =
IO.sleep(5.seconds).followedBy(myState.update { it + listOf("#1") })
fun add2(myState: Ref<ForIO, List<String>>): IO<Unit> =
IO.sleep(3.seconds).followedBy(myState.update { it + listOf("#2") })
fun read(myState: Ref<ForIO, List<String>>): IO<Unit> =
myState.get().effectMap { state -> println("$state") }
suspend fun main(): Unit = IO.fx {
@nomisRev
nomisRev / Example.kt
Created November 28, 2019 09:11
Stacksafe Mono Reactor
import reactor.core.publisher.Mono
import java.util.concurrent.Executor
fun noTrampolining(): Mono<Int> {
var initial = Mono.just(0)
(0..5000).forEach { i ->
initial = initial.map { i }
}
return initial
@nomisRev
nomisRev / actors.kt
Created January 22, 2020 19:11
Arrow FS2 Actors example ported
package com.fortyseven.fptraining.slides
import arrow.Kind
import arrow.core.Either
import arrow.core.None
import arrow.core.Option
import arrow.core.Some
import arrow.core.Tuple2
import arrow.core.getOrElse
import arrow.fx.IO
@nomisRev
nomisRev / EventBus.kt
Created March 27, 2020 10:22
EventBus, question on KotlinLang Slack
import arrow.Kind
import arrow.fx.ForIO
import arrow.fx.IO
import arrow.fx.IOOf
import arrow.fx.Queue
import arrow.fx.extensions.fx
import arrow.fx.extensions.io.concurrent.concurrent
import arrow.fx.extensions.io.dispatchers.dispatchers
import arrow.fx.fix
import arrow.fx.typeclasses.Concurrent
@nomisRev
nomisRev / chunks.kt
Created March 27, 2020 10:23
Process chunks with IO - Question on KotlinLang Slack
import arrow.fx.IO
import arrow.fx.extensions.fx
fun chunks(): List<List<String>> = listOf(
listOf("a", "b", "c"),
listOf("d", "e", "g")
)
fun process(prevChunks: List<String>, rest: List<List<String>>, merged: List<String>, size: Int, max: Int): IO<List<String>> =
if (size == max) IO.just(merged)
@nomisRev
nomisRev / CircuitBreaker.kt
Created April 15, 2020 10:11
Simple CircuitBreaker example with Kotlin & Arrow Fx
import arrow.fx.extensions.io.applicative.map
import arrow.fx.extensions.io.monad.flatMap
import arrow.fx.extensions.io.monadDefer.monadDefer
import java.lang.RuntimeException
enum class CBState {
CLOSED, OPEN, DISABLED;
}
class CallNotPermitedException(val state: CBState):