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

Prefer more specific types for flatMap #923

Open
johnmcclean opened this issue Nov 5, 2018 · 1 comment
Open

Prefer more specific types for flatMap #923

johnmcclean opened this issue Nov 5, 2018 · 1 comment

Comments

@johnmcclean
Copy link
Member

When the type changes it breaks left identity

Future<Integer> bindAndUnit = Future.ofResult(10).flatMap(i->Future.ofResult(i+10)));
//Future[20]

Future<Integer> applyF = Lambda.λ(i->Future.ofResult(i+10)))
                                                         .apply(10);
//Future[20]

But changing the Function to return a Maybe or Option also works, but breaks the law
java i->Maybe.just(10)

Future<Integer> bindAndUnit = Future.ofResult(10).flatMap(i->Maybe.just(i+10)));
//Future[20]

Maybe<Integer> applyF = Lambda.λ(i->Maybe.just(i+10)))
                                                         .apply(10);
//Maybe[20]

Areas affected

  • All sub-types of MonadicValue (Eval, Future, Maybe, Option)
  • All sub-types of ImmutableList (DifferenceList, IntMap, LazySeq, LazySeq, LazyString, NonEmptyList, Seq, Vector)
  • ImmutableSet, ImmutableSortedSet, ImmutableQueue
  • ReactiveSeq (e.g. FutureStream, ReactiveSeq)

Proposed Approach

This is a breaking change, and is a potential enhancement for cyclops 11 or 12.

  • Use concatMap to flatMap to an Iterable type
  • Use mergeMap to flatMap to a reactive-streams Publisher
  • For ReactiveSeq flatMap is already defined via the Stream interface (so can't be changed without not implementing Stream)
  • Remove flatMap from MonadicValue and implement type specific flatMap per affected type (Future, Eval, Option)
  • Maybe's flatMap will still be affected as the return type from the inherited flatMap on Option will still be Option
  • Remove flatMap from ImmutableList, ImmutableSet, ImmutableSortedSet and ImmutableQueue (provide type specific implementations on sub-classes)
@kwonglau
Copy link
Contributor

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants