oliveiragahenrique

oliveiragahenrique

Pure functional module successful response format

I’m designing a pure functional data structure to manage banking accounts, a simplified version of it would be:

defmodule Account do

  defstruct balance: 0, limit: -500

  def new() do
    %Account{}
  end

  def deposit(%Account{} = account, amount) do
    new_account =
      %Account{account | balance: account.balance + amount}

    {:ok, new_account}
  end

  def withdraw(%Account{} = account, amount) do
    new_balance = account.balance - amount

    if new_balance >= account.limit do
      new_account =
        %Account{account | balance: new_balance}

      {:ok, new_account}
    else
      {:denied, "No funds", account}
    end
  end
end

My question is about when should I use the response pattern of {:ok, data} for sucess and {:error, reason, unchanged_data} for failures.

This sounds like the way to go here on this problem, but if I go with this pattern I lose the feature of do something like this:

account = 
  Account.new()
  |> Account.deposit(%{amount: 5000})
  |> Account.deposit(%{amount: 3000})
  |> Account.withdraw(%{amount: 1000})

I really like to design modules that could use the pipe operator like this, but how to do this when the module functions can fail?

If I just remove the :ok from the sucess response would solve the problem, but is this a good pattern?

What you guys think? How can I keep the pipe feature together with a good response check validation?

Marked As Solved

kokolegorille

kokolegorille

If the pipeline can break, I would use with

with {:ok, a} <- Account.deposit(Account.new(), %{amount: 5000}),
  {:ok, a} <- Account.deposit(a, %{amount:3000}),
  {:ok, a} <- Account.deposit(a, %{amount: 1000})
do
  # do something with a
else
  {:error, _} -> ...
end

Also Liked

bottlenecked

bottlenecked

@rrrene has written a series of articles on data flow patterns that perhaps could help. You may want to read up on the Token-based approach for your specific use case.

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement