Giter VIP home page Giter VIP logo

monadex's People

Contributors

rob-brown avatar vic avatar zabirauf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

monadex's Issues

bind operator

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

CQRS Monads

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.

MonadEx over "Try & Rescue"

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

Example of composing monads?

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?

Bind operator question

Background

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.

Problem

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.

Question

What a I doing wrong?

How to chain Result Monads using Monadex?

Background

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

Problem

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

https://hexdocs.pm/monadex/Monad.Result.html#content

Question

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.