kostonstyle
I wrote a a function that delete the content of database.
Consider following code snipped:
defmodule Seeds do
def delete_db(values) when is_list(values) do
cond do
length(values) > 0 ->
Repo.delete_all(values)
true ->
{:ok}
end
end
end
When I call the delete_db function, it will happen a side effect, the data on db will be deleted.
My question is, do I follow functional specification?
Thanks
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
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
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 17 Posts
gon782
What exactly does that mean? It’s not technically a function, no, because it has a side-effect. The reality of Erlang & Elixir is that there can be side-effects in procedures anywhere. Pure functions in these languages are an ideal; something to strive for, not law.
It’s also not law in Haskell, for example, but is clearly marked as happening, at least. I would suggest not fretting about using side-effects in Elixir. Instead, pay attention to where you can do without them by passing data. Don’t do this to blindly follow ideals, but to get code that can be used in any context and with predictable results.
hubertlepicki
You won’t really get pure functions that do operations on databse, files, environment variables etc. etc.
the reason is that they interface with impure external environment. So I do not think there is anything wrong from that point of view in your code.
kostonstyle
So my code is ok?
kostonstyle
Can you show me please an example?
bbense
The essential quality of a “pure” function is that it always has the same output for the same inputs.
You could make your function pure by passing in the state of the database and returning the state of the database, However, that’s not really practical. But you should strive to have anything you operate on in the function passed in as an input, rather that magically appearing from the ether.
This makes the code much easier to test and also limits the side effects to as small a surface area as possible. There is obviously a trade-off between adding needless complexity and applying this principle. To misquote Terry Pratchett
benwilson512
Hey there. Your function is generally fine, but has a couple of style issues. First, it’d be clearer to use
Enum.any?instead oflenght(values) > 0. Not only is this more readable, we don’t actually care what the length is, so long as there’s at least one item. No point in counting them all.Secondly, you should just return
:okinstead of{:ok}. There’s no point in wrapping it in a tuple.With these changes in mind, your best bet is something like:
gon782
Well, with refactoring in mind it might be better to simply do this:
It’s short, splits the procedure up into cases neatly and asserts structure at the same time.
benwilson512
As a final tweak, you can have the second clause just be
def delete_db(values)because the only way it gets there is if it isn’t an empty list. You could addwhen is_list(values)if you really wanted to as wellgon782
Think of it this way:
If something needs updating, let’s say a map of some keys and values, you could conceivably have a process that guards that state and the way you interact with it is to send messages to that process in order to modify the data.
When you use this model you run the risk of having the process crash on you, possibly losing state while doing so, and also having the data modified at any point because other processes can also change the state. What you’ve effectively created is something like a reference to the state you want to work with. Sure, when you have it in your hand you can trust that it’s the same state you got from the KV process, but you can never trust that you have the “correct” data in the sense that it’s the updated one.
The BEAM is great in that your particular piece of memory that holds the data you have can’t be changed by anything else, so that’s great, but putting too many things in processes and calling stateful functions with them will still invite harder debugging.
If you had a function that modified the map and then passed that map to the function as an argument everything that that function relies on is inside that function. It doesn’t make any assumptions about the world; for example that a KV store is running somewhere.
It’s trivial to emulate stateful constructs in Elixir, but we generally never set out to do so unless it’s really needed. The point is to make the least amount of assumptions possible about what exists when you run something. The ideal is for everything that a function touches to be something it’s given up-front.
gon782
The possible issue is that you’re not necessarily asserting that it should be a list if you ended up using only
values, yeah. I prefer code to be as assertive as possible about structure and constants if possible. If something in the function itself makes the assumption that lists are passed in, we should try to make things that don’t follow that crash horribly, IMO.On a related note: I vastly prefer the structural form to is_list().