Special characters methods for dry-monads


In Haskell, bind and fmap have special characters method names. bind is >>= or >> (depending on the type of bind, but in ruby it would be the same type in both implementation and usage), while fmap can be <$>.

This becomes visually very useful, because you quickly get used to that symbols which are more eye-catching that just another word. You learn to quickly read through them.

In ruby we are more limited about the symbols we can use, however we still have some possibilities:

  • bind could become just the same of one of the Haskell versions: >>. This is easy to memorize because it is similar to the fish operator >=>, which is monadic function composition, from which bind is just a specialized version. These right arrows clearly tell something about composing the left to the right.
  • fmap in Haskell is <$> because $ can be used for function application, which is very similar to what fmap does: applies a function within the container. In Haskell it makes more sense because you write the function to the left and the value to the right: function <$> value. In dry-monads we are using the other way: value.fmap { ... }, but still we could alias it to call so we can use just the dot ., but we would be very limited in its usage because something like value. { ... } is not allowed. So, surely it would be better to find another one.

What do you think?


This is what the kleisli gem did and I don’t endorse it. Ruby lacks good support for operators so all attempts to imitate Haskell in Ruby will be quite poor IMO. On the other hand, syntax extensions can be added as a separate gem with either monkey patches or refinements (or both), that’d be interesting to see.


Thanks for your answer @flash-gordon

You are right, it isn’t possible to imitate Haskell at that level, however, taking a conservative approach I think it would not be too bad, and it is more a thing about getting used to a new style (like using . instead of .call, first it seems weird but at the end it feels really good).

For example, in ruby we could do something like the following without problems:

a .>> { |a| a.do_something } .>> { |b| b.do_something }

I think it improves readability, but it is just my view.

Maybe I’ll explore the idea of doing a separate gem, although it would be too skinny :smile:

Thanks anyway! :smile: