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.

Last Post!

oliveiragahenrique

oliveiragahenrique

Great tip! Gonna check it out later for sure!

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement