List of monad libaries for Elixir

Background

I use a library called rop in my code, which is basically an implementation of the Error/Result Monad.

On my never ending search for improvement, I have decided to adopt mix format in one of my personal projects.

Problem

The problem here is that mix format and the rop library don’t play nice together. The library defines as a macro the operator >>>, which is also the same operator used for bitwise operations in Elixir.

So the code does not get properly formatted. Ever.

I have tried to contact the maintainer of the project via the project page by creating issues, but to no avail. The project has also not been updated for several years now which leads to me to think its sunny days are long behind it.

Instead of cloning the project and doing a duplicate of someone else’s work (albeit with some changes and fixes) I have decided it would be preferable to look for another library.

What am I looking for?

I am looking for Monad libraries in Elixir that:

Thus I have found the following options:

Which is not a lot …

Question

Cloning the library in question to fix it is not totally out of question, however unless I add some serious heat to the project I see it more as polluting the hex archive with a duplicate of something that already exists.

  • what would you do in my situation?
  • what other monad / fp libraries do you know?

Maybe Witchcraft. You might also find videos from the author…

https://www.youtube.com/results?search_query=brooklyn+zelenka

But You might have the same formatting concerns.

Family Function Operator
Setoid equivalent? ==
nonequivalent? !=
Ord greater_than? >
lesser_than? <
Semigroup append <>
Functor lift ~>
convey ~>>
chain >>>
over <~
ap <<~
reverse_chain <<<
Semigroupoid compose <
pipe_compose <~>
Arrow product ^^^
fanout &&&
2 Likes

Unfortunately Witchcraft also uses the bitwise operator for certain operations. That is why I didn’t include it:

It breaks the first requirement :frowning:

The library is so small, that I’d rather copy that 1 macro to my codebase and replace >>> with ~>>

1 Like

Maybe change an idea a little and write your own macro like:

defmacro on_ok(value, call) do
  quote do
    case unquote(value) do
      {:ok, val} -> {:ok, unquote(Macro.pipe(quote(do: val), call, 0))}
      {:error, _} = error -> error
    end
  end
end

Now it is useable like:

foo()
|> on_ok(bar())
|> on_ok(baz())

Which will be logically equivalent to:

with {:ok, a} <- foo(),
     {:ok, b} <- bar(a),
     {:ok, c} <- baz(b),
     do: {:ok, c}
3 Likes

@fuelen @hauleth I understand I could do a change like that. As I mentioned it wouldn’t be out of the scope, however (ignoring the risk of repeating myself):

(…) however unless I add some serious heat to the project I see it more as polluting the hex archive with a duplicate of something that already exists.