Skip to content

Instantly share code, notes, and snippets.

View nomisRev's full-sized avatar
🏂

Simon Vergauwen nomisRev

🏂
View GitHub Profile
@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 / 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 / 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 {
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 / 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) }
@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 / 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 / example.kt
Created February 7, 2018 16:27
Optics DSL
@lenses data class Street(val number: Int, val name: String)
@lenses data class Address(val city: String, val street: Street)
@lenses data class Company(val name: String, val address: Address)
@lenses data class Employee(val name: String, val company: Company?)
@lenses data class Employees(val employees: ListKW<Employee>)
employees.setter().employees.each().company.nullable.address.street.name
.modify(String::toUpperCase) //modifies all employees companies address street name.
employees.setter().employees.get(0).company.nullable.address.street.name
@nomisRev
nomisRev / BoundSetter.kt
Created February 6, 2018 08:15
Added traversal
import arrow.core.Option
import arrow.core.identity
import arrow.data.ListKW
import arrow.data.ListKWHK
import arrow.data.ListKWKind
import arrow.data.ev
import arrow.data.k
import arrow.lenses
interface BoundSetter<S, A> {
@nomisRev
nomisRev / keyvaluestore.kt
Created November 26, 2017 14:54
KeyValueStore with Free in Kategory
@higherkind sealed class KeyValueStore<A> : KeyValueStoreKind<A> {
companion object : FreeMonadInstance<KeyValueStoreHK>
}
data class Put<A>(val key: String, val value: A) : KeyValueStore<A>()
data class Get<A>(val key: String) : KeyValueStore<Option<A>>()
data class Delete(val key: String) : KeyValueStore<Unit>()
object Clear : KeyValueStore<Unit>()
typealias FreeKeyValueStore<A> = Free<KeyValueStoreHK, A>