Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package experiment
import arrow.Kind
import arrow.core.Either
import arrow.core.Option
import arrow.effects.ForIO
import arrow.effects.IO
import arrow.effects.extensions.fx
import arrow.effects.extensions.io.concurrent.concurrent
import arrow.effects.extensions.io.unsafeRun.runBlocking
import arrow.effects.fix
import arrow.effects.typeclasses.Concurrent
import arrow.effects.typeclasses.ConcurrentFx
import arrow.unsafe
interface HasSideEffectHandling<F> : Concurrent<F> {
suspend fun <A> Kind<F, A>.asSuspended(): A
suspend fun <A> attempt(f: suspend () -> A): Either<Throwable, A>
}
object IOSideEffectHandling : HasSideEffectHandling<ForIO>,
Concurrent<ForIO> by IO.concurrent() {
override suspend fun <A> Kind<ForIO, A>.asSuspended(): A {
return this.fix().suspended()
}
override suspend fun <A> attempt(f: suspend () -> A): Either<Throwable, A> {
return effect(f).attempt().asSuspended()
}
}
// ----------------------------------------------------------
interface HasConsole<ENV> {
suspend fun ENV.printToConsole(s: String)
suspend fun ENV.readFromConsole(): Option<String>
}
interface LiveConsole<F, ENV> : HasConsole<ENV>
where ENV : HasSideEffectHandling<F> {
override suspend fun ENV.printToConsole(s: String) {
println(s)
}
override suspend fun ENV.readFromConsole(): Option<String> {
return attempt { readLine() }
.fold({ Option.empty() },
{ Option.fromNullable(it) })
}
}
// -------------------------------------------------------------
interface Env: HasConsole<Env>, HasSideEffectHandling<ForIO>
val env: Env = object : Env,
HasConsole<Env> by object : LiveConsole<ForIO, Env> {},
HasSideEffectHandling<ForIO> by IOSideEffectHandling
{}
private suspend fun Env.program(): Option<String> {
printToConsole("Hello")
return readFromConsole()
}
fun main() {
val x = unsafe { runBlocking {
env.effect { env.program() }
} }
println(x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.