Fl4m3Ph03n1x
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?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Sebb
isn’t
fmapfor functors while monads usebind?(not sure, never used monads, Elixir gives me all I need (so far) with pattern matching,
{:atom, value}and lists.)Fl4m3Ph03n1x
It is my current understanding that a Monad is a Functor and therefore should also implement this protocol.
(Not a pro on Monads also, still learning).
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.fmaptakes a value and then a function: MonadEx/lib/functor.ex at master · rob-brown/MonadEx · GitHubBut
<|>reverses the order: MonadEx/lib/monad/operators.ex at master · rob-brown/MonadEx · GitHubSo 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, notfmap.bind’s signature isbind :: m a -> (a -> m b) -> m bwheremis the monad(the context). What this tells you is that you start with a monadmwith a typea, you give it a function that works with anaand returns a monadmwith a typemb, andbindwill give you back that lastm b.To illustrate it a bit better, if I have a value
xand I apply it a functiona -> m b, like something that returns aMaybe b, and I want to apply thata -> m bagain, I would end up withMaybe (Maybe b).bindlets you do composition while avoiding that nesting.fmapon the other hand is a way to lift a function to work in a context. It’s signature isfmap :: (a -> b) -> (f a -> f b), which means that you start with a function fromatoband get back a lifted function that works in a contextf(the functor). This is essentially what you do withEnum.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 aMaybe,fmapwould lift the function to be applied to the element inside theJustbut skip theNothing.Monads are indeed Functors in the sense that you can define
fmapin terms ofbindandreturn(the functions in that monadex module):The other altenative is defining
bindin terms ofjoinandfmap.The definition of
fmapdoesn’t involve monads though, it’s just a way to lift a function to work in a particular context.An
fmapdefinition for a result tuple would look like this:While a definition of
bindwould looke like this:Notice that in
fmapwe extract thexfrom the tuple so it can work withf(thea -> f alifting), and then it wraps the result of applyingftoxin an ok tuple again (theb -> f blifting), essentially making the functionfwork in the context of a result tuple. But inbindthefalready 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
fmapwould be:So
fmaphere returns a version offthat can work with result tuples. I think this is a better illustration of what lifting a function means.Fl4m3Ph03n1x
Indeed if I reverse the order of things they work.
To me this feels quite counter intuitive.
This means I cannot pipe functions in their natural order of progression.
Am I missing something here?
Thanks @dorgan for the explanation, the examples with the tuples really did help!
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
fandg, their compositiong ∘ f(reads asgafterf) could be visualized with this diagram:A lot of category theory constructs are built in this way, you have an
a, aband ac, you have anfthat goes fromatoband agthat goes frombtocand you want to find the compositiong ∘ fthat goes directly fromatoc. 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.