-
Notifications
You must be signed in to change notification settings - Fork 451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add MaybeK wrapper for RxJava Maybe #862
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ad2b3a1
add MaybeK
JorgeCastilloPrz ad2a368
add MaybeK instances
JorgeCastilloPrz 5073d43
add MaybeK tests including law tests
JorgeCastilloPrz 7106999
add foldable instance
JorgeCastilloPrz d319d8d
redefine foldLeft and foldRight in terms of fold
JorgeCastilloPrz ed92f6f
add docs
JorgeCastilloPrz 524bb57
refactor fold to expression
JorgeCastilloPrz e1b3544
Merge branch 'master' into add-wrapper-for-rxjava-maybe
pakoito cf96dc4
fix fold when empty and add tests
JorgeCastilloPrz 5ebf7ea
Merge branch 'add-wrapper-for-rxjava-maybe' of github.com:arrow-kt/ar…
JorgeCastilloPrz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
83 changes: 83 additions & 0 deletions
83
modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeK.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,83 @@ | ||
package arrow.effects | ||
|
||
import arrow.core.* | ||
import arrow.effects.typeclasses.Proc | ||
import arrow.higherkind | ||
import io.reactivex.Maybe | ||
import io.reactivex.MaybeEmitter | ||
|
||
fun <A> Maybe<A>.k(): MaybeK<A> = MaybeK(this) | ||
|
||
fun <A> MaybeKOf<A>.value(): Maybe<A> = this.fix().maybe | ||
|
||
@higherkind | ||
data class MaybeK<A>(val maybe: Maybe<A>) : MaybeKOf<A>, MaybeKKindedJ<A> { | ||
|
||
fun <B> map(f: (A) -> B): MaybeK<B> = | ||
maybe.map(f).k() | ||
|
||
fun <B> ap(fa: MaybeKOf<(A) -> B>): MaybeK<B> = | ||
flatMap { a -> fa.fix().map { ff -> ff(a) } } | ||
|
||
fun <B> flatMap(f: (A) -> MaybeKOf<B>): MaybeK<B> = | ||
maybe.flatMap { f(it).fix().maybe }.k() | ||
|
||
fun <B> fold(ifEmpty: () -> B, ifSome: (A) -> B): B = when { | ||
maybe.isEmpty.blockingGet() -> ifEmpty() | ||
else -> ifSome(this.maybe.blockingGet()) | ||
} | ||
|
||
fun <B> foldLeft(b: B, f: (B, A) -> B): B = | ||
fold({ b }, { a -> f(b, a) }) | ||
|
||
fun <B> foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> = | ||
Eval.defer { fold({ lb }, { a -> f(a, lb) }) } | ||
|
||
fun isEmpty(): Boolean = maybe.isEmpty.blockingGet() | ||
|
||
fun nonEmpty(): Boolean = !isEmpty() | ||
|
||
fun exists(predicate: Predicate<A>): Boolean = fold({ false }, { a -> predicate(a) }) | ||
|
||
fun forall(p: Predicate<A>): Boolean = fold({ true }, p) | ||
|
||
fun handleErrorWith(function: (Throwable) -> MaybeK<A>): MaybeK<A> = | ||
maybe.onErrorResumeNext { t: Throwable -> function(t).maybe }.k() | ||
|
||
fun runAsync(cb: (Either<Throwable, A>) -> MaybeKOf<Unit>): MaybeK<Unit> = | ||
maybe.flatMap { cb(Right(it)).value() }.onErrorResumeNext(io.reactivex.functions.Function { cb(Left(it)).value() }).k() | ||
|
||
companion object { | ||
fun <A> just(a: A): MaybeK<A> = | ||
Maybe.just(a).k() | ||
|
||
fun <A> raiseError(t: Throwable): MaybeK<A> = | ||
Maybe.error<A>(t).k() | ||
|
||
operator fun <A> invoke(fa: () -> A): MaybeK<A> = | ||
defer { just(fa()) } | ||
|
||
fun <A> defer(fa: () -> MaybeKOf<A>): MaybeK<A> = | ||
Maybe.defer { fa().value() }.k() | ||
|
||
fun <A> async(fa: Proc<A>): MaybeK<A> = | ||
Maybe.create({ emitter: MaybeEmitter<A> -> | ||
fa { either: Either<Throwable, A> -> | ||
either.fold({ | ||
emitter.onError(it) | ||
}, { | ||
emitter.onSuccess(it) | ||
}) | ||
|
||
} | ||
}).k() | ||
|
||
tailrec fun <A, B> tailRecM(a: A, f: (A) -> MaybeKOf<Either<A, B>>): MaybeK<B> { | ||
val either = f(a).fix().value().blockingGet() | ||
return when (either) { | ||
is Either.Left -> tailRecM(either.a, f) | ||
is Either.Right -> Maybe.just(either.b).k() | ||
} | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/MaybeKInstances.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,115 @@ | ||
package arrow.effects | ||
|
||
import arrow.Kind | ||
import arrow.core.Either | ||
import arrow.core.Eval | ||
import arrow.effects.typeclasses.Async | ||
import arrow.effects.typeclasses.Effect | ||
import arrow.effects.typeclasses.MonadDefer | ||
import arrow.effects.typeclasses.Proc | ||
import arrow.instance | ||
import arrow.typeclasses.* | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKFunctorInstance : Functor<ForMaybeK> { | ||
override fun <A, B> Kind<ForMaybeK, A>.map(f: (A) -> B): MaybeK<B> = | ||
fix().map(f) | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKApplicativeInstance : Applicative<ForMaybeK> { | ||
override fun <A, B> MaybeKOf<A>.ap(ff: MaybeKOf<(A) -> B>): MaybeK<B> = | ||
fix().ap(ff) | ||
|
||
override fun <A, B> Kind<ForMaybeK, A>.map(f: (A) -> B): MaybeK<B> = | ||
fix().map(f) | ||
|
||
override fun <A> just(a: A): MaybeK<A> = | ||
MaybeK.just(a) | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKMonadInstance : Monad<ForMaybeK> { | ||
override fun <A, B> MaybeKOf<A>.ap(ff: MaybeKOf<(A) -> B>): MaybeK<B> = | ||
fix().ap(ff) | ||
|
||
override fun <A, B> MaybeKOf<A>.flatMap(f: (A) -> Kind<ForMaybeK, B>): MaybeK<B> = | ||
fix().flatMap(f) | ||
|
||
override fun <A, B> MaybeKOf<A>.map(f: (A) -> B): MaybeK<B> = | ||
fix().map(f) | ||
|
||
override fun <A, B> tailRecM(a: A, f: kotlin.Function1<A, MaybeKOf<arrow.core.Either<A, B>>>): MaybeK<B> = | ||
MaybeK.tailRecM(a, f) | ||
|
||
override fun <A> just(a: A): MaybeK<A> = | ||
MaybeK.just(a) | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKFoldableInstance : Foldable<ForMaybeK> { | ||
|
||
override fun <A, B> Kind<ForMaybeK, A>.foldLeft(b: B, f: (B, A) -> B): B = | ||
fix().foldLeft(b, f) | ||
|
||
override fun <A, B> Kind<ForMaybeK, A>.foldRight(lb: Eval<B>, f: (A, Eval<B>) -> Eval<B>): Eval<B> = | ||
fix().foldRight(lb, f) | ||
|
||
override fun <A> Kind<ForMaybeK, A>.isEmpty(): Boolean = | ||
fix().isEmpty() | ||
|
||
override fun <A> Kind<ForMaybeK, A>.exists(p: (A) -> Boolean): Boolean = | ||
fix().exists(p) | ||
|
||
override fun <A> MaybeKOf<A>.forAll(p: (A) -> Boolean): Boolean = | ||
fix().forall(p) | ||
|
||
override fun <A> Kind<ForMaybeK, A>.nonEmpty(): Boolean = | ||
fix().nonEmpty() | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKApplicativeErrorInstance : | ||
MaybeKApplicativeInstance, | ||
ApplicativeError<ForMaybeK, Throwable> { | ||
override fun <A> raiseError(e: Throwable): MaybeK<A> = | ||
MaybeK.raiseError(e) | ||
|
||
override fun <A> MaybeKOf<A>.handleErrorWith(f: (Throwable) -> MaybeKOf<A>): MaybeK<A> = | ||
fix().handleErrorWith { f(it).fix() } | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKMonadErrorInstance : | ||
MaybeKMonadInstance, | ||
MonadError<ForMaybeK, Throwable> { | ||
override fun <A> raiseError(e: Throwable): MaybeK<A> = | ||
MaybeK.raiseError(e) | ||
|
||
override fun <A> MaybeKOf<A>.handleErrorWith(f: (Throwable) -> MaybeKOf<A>): MaybeK<A> = | ||
fix().handleErrorWith { f(it).fix() } | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKMonadDeferInstance : | ||
MaybeKMonadErrorInstance, | ||
MonadDefer<ForMaybeK> { | ||
override fun <A> defer(fa: () -> MaybeKOf<A>): MaybeK<A> = | ||
MaybeK.defer(fa) | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKAsyncInstance : | ||
MaybeKMonadDeferInstance, | ||
Async<ForMaybeK> { | ||
override fun <A> async(fa: Proc<A>): MaybeK<A> = | ||
MaybeK.async(fa) | ||
} | ||
|
||
@instance(MaybeK::class) | ||
interface MaybeKEffectInstance : | ||
MaybeKAsyncInstance, | ||
Effect<ForMaybeK> { | ||
override fun <A> MaybeKOf<A>.runAsync(cb: (Either<Throwable, A>) -> MaybeKOf<Unit>): MaybeK<Unit> = | ||
fix().runAsync(cb) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to RxJava's docs,
this.maybe.blockingGet()
returnsnull
for no value andA
for a value. Given that RxJava2 doesn't allow nulls inMaybe
, you can rewrite this to be simpler:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest adding a new test for it too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll address both comments tomorrow. Out of home today, Champions League finals tonight 💥
If you're hurry for a release, feel free to push those changes. Otherwise I'll do.