All Downloads are FREE. Search and download functionalities are using the official Maven repository.

app.cash.quiver.continuations.OutcomeEagerEffectScope.kt Maven / Gradle / Ivy

Go to download

Quiver library providing extension methods and type aliases to improve Arrow

The newest version!
@file:Suppress("DEPRECATION")

package app.cash.quiver.continuations

import arrow.core.Either
import arrow.core.continuations.EagerEffectScope
import arrow.core.continuations.EffectScope
import arrow.core.continuations.eagerEffect
import arrow.core.continuations.effect
import arrow.core.left
import arrow.core.merge
import arrow.core.right
import app.cash.quiver.Absent
import app.cash.quiver.Failure
import app.cash.quiver.Outcome
import app.cash.quiver.Present

@JvmInline
value class OutcomeEagerEffectScope(private val cont: EagerEffectScope, Absent>>) :
  EagerEffectScope, Absent>> {

  suspend fun  Outcome.bind(): B =
    when (this) {
      is Absent -> shift(Absent.right())
      is Failure -> shift(this.left())
      is Present -> value
    }

  @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
  override suspend fun  shift(r: Either, Absent>): B = cont.shift(r)
}

@JvmInline
value class OutcomeEffectScope(private val cont: EffectScope, Absent>>) :
  EffectScope, Absent>> {

  public suspend fun  Outcome.bind(): B =
    when (this) {
      Absent -> shift(Absent.right())
      is Failure -> shift(this.left())
      is Present -> value
    }

  override suspend fun  shift(r: Either, Absent>): B =
    cont.shift(r)
}

@Suppress("ClassName")
object outcome {
  inline fun  eager(crossinline f: suspend OutcomeEagerEffectScope.() -> A): Outcome =
    eagerEffect {
      @Suppress("ILLEGAL_RESTRICTED_SUSPENDING_FUNCTION_CALL")
      f(OutcomeEagerEffectScope(this))
    }.fold({ it.merge() }, ::Present)

  suspend inline operator fun  invoke(crossinline f: suspend OutcomeEffectScope.() -> A): Outcome =
    effect { f(OutcomeEffectScope(this)) }.fold({ it.merge() }, ::Present)
}