Skip to content
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 10 commits into from
May 27, 2018

Conversation

JorgeCastilloPrz
Copy link
Member

Fixes #860

  • Added MaybeK
  • Added instances
  • Added some tests including law tests for the instances.

@pakoito I've added fold(), foldLeft, foldRight etc to it since I saw those where also defined in other "equivalent" data types like Option and maybe also represents code branching. Also a Foldable instance. But to be honest I'm not 100% sure the code on those methods / instance is correct, but FoldableLaws are passing, which seems suspicious. Could you take a look?

I've not added Traverse instance for it yet since traverse uses to rely on fold and I'm still not confident with my implementation.

Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dooocs

@JorgeCastilloPrz
Copy link
Member Author

Yeees I'll add docs this weekend, but need your help on those folds :D

@pakoito
Copy link
Member

pakoito commented May 25, 2018

IIRC Maybe has the same semantics as Observable, except it's max 1 return.

https://github.com/arrow-kt/arrow/blob/master/modules/effects/arrow-effects-rx2/src/main/kotlin/arrow/effects/ObservableK.kt#L33-L42

@JorgeCastilloPrz
Copy link
Member Author

Given that Single does not provide reduce operation (bc max 1 return), the equivalent to that foldLeft()implementation from ObservableK, but in terms of Single, would be the one I have now (I've redefined it in terms of fold()).

On foldRight() you usually need laziness (for being able to evaluate infinite streams), but here I'm gonna have a single value at most, so I'm a bit concerned about whether that ObservableK impl based on Eval.defer() applies here. Still, I've kept is as deferred eval even for 1 element and redefined when in terms of fold(). It should be equivalent to what you have there.

@JorgeCastilloPrz
Copy link
Member Author

There you got the docs also, back to you @pakoito

@codecov
Copy link

codecov bot commented May 26, 2018

Codecov Report

Merging #862 into master will increase coverage by 0.29%.
The diff coverage is 85.18%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #862      +/-   ##
============================================
+ Coverage     44.85%   45.15%   +0.29%     
- Complexity      628      640      +12     
============================================
  Files           300      302       +2     
  Lines          7709     7763      +54     
  Branches        834      837       +3     
============================================
+ Hits           3458     3505      +47     
- Misses         3947     3954       +7     
  Partials        304      304
Impacted Files Coverage Δ Complexity Δ
...2/src/main/kotlin/arrow/effects/MaybeKInstances.kt 81.81% <81.81%> (ø) 0 <0> (?)
...ffects-rx2/src/main/kotlin/arrow/effects/MaybeK.kt 87.5% <87.5%> (ø) 12 <12> (?)
...ics/src/main/kotlin/arrow/optics/instances/mapk.kt 91.66% <0%> (+4.16%) 0% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5ed0c8d...5ebf7ea. Read the comment docs.

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 {
Copy link
Member

@pakoito pakoito May 26, 2018

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() returns null for no value and A for a value. Given that RxJava2 doesn't allow nulls in Maybe, you can rewrite this to be simpler:

maybe.blockingGet().let {
  if (it == null) ifEmpty() else ifSome(if)
}

Copy link
Member

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.

Copy link
Member Author

@JorgeCastilloPrz JorgeCastilloPrz May 26, 2018

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.

@JorgeCastilloPrz
Copy link
Member Author

Done and done @pakoito 👍

Copy link
Member

@pakoito pakoito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's mergeee!

@JorgeCastilloPrz JorgeCastilloPrz merged commit da7655f into master May 27, 2018
@JorgeCastilloPrz
Copy link
Member Author

👍

@JorgeCastilloPrz JorgeCastilloPrz deleted the add-wrapper-for-rxjava-maybe branch May 27, 2018 07:50
RawToast pushed a commit to RawToast/kategory that referenced this pull request Jul 18, 2018
* add MaybeK

* add MaybeK instances

* add MaybeK tests including law tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants