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
Also Liked
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









