Skip to content

Instantly share code, notes, and snippets.

Raúl Raja Martínez raulraja

View GitHub Profile
@raulraja
raulraja / IO.kt
Created Apr 17, 2020
Kinds without uncheched casts
View IO.kt
/** Bifunctor IO */
class IO<out E, out A> :
`*`<IO<*, *>>,
`*-*`<IO<*, *>, A>,
`*-*-*`<IO<*, *>, E, A> {
fun <B> fmap(f: (A) -> B): IO<Nothing, B> = TODO()
fun <B> flatMap(f: (A) -> `*-*`<IO<*, *>, B>): IO<Nothing, B> = TODO()
fun <C, D> bimap(fl: (E) -> C, fr: (A) -> D): IO<C, D> = TODO()
companion object {
@raulraja
raulraja / AutoCancellation.kt
Last active Mar 8, 2020
Arrow Fx. Auto cancels without leaving to user discipline to check for `isAlive`. Examples by @nomisRev
View AutoCancellation.kt
import arrow.fx.*
import arrow.fx.extensions.*
import arrow.fx.typeclasses.*
import arrow.fx.extensions.io.bracket.guaranteeCase
fun <A> IOOf<A>.onCancel(token: IOOf<Unit>): IO<A> =
guaranteeCase { case ->
when (case) {
ExitCase.Canceled -> token
else -> IO.unit
@raulraja
raulraja / rules.kt
Created Mar 5, 2020
Validation rules abstracting strategies with operations as extension functions
View rules.kt
package arrow.rules.test
import arrow.Kind
import arrow.core.Either
import arrow.core.EitherPartialOf
import arrow.core.Nel
import arrow.core.NonEmptyList
import arrow.core.Validated
import arrow.core.ValidatedPartialOf
import arrow.core.extensions.either.applicativeError.applicativeError
@raulraja
raulraja / TwitterHandle.kt
Created Feb 20, 2020
Type Refinements with Type Proofs in Kotlin
View TwitterHandle.kt
/* Coming up ~ April 2020 */
package test
import arrow.*
inline class TwitterHandle(val handle: String) {
companion object : Refined<String> {
override val validate: String.() -> Map<String, Boolean> = {
mapOf(
"Should start with '@'" to startsWith("@"),
@raulraja
raulraja / prelude.kt
Created Jan 7, 2020
Daydreaming proofs as contracts
View prelude.kt
package arrow.meta.ide.plugins.proofs.contracts
import kotlin.contracts.Effect
import kotlin.contracts.ExperimentalContracts
interface TypeLevelEffect : Effect
interface Declaration : Effect
fun declaration(): Declaration = TODO()
View toStringReplacement.meta.kt
package arrow.meta.plugins.helloWorld
import arrow.meta.Meta
import arrow.meta.Plugin
import arrow.meta.invoke
import arrow.meta.phases.analysis.ElementScope
import arrow.meta.quotes.ClassScope
import arrow.meta.quotes.Scope
import arrow.meta.quotes.Transform
import arrow.meta.quotes.classOrObject
View DefaultDicitionary.kt
package dictionary
/**
* A default dictionary is a map that provides a default concrete Monoid empty value for [B]
* Inspired by some of the examples of https://kotlinexpertise.com/default-map-in-kotlin/
* https://twitter.com/raulraja/status/1141697899383992325
*/
data class DefaultDictionary<A, B>(val empty: B, val underlying: Map<A, B> = emptyMap()) : Map<A, B> by underlying {
/**
@raulraja
raulraja / di.kt
Last active Jun 8, 2020
Simple Kotlin DI
View di.kt
package com.fortyseven.fptraining
import arrow.core.Either
import arrow.core.right
import arrow.effects.IO
import arrow.effects.extensions.io.fx.fx
data class Account(val balance: Int)
data class AccountEntity(val balance: Int)
@raulraja
raulraja / Capabilities.kt
Last active Feb 26, 2019
Kotlin transformers encoding for `Death of Final Tagless` by John De Goes.
View Capabilities.kt
package arrow.effects.zio.example
/** Capabilities aggregation ala Kotlin **/
class Capabilities(
console: Console,
random: Random
) : Console by console, Random by random
@raulraja
raulraja / ArrowTrial.kt
Last active Jan 6, 2019 — forked from bjonnh/ArrowTrial.kt
How to run suspend function in Arrow IOs
View ArrowTrial.kt
import arrow.core.left
import arrow.core.right
import arrow.effects.IO
import arrow.effects.extensions.io.applicativeError.handleErrorWith
import arrow.effects.extensions.io.monad.binding
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import java.time.LocalDateTime
You can’t perform that action at this time.