Fl4m3Ph03n1x

Fl4m3Ph03n1x

How to chain Result Monads using Monadex?

Background

In my quest to learn more about Monadex I am trying to chain a Result Monad several times in a function.
I understand this is achieved via the fmap, a function which takes a function and a Monad and returns a Monad.

defmodule TestMonadex do
  use Monad.Operators

  import Monad.Result

  # This wont work
  def p2(x) do
    x
    |> success()
    <|> (&plus_1/1)
    <|> (&plus_1/1) 
  end

  defp plus_1(n), do: n + 1
end

Problem

The problem here is that according to the documentation, Result Monad does not implement the fmap:

Question

  • Why can’t my example work?
  • How can I chain the Result Monad?

Marked As Solved

dorgan

dorgan

I wrote an explanation of fmap vs bind but then I realized the problem here is that for some reason the function needs to be in the left hand side of the <|> operator.
Functor.fmap takes a value and then a function: MonadEx/lib/functor.ex at master · rob-brown/MonadEx · GitHub
But <|> reverses the order: MonadEx/lib/monad/operators.ex at master · rob-brown/MonadEx · GitHub

So I think your function should do (&plus_1/1) <|> (&plus_1/1) <|> success(x) instead?


The explanation I started to write in case you want it:

The way to chain monads is by using bind, not fmap.
bind’s signature is bind :: m a -> (a -> m b) -> m b where m is the monad(the context). What this tells you is that you start with a monad m with a type a, you give it a function that works with an a and returns a monad m with a type mb, and bind will give you back that last m b.
To illustrate it a bit better, if I have a value x and I apply it a function a -> m b, like something that returns a Maybe b, and I want to apply that a -> m b again, I would end up with Maybe (Maybe b). bind lets you do composition while avoiding that nesting.

fmap on the other hand is a way to lift a function to work in a context. It’s signature is fmap :: (a -> b) -> (f a -> f b), which means that you start with a function from a to b and get back a lifted function that works in a context f(the functor). This is essentially what you do with Enum.map, you give it an list and it takes care of using the function in every element of the list and returns back a list. If you have a Maybe, fmap would lift the function to be applied to the element inside the Just but skip the Nothing.

Monads are indeed Functors in the sense that you can define fmap in terms of bind and return(the functions in that monadex module):

fmap f m = m >>= (return . f)

The other altenative is defining bind in terms of join and fmap.

The definition of fmap doesn’t involve monads though, it’s just a way to lift a function to work in a particular context.

An fmap definition for a result tuple would look like this:

def fmap({:ok, x}, f) do
  {:ok, f.(x)}
end
def fmap({:error, x}, _) do
  {:error, x}
end

While a definition of bind would looke like this:

def bind({:ok, x}, f) do
  f.(x)
end
def bind({:error, x}, _) do
  {:error, x}
end

Notice that in fmap we extract the x from the tuple so it can work with f (the a -> f a lifting), and then it wraps the result of applying f to x in an ok tuple again (the b -> f b lifting), essentially making the function f work in the context of a result tuple. But in bind the f already returns a result tuple, so we don’t need to wrap it, otherwise it would result in nested result tuples.

More “correct” definitions would be curried functions, though, so fmap would be:

def fmap(f), do: fn
  {:ok, x} -> {:ok, f.(x)}
  {:error, x} -> {:error, x}
end

So fmap here returns a version of f that can work with result tuples. I think this is a better illustration of what lifting a function means.

Also Liked

dorgan

dorgan

I think this is correct.

I think the same, but I also think that the reason would be familiarity for functional programmers/mathematicians that are used to functional composition(which is essential to category theory).
If I have functions f and g, their composition g ∘ f (reads as g after f) could be visualized with this diagram:

     f             g
a───────────►b───────────►c
│                         ▲
└─────────────────────────┘
            g ∘ f

A lot of category theory constructs are built in this way, you have an a, a b and a c, you have an f that goes from a to b and a g that goes from b to c and you want to find the composition g ∘ f that goes directly from a to c. Note that the order of the operands of the composition is goes against the direction of the arrows, and I think that the order of operands in the <|> derives from this notation. If you look at it from this perspective, then it becomes the most intuitive/natural ordering, but I agree it’s hard to grasp at first.

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement