Skip to content

Instantly share code, notes, and snippets.

@colomboe
Created July 18, 2019 09:21
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colomboe/66049b48f35ece7421b1331bfa870195 to your computer and use it in GitHub Desktop.
Save colomboe/66049b48f35ece7421b1331bfa870195 to your computer and use it in GitHub Desktop.
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