Skip to content

Commit

Permalink
Complete Async Type Class (#1198)
Browse files Browse the repository at this point in the history
* add asyncF to Async

* up to date

* Add EitherT Bracket & MonadDefer instances

* Add Async for EitherT. 1 law failing

* Update instances and add OptionT instances

* Add instances and fix constraints

* Complete instances

* Add asyncF for IO & AsyncLaws

* fix instances maybe, deferred and single

* Missing instances

* Rx instances

* Update BracketLaws

* Add cancellation support to Single Bracket

* Add cancellation support to Observable bracket

* Add cancellation support to Maybe bracket

* Add cancellation support to Flowable bracket

* Add test for IO & make test consistent

* Cleanup

* Remove unused code

* Fix detekt

* FluxK Bracket instance

* Clean up

* Fix Reactor bracket instances

* Fix ObservableK docs

* import arrow.effects.maybek.applicative.applicative

* Speed up MonadDeferLaws tests

* DeferredK bracket instance

* Update DeferredK instance to work with Generated & Wrapped

* Add documentation

* Clean up

* DeferredK asyncF

* ObservableK asyncF

* FlowableK asyncF

* Add MaybeK asyncF

* Add SingleK asyncF

* Add instances for monad transformers

* Add cancellable to Async

* Fix build and add shift/cancellableF to Async

* Fix API cancellableF

* Clean up

* Add some documentation for async

* Complete docs Async

* Fix cancelable and fix build

* Clean up and fix cancelable

* Update docs

* Update docs

* Detekt fix

* Revert access modifier change & remove test file

* Add monoid instance Id

* Cleanup IO instance

* Fix docs

* Remove duplicated Id instances

* Fix merge and improve style  instances

* Fix code style & remove debug println

* Remove unused imports
  • Loading branch information
nomisRev authored and raulraja committed Dec 29, 2018
1 parent d110364 commit 824fd46
Show file tree
Hide file tree
Showing 58 changed files with 1,034 additions and 298 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package arrow.common.utils
import arrow.common.messager.logE
import arrow.documented
import arrow.meta.encoder.jvm.KotlinMetatadataEncoder
import me.eugeniomarletti.kotlin.metadata.kotlinMetadata
import me.eugeniomarletti.kotlin.metadata.shadow.metadata.deserialization.receiverType
import me.eugeniomarletti.kotlin.processing.KotlinAbstractProcessor
import java.io.File
import java.io.IOException
Expand Down Expand Up @@ -58,8 +60,14 @@ abstract class AbstractProcessor : KotlinAbstractProcessor(), ProcessorUtils, Ko
ElementKind.INTERFACE -> (this as TypeElement).qualifiedName.toString()
ElementKind.METHOD -> (this as ExecutableElement).let {
val name = (it.enclosingElement as TypeElement).qualifiedName.toString()

val extensionName = (it.enclosingElement.kotlinMetadata?.asClassOrPackageDataWrapper(it.enclosingElement as TypeElement) as? ClassOrPackageDataWrapper.Class)?.let { classData ->
val n = classData.getFunction(it).toMeta(classData, it).receiverType?.simpleName
if (n == classData.simpleName) "" else "$n-"
} ?: ""

val functionName = it.simpleName.toString()
"$name.$functionName"
"$name.$extensionName$functionName"
}
else -> knownError("Unsupported @documented $kind")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ interface PolyTemplateGenerator : MetaApi {
private fun String.replaceMonadDeferImports(info: TypeClassInstance): String {
val monadDeferPackageName = PackageName(info.instance.packageName.value +
"." + info.projectedCompanion.simpleName.substringAfterLast(".").toLowerCase() +
".monaddefer")
".monadDefer")
return replace(
"_imports_monaddefer_",
"""
Expand All @@ -133,8 +133,7 @@ interface PolyTemplateGenerator : MetaApi {
|import ${packageName.value.quote()}.*
|import arrow.core.*
|$factoryImports
|$additionalImports
|""".trimMargin()
|$additionalImports""".trimMargin()
)
}

Expand All @@ -152,8 +151,7 @@ interface PolyTemplateGenerator : MetaApi {
val factory = it.returnType?.simpleName?.decapitalize()?.substringBefore("<") ?: ""
val fact = if (factory == "functor") "monad" else factory
"""|import arrow.instances.id.$fact.$fact
|import arrow.core.*
""".trimMargin()
|import arrow.core.*""".trimMargin()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package arrow.core

import arrow.core.Id
import arrow.instances.eq
import arrow.instances.hash
import arrow.instances.id.applicative.applicative
import arrow.instances.id.comonad.comonad
import arrow.instances.id.eq.eq
import arrow.instances.id.hash.hash
import arrow.instances.id.monad.monad
import arrow.instances.id.monoid.monoid
import arrow.instances.id.show.show
import arrow.instances.id.traverse.traverse
import arrow.instances.id.semigroup.semigroup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class StateT<F, S, A>(
* @param ff function with the [StateT] context.
*/
fun <B> ap(MF: Monad<F>, ff: StateTOf<F, S, (A) -> B>): StateT<F, S, B> =
ff.fix().map2(MF, this) { f, a -> f(a) }
ff.fix().map2(MF, this) { f: (A) -> B, a: A -> f(a) }

/**
* Create a product of the value types of [StateT].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class KleisliTest : UnitSpec() {

testLaws(
BracketLaws.laws(
BF = Kleisli.bracket<ForIO, Int, Throwable>(IO.bracket()),
Kleisli.bracket<ForIO, Int, Throwable>(IO.bracket()),
EQ = IOEQ(),
EQ_EITHER = IOEitherEQ(),
EQERR = IOEQ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface ConstFoldableInstance<A> : Foldable<ConstPartialOf<A>> {
@extension
interface ConstTraverseInstance<X> : Traverse<ConstPartialOf<X>>, ConstFoldableInstance<X> {

override fun <T, U> Kind<ConstPartialOf<X>, T>.map(f: (T) -> U): Const<X, U> = fix().retag()
override fun <T, U> ConstOf<X, T>.map(f: (T) -> U): Const<X, U> = fix().retag()

override fun <G, A, B> ConstOf<X, A>.traverse(AP: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, ConstOf<X, B>> =
fix().traverse(AP, f)
Expand Down Expand Up @@ -108,7 +108,7 @@ class ConstContext<A>(val MA: Monoid<A>) : ConstApplicativeInstance<A>, ConstTra
override fun MA(): Monoid<A> = MA

override fun <T, U> ConstOf<A, T>.map(f: (T) -> U): Const<A, U> =
fix().map(f)
fix().retag()
}

class ConstContextPartiallyApplied<L>(val MA: Monoid<L>) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package arrow.instances

import arrow.Kind
import arrow.Kind2
import arrow.core.*
import arrow.deprecation.ExtensionsDSLDeprecated
import arrow.extension
Expand Down Expand Up @@ -48,12 +47,12 @@ interface EitherMonoidInstance<L, R> : Monoid<Either<L, R>>, EitherSemigroupInst

@extension
interface EitherFunctorInstance<L> : Functor<EitherPartialOf<L>> {
override fun <A, B> Kind<EitherPartialOf<L>, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)
override fun <A, B> EitherOf<L, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)
}

@extension
interface EitherBifunctorInstance : Bifunctor<ForEither> {
override fun <A, B, C, D> Kind2<ForEither, A, B>.bimap(fl: (A) -> C, fr: (B) -> D): Kind2<ForEither, C, D> =
override fun <A, B, C, D> EitherOf<A, B>.bimap(fl: (A) -> C, fr: (B) -> D): Either<C, D> =
fix().bimap(fl, fr)
}

Expand All @@ -62,24 +61,24 @@ interface EitherApplicativeInstance<L> : Applicative<EitherPartialOf<L>>, Either

override fun <A> just(a: A): Either<L, A> = Right(a)

override fun <A, B> Kind<EitherPartialOf<L>, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)
override fun <A, B> EitherOf<L, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)

override fun <A, B> Kind<EitherPartialOf<L>, A>.ap(ff: Kind<EitherPartialOf<L>, (A) -> B>): Either<L, B> =
override fun <A, B> EitherOf<L, A>.ap(ff: EitherOf<L, (A) -> B>): Either<L, B> =
fix().eitherAp(ff)
}

@extension
interface EitherMonadInstance<L> : Monad<EitherPartialOf<L>>, EitherApplicativeInstance<L> {

override fun <A, B> Kind<EitherPartialOf<L>, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)
override fun <A, B> EitherOf<L, A>.map(f: (A) -> B): Either<L, B> = fix().map(f)

override fun <A, B> Kind<EitherPartialOf<L>, A>.ap(ff: Kind<EitherPartialOf<L>, (A) -> B>): Either<L, B> =
override fun <A, B> EitherOf<L, A>.ap(ff: EitherOf<L, (A) -> B>): Either<L, B> =
fix().eitherAp(ff)

override fun <A, B> Kind<EitherPartialOf<L>, A>.flatMap(f: (A) -> Kind<EitherPartialOf<L>, B>): Either<L, B> =
override fun <A, B> EitherOf<L, A>.flatMap(f: (A) -> EitherOf<L, B>): Either<L, B> =
fix().eitherFlatMap { f(it).fix() }

override fun <A, B> tailRecM(a: A, f: (A) -> Kind<EitherPartialOf<L>, Either<A, B>>): Either<L, B> =
override fun <A, B> tailRecM(a: A, f: (A) -> EitherOf<L, Either<A, B>>): Either<L, B> =
Either.tailRecM(a, f)
}

Expand All @@ -88,7 +87,7 @@ interface EitherApplicativeErrorInstance<L> : ApplicativeError<EitherPartialOf<L

override fun <A> raiseError(e: L): Either<L, A> = Left(e)

override fun <A> Kind<EitherPartialOf<L>, A>.handleErrorWith(f: (L) -> Kind<EitherPartialOf<L>, A>): Either<L, A> {
override fun <A> EitherOf<L, A>.handleErrorWith(f: (L) -> EitherOf<L, A>): Either<L, A> {
val fea = fix()
return when (fea) {
is Either.Left -> f(fea.a).fix()
Expand All @@ -103,10 +102,10 @@ interface EitherMonadErrorInstance<L> : MonadError<EitherPartialOf<L>, L>, Eithe
@extension
interface EitherFoldableInstance<L> : Foldable<EitherPartialOf<L>> {

override fun <A, B> Kind<EitherPartialOf<L>, A>.foldLeft(b: B, f: (B, A) -> B): B =
override fun <A, B> EitherOf<L, A>.foldLeft(b: B, f: (B, A) -> B): B =
fix().foldLeft(b, f)

override fun <A, B> Kind<EitherPartialOf<L>, A>.foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> =
override fun <A, B> EitherOf<L, A>.foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> =
fix().foldRight(lb, f)
}

Expand All @@ -116,14 +115,14 @@ fun <G, A, B, C> EitherOf<A, B>.traverse(GA: Applicative<G>, f: (B) -> Kind<G, C
@extension
interface EitherTraverseInstance<L> : Traverse<EitherPartialOf<L>>, EitherFoldableInstance<L> {

override fun <G, A, B> Kind<EitherPartialOf<L>, A>.traverse(AP: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, Kind<EitherPartialOf<L>, B>> =
override fun <G, A, B> EitherOf<L, A>.traverse(AP: Applicative<G>, f: (A) -> Kind<G, B>): Kind<G, EitherOf<L, B>> =
fix().eitherTraverse(AP, f)
}

@extension
interface EitherSemigroupKInstance<L> : SemigroupK<EitherPartialOf<L>> {

override fun <A> Kind<EitherPartialOf<L>, A>.combineK(y: Kind<EitherPartialOf<L>, A>): Either<L, A> =
override fun <A> EitherOf<L, A>.combineK(y: EitherOf<L, A>): Either<L, A> =
fix().eitherCombineK(y)
}

Expand Down Expand Up @@ -171,7 +170,7 @@ interface EitherHashInstance<L, R> : Hash<Either<L, R>>, EitherEqInstance<L, R>
}

class EitherContext<L> : EitherMonadErrorInstance<L>, EitherTraverseInstance<L>, EitherSemigroupKInstance<L> {
override fun <A, B> Kind<EitherPartialOf<L>, A>.map(f: (A) -> B): Either<L, B> =
override fun <A, B> EitherOf<L, A>.map(f: (A) -> B): Either<L, B> =
fix().map(f)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package arrow.instances

import arrow.Kind
import arrow.core.*
import arrow.deprecation.ExtensionsDSLDeprecated
import arrow.extension
import arrow.typeclasses.*

@extension
interface EvalFunctorInstance : Functor<ForEval> {
override fun <A, B> Kind<ForEval, A>.map(f: (A) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.map(f: (A) -> B): Eval<B> =
fix().map(f)
}

@extension
interface EvalApplicativeInstance : Applicative<ForEval> {
override fun <A, B> Kind<ForEval, A>.ap(ff: Kind<ForEval, (A) -> B>): Eval<B> =
override fun <A, B> EvalOf<A>.ap(ff: EvalOf<(A) -> B>): Eval<B> =
fix().ap(ff)

override fun <A, B> Kind<ForEval, A>.map(f: (A) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.map(f: (A) -> B): Eval<B> =
fix().map(f)

override fun <A> just(a: A): Eval<A> =
Expand All @@ -26,16 +25,16 @@ interface EvalApplicativeInstance : Applicative<ForEval> {

@extension
interface EvalMonadInstance : Monad<ForEval> {
override fun <A, B> Kind<ForEval, A>.ap(ff: Kind<ForEval, (A) -> B>): Eval<B> =
override fun <A, B> EvalOf<A>.ap(ff: EvalOf<(A) -> B>): Eval<B> =
fix().ap(ff)

override fun <A, B> Kind<ForEval, A>.flatMap(f: (A) -> Kind<ForEval, B>): Eval<B> =
override fun <A, B> EvalOf<A>.flatMap(f: (A) -> EvalOf<B>): Eval<B> =
fix().flatMap(f)

override fun <A, B> tailRecM(a: A, f: kotlin.Function1<A, EvalOf<Either<A, B>>>): Eval<B> =
Eval.tailRecM(a, f)

override fun <A, B> Kind<ForEval, A>.map(f: (A) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.map(f: (A) -> B): Eval<B> =
fix().map(f)

override fun <A> just(a: A): Eval<A> =
Expand All @@ -44,37 +43,37 @@ interface EvalMonadInstance : Monad<ForEval> {

@extension
interface EvalComonadInstance : Comonad<ForEval> {
override fun <A, B> Kind<ForEval, A>.coflatMap(f: (Kind<ForEval, A>) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.coflatMap(f: (EvalOf<A>) -> B): Eval<B> =
fix().coflatMap(f)

override fun <A> Kind<ForEval, A>.extract(): A =
override fun <A> EvalOf<A>.extract(): A =
fix().extract()

override fun <A, B> Kind<ForEval, A>.map(f: (A) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.map(f: (A) -> B): Eval<B> =
fix().map(f)
}

@extension
interface EvalBimonadInstance : Bimonad<ForEval> {
override fun <A, B> Kind<ForEval, A>.ap(ff: Kind<ForEval, (A) -> B>): Eval<B> =
override fun <A, B> EvalOf<A>.ap(ff: EvalOf<(A) -> B>): Eval<B> =
fix().ap(ff)

override fun <A, B> Kind<ForEval, A>.flatMap(f: (A) -> Kind<ForEval, B>): Eval<B> =
override fun <A, B> EvalOf<A>.flatMap(f: (A) -> EvalOf<B>): Eval<B> =
fix().flatMap(f)

override fun <A, B> tailRecM(a: A, f: kotlin.Function1<A, EvalOf<Either<A, B>>>): Eval<B> =
Eval.tailRecM(a, f)

override fun <A, B> Kind<ForEval, A>.map(f: (A) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.map(f: (A) -> B): Eval<B> =
fix().map(f)

override fun <A> just(a: A): Eval<A> =
Eval.just(a)

override fun <A, B> Kind<ForEval, A>.coflatMap(f: (Kind<ForEval, A>) -> B): Eval<B> =
override fun <A, B> EvalOf<A>.coflatMap(f: (EvalOf<A>) -> B): Eval<B> =
fix().coflatMap(f)

override fun <A> Kind<ForEval, A>.extract(): A =
override fun <A> EvalOf<A>.extract(): A =
fix().extract()
}

Expand Down
Loading

0 comments on commit 824fd46

Please sign in to comment.