streetsofboston
02/11/2019, 9:09 PMKind<F, A>
(and its Monad<F>
as well), how do I pry out the value of type A
?
In other word, how would I implement this extension function?
fun <F,A> Kind<F,A>.evaluateKind(monad: Monad<F>): A {
// TODO Get and return the 'A' typed value from 'this' Kind<F,A>
}
Bob Glamm
02/11/2019, 9:13 PMF
was Option
and the value for Kind<F,A>
was Option.None
what would the return value of A
be?streetsofboston
02/11/2019, 9:16 PMKind<F,A>
to a Try<A>
then, basically changing the F
to a constructed Try
?streetsofboston
02/11/2019, 9:17 PMOption<A>
? Or even A?
🙂raulraja
02/11/2019, 9:21 PMKind<F, A> -> A
it's Comonad extract
but it does not apply to the types you are dealing with. That function can't be implemented without blocking or throwing exceptions. That is why it's suspended. What you want is bind
but it will always return the value back in the wrapper.raulraja
02/11/2019, 9:22 PMraulraja
02/11/2019, 9:22 PMstreetsofboston
02/11/2019, 9:27 PMfun <F,A> sideEffectValue(monad: Monad<F>, f: suspend () -> A): Kind<F, A> {
return monad.fx { !effect(f) }
}
fun main() {
val ioValue = sideEffectValue(IO.monad()) { "Hello" }
val eitherValue = sideEffectValue(Either.monad<Int>()) { "Hello "}
}
streetsofboston
02/11/2019, 9:29 PMioValue
or the eitherValue
, if the only thing I know they are a Kind<F, String>
?raulraja
02/11/2019, 9:30 PMimport <http://arrow.effects.IO|arrow.effects.IO>
import arrow.unsafe
import arrow.effects.extensions.io.unsafeRun.runBlocking
import arrow.effects.extensions.io.fx.fx
suspend fun sayHello(): Unit =
println("Hello World")
suspend fun sayGoodBye(): Unit =
println("Good bye World!")
fun greet(): IO<Unit> =
fx {
!effect { sayHello() }
!effect { sayGoodBye() }
}
fun main() { // The edge of our world
unsafe { runBlocking { greet() } }
}
raulraja
02/11/2019, 9:30 PMraulraja
02/11/2019, 9:30 PMstreetsofboston
02/11/2019, 9:31 PMIO
. I could use Function0
as well. How can I make this agnostic on which one is exactly used?raulraja
02/11/2019, 9:31 PMraulraja
02/11/2019, 9:32 PM@extension
interface IOUnsafeRun : UnsafeRun<ForIO> {
override suspend fun <A> unsafe.runBlocking(fa: () -> Kind<ForIO, A>): A = fa().fix().unsafeRunSync()
override suspend fun <A> unsafe.runNonBlocking(fa: () -> Kind<ForIO, A>, cb: (Either<Throwable, A>) -> Unit) =
fa().fix().unsafeRunAsync(cb)
}
raulraja
02/11/2019, 9:32 PMstreetsofboston
02/11/2019, 9:32 PMraulraja
02/11/2019, 9:32 PMstreetsofboston
02/11/2019, 9:33 PMIO
, Function0
, and other ‘effectful’ monads.raulraja
02/11/2019, 9:33 PMstreetsofboston
02/11/2019, 9:33 PMraulraja
02/11/2019, 9:33 PMraulraja
02/11/2019, 9:34 PMstreetsofboston
02/11/2019, 9:35 PMstreetsofboston
02/11/2019, 9:36 PMraulraja
02/11/2019, 9:44 PMEither
value for example you can get A
on the right by calling either.getOrRaiseError { throw it }
raulraja
02/11/2019, 9:45 PMraulraja
02/11/2019, 9:45 PM