This repository has been archived by the owner on Feb 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Use an object to allow default suspend fx constructor and better discovery for eager one * Relocate fx constructors Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
- Loading branch information
Showing
14 changed files
with
216 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
arrow-core-data/src/main/kotlin/arrow/core/computations/const.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package arrow.core.computations | ||
|
||
import arrow.Kind | ||
import arrow.core.Const | ||
import arrow.core.ConstOf | ||
import arrow.core.ConstPartialOf | ||
import arrow.core.EagerBind | ||
import arrow.core.MonadContinuation | ||
import arrow.core.ShortCircuit | ||
import arrow.core.SuspendMonadContinuation | ||
import arrow.core.value | ||
import arrow.typeclasses.suspended.BindSyntax | ||
import kotlin.coroutines.Continuation | ||
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | ||
|
||
object const { | ||
fun <A, T> eager(c: suspend EagerBind<ConstPartialOf<A>>.() -> A): Const<A, T> { | ||
val continuation: ConstContinuation<A, A> = ConstContinuation() | ||
return continuation.startCoroutineUninterceptedAndReturn { | ||
Const.just(c()) | ||
} as Const<A, T> | ||
} | ||
|
||
suspend operator fun <A, T> invoke(c: suspend BindSyntax<ConstPartialOf<A>>.() -> A): Const<A, T> = | ||
suspendCoroutineUninterceptedOrReturn { cont -> | ||
val continuation = ConstSContinuation(cont as Continuation<ConstOf<A, T>>) | ||
continuation.startCoroutineUninterceptedOrReturn { | ||
Const.just(c()) | ||
} | ||
} | ||
|
||
internal class ConstSContinuation<A, T>( | ||
parent: Continuation<ConstOf<A, T>> | ||
) : SuspendMonadContinuation<ConstPartialOf<A>, T>(parent) { | ||
override fun ShortCircuit.recover(): Const<A, T> = | ||
throw this | ||
|
||
override suspend fun <B> Kind<ConstPartialOf<A>, B>.bind(): B = | ||
value() as B | ||
} | ||
|
||
internal class ConstContinuation<A, T> : MonadContinuation<ConstPartialOf<A>, T>() { | ||
override fun ShortCircuit.recover(): Const<A, T> = | ||
throw this | ||
|
||
override suspend fun <B> Kind<ConstPartialOf<A>, B>.bind(): B = | ||
value() as B | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
arrow-core-data/src/main/kotlin/arrow/core/computations/either.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package arrow.core.computations | ||
|
||
import arrow.Kind | ||
import arrow.core.EagerBind | ||
import arrow.core.Either | ||
import arrow.core.EitherOf | ||
import arrow.core.EitherPartialOf | ||
import arrow.core.MonadContinuation | ||
import arrow.core.ShortCircuit | ||
import arrow.core.SuspendMonadContinuation | ||
import arrow.core.fix | ||
import arrow.core.identity | ||
import arrow.typeclasses.suspended.BindSyntax | ||
import kotlin.coroutines.Continuation | ||
import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn | ||
|
||
object either { | ||
fun <E, A> eager(c: suspend EagerBind<EitherPartialOf<E>>.() -> A): Either<E, A> { | ||
val continuation: EitherContinuation<E, A> = EitherContinuation() | ||
return continuation.startCoroutineUninterceptedAndReturn { | ||
Either.Right(c()) | ||
} as Either<E, A> | ||
} | ||
|
||
suspend operator fun <E, A> invoke(c: suspend BindSyntax<EitherPartialOf<E>>.() -> A): Either<E, A> = | ||
suspendCoroutineUninterceptedOrReturn { cont -> | ||
val continuation = EitherSContinuation(cont as Continuation<EitherOf<E, A>>) | ||
continuation.startCoroutineUninterceptedOrReturn { | ||
Either.Right(c()) | ||
} | ||
} | ||
|
||
internal class EitherSContinuation<E, A>( | ||
parent: Continuation<EitherOf<E, A>> | ||
) : SuspendMonadContinuation<EitherPartialOf<E>, A>(parent) { | ||
override fun ShortCircuit.recover(): Kind<EitherPartialOf<E>, A> = | ||
Either.Left(value as E) | ||
|
||
override suspend fun <A> Kind<EitherPartialOf<E>, A>.bind(): A = | ||
fix().fold({ e -> throw ShortCircuit(e) }, ::identity) | ||
} | ||
|
||
internal class EitherContinuation<E, A> : MonadContinuation<EitherPartialOf<E>, A>() { | ||
override fun ShortCircuit.recover(): Kind<EitherPartialOf<E>, A> = | ||
Either.Left(value as E) | ||
|
||
override suspend fun <A> Kind<EitherPartialOf<E>, A>.bind(): A = | ||
fix().fold({ e -> throw ShortCircuit(e) }, ::identity) | ||
} | ||
} |
Oops, something went wrong.