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?

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
5 Likes

@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.

2 Likes

Great tip! Gonna check it out later for sure!