Hello, I was wondering how to convert `IO` to `Mon...
# arrow
c
Hello, I was wondering how to convert
IO
to
MonoK
nicely, could anybody help?
s
Hey Chris, These are the methods you can use to transform between
MonoK
and
IO
. These should be added to Arrow Fx and its integration libs.
Copy code
import arrow.core.Left
import arrow.core.Right
import <http://arrow.fx.IO|arrow.fx.IO>
import reactor.core.publisher.Mono

fun <A> IO<A>.toMonoK(): MonoK<A> =
  MonoK(Mono.create<A> { sink ->
    val dispose = unsafeRunAsyncCancellable { result ->
      result.fold(sink::error, sink::success)
    }
    sink.onCancel { dispose.invoke() }
  })

fun <A> MonoK<A>.toIO(): IO<A> =
  IO.cancelable { cb ->
    val dispose = value().subscribe({ a -> cb(Right(a)) }, { e -> cb(Left(e)) })
    IO { dispose.dispose() }
  }
❤️ 2
🔝 2
c
Sweet, thanks very much!
s
You’re welcome!