rob-brown / monadex Goto Github PK
View Code? Open in Web Editor NEWUpgrade your pipelines with monads.
License: MIT License
Upgrade your pipelines with monads.
License: MIT License
I am trying this library and in specific I am trying the Result Monad. The documentation mentions a function called Result.bind and I have made it work with the following code snippet:
defmodule TestMonadex do
use Monad.Operators
import Monad.Result
def p1(x) do
x
|> success()
|> bind(&plus_1/1)
end
defp plus_1(n), do: n + 1
end
And so far so good.
The problem arises when I try to use the bind operator. According to the docs, this operator is the same thing as the Result.bind
function. Following this logic, the following code snippet should work:
defmodule TestMonadex do
use Monad.Operators
import Monad.Result
def p2(x) do
success(x) ->> (&plus_1/1) # This does not compile!
end
defp plus_1(n), do: n + 1
end
Unfortunately, this is not compiling:
(SyntaxError) syntax error before: '>'
Stacktrace:
โ (elixir 1.11.2) lib/kernel/parallel_compiler.ex:314: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7
an expression is always required on the right side of ->. Please provide a value after ->
At this point I strongly believe I am not understanding something from the docs or from the library.
What a I doing wrong?
Dear Rob,
I was wandering how to make a pure functional Monad to use with CQRS. The idea I found here: https://github.com/slashdotdash/eventsourced
Every input command should generate an Event, and this event should be applied on the State. Maybe you could add in MonadEx or illuminate my way to implement it alone.
Hi Daniel,
many thanks for the fantastic Monadex lib, I finally feel that I understood the
monads abstractions, and how important they are to reduce our codebase.
But I still have some questions about the practical use of MonadEx.
Let me ask you, for the "Result" monad, the solution below, to deal with errors during the pipe execution, lacks something to drive me to use MonadEx, or it's enough ? What would be the advantage to use MonadEx over the solution below ?
thanks
Henry
try do
opts
|> Keyword.fetch!(:source_file)
|> File.read!
rescue
e in KeyError -> IO.puts "missing :source_file option"
e in File.Error -> IO.puts "unable to read source file"
end
I was wondering if it would still be idiomatic to pipe the monad's value to the right hand side function similar to the pipe operator upon a successful result monad instead of calling the fn closure and passing the value?
def my_func(value), do: IO.puts value
success(value) ~>> my_func
instead of
success(value) ~>> fn value -> my_func(value) end
My experience in other languages that support monads, is that you need a transformer to compose monads (eg MaybeT would allow you to cut through a Maybe inside another Monad by wrapping the whole thing in the transformer.)
I tried some time ago to implement transformers for MonadEx, and I hit a wall I believe attempting to represent a higher-kinded type (MaybeT would take some monad, without the contents known, so like MaybeT[Reader[_], A], rather than MaybeT[Reader[X], A].) Sorry if this sounds off, it was a pretty long time ago and I unfortunately didn't save my progress.
Anyway all of that said, my question comes back to the README for MonadEx. There's a heading that talks briefly about composing monads, but no example. Can you clarify how you would go about doing this in Elixir using this lib?
In my quest to learn more about this library 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
The problem here is that according to the documentation, Result Monad does not implement the fmap
:
https://hexdocs.pm/monadex/Monad.Result.html#content
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.