question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Idea] Fluent Interface for Monad instances.

See original GitHub issue

This is untested at the moment, its just an idea. One advantage FJ has over us at the moment is a fluent interface for their monads (admittably by violating the DRY principle).

Maybe we could design a wrapper to also give us the fluent interface pattern.

One way:

interface FluentInterfaceM<M,A> {
  __<M,A> unbox(Monad<M> mMonad);

  static <A> FluentInterfaceM<M,A> pure(A a) {
    return (Monad<M> mMonad) -> mMonad.pure(a);
  }

  default <B> FluentInterfaceM<M,B> map(F<A,B> f) {
    return (Monad<M> mMonad) ->
      mMonad.map(f, FluentInterface.this.unbox(mMonad))
  }

  default <B> FluentInterfaceM<M,B> bind(F<A,FluentInterfaceM<M,B>> f) {
    return (Monad<M> mMonad) ->
      mMonad.bind(
        FluentInterface.this.unbox(mMonad)
        (A x) -> f.f(x).unbox(mMonad)
      );
  }
}

Example use:

__<__<__<StateT.Mu,Integer>,Maybe.Mu>,Unit> test =
  FluentInterfaceM.pure(5)
    .map((Integer x) -> x + 1)
    .map((Integer x) -> x * 2)
    .map((Integer x) -> x - 3)
    .unbox(StateT.monad(Maybe.monad));

Maybe FIM instead of FluentInterfaceM to keep it short.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
DanielGronaucommented, Jul 26, 2016

How about this:

   interface MonadW<M, A, MM extends Monad<M>> {
        static <M, A, MM extends Monad<M>> MonadW<M, A, MM> pure(A a) {
            return mMonad -> mMonad.pure(a);
        }

        default <B> MonadW<M, B, MM> bind(Function<A, MonadW<M, B, MM>> f) {
            return mMonad -> mMonad.bind(
                    MonadW.this.unbox(mMonad),
                    (A x) -> f.apply(x).unbox(mMonad)
                    );
        }

        default <B> MonadW<M, B, MM> map(Function<A, B> f) {
            return mMonad -> mMonad.map(f, MonadW.this.unbox(mMonad));
        }

        __<M, A> unbox(MM mMonad);
    }

    interface MonadPlusW<M, A, MP extends MonadPlus<M>> extends MonadW<M, A, MP> {
        @Override
        __<M, A> unbox(MP mMonadPlus);

        default MonadPlusW<M,A, MP> mzero() {
            return mPlus -> mPlus.mzero();
        }
    }
0reactions
clinuxrulzcommented, Sep 7, 2016

Version 3 of do notation now has a fluent style. Making monad wrappers less important. But I’ll leave this open for discussion of other kinds of wrappers for other type-classes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do monads have fluent interfaces? - Stack Overflow
The fluent method convention is to return the same type of value so as to continue chaining calls that are all valid on...
Read more >
FluentInterface - Martin Fowler
The API is primarily designed to be readable and to flow. The price of this fluency is more effort, both in thinking and...
Read more >
Fluent Interfaces Are Bad for Maintainability - Yegor Bugayenko
Fluent interface is a very popular and convenient design pattern, which, however, makes objects larger and less maintainable.
Read more >
The Lazy Monad | Hacker News
Monads are a pattern for function chaining. It can be compared with fluent interfaces in OOP, which is also a pattern for chaining...
Read more >
Fluent Interfaces : r/programming - Reddit
I generally despise fluent interfaces. They tend to lead to API ambiguity or inconsistency, and they can often be rewritten to be clearer...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found