chan11347
i want to add a condition in the add_entry to limit the input such like if else in java, any help
server.ex
def add_entry(todo_server, new_entry) do
GenServer.cast(todo_server, {:add_entry, new_entry})
end
@impl GenServer
def handle_cast({:add_entry, new_entry}, {name, todo_list}) do
new_list = Todo.List.add_entry(todo_list, new_entry)
Todo.Database.store(name, new_list)
{:noreply, {name, new_list}}
end
list.ex
def add_entry(todo_list, entry) do
entry = Map.put(entry, :id, todo_list.auto_id)
new_entries = Map.put(todo_list.entries, todo_list.auto_id, entry)
%Todo.List{todo_list | entries: new_entries, auto_id: todo_list.auto_id + 1}
end
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
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
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
Check out https://elixir-lang.org/getting-started/case-cond-and-if.html
mindok
Depending on what you are trying to achieve, having multiple function heads for the
add_entryfunction is idiomatic.for example:
Alternatively, if you really want to use if, remember that it returns a value and you can’t modify variables with scope external to it, so you have to do something like:
If you want to spell out a bit more what you are trying to do we could give you the best option…
chan11347
Todo.Server.add_entry(test, %{date: ~D[2020-05-19], time: ~T[16:52:00], title: "Shopping"})this is what the input and I want to set it if any of the value is nil or empty it return an IO statement " input cannot be empty and return to the same iex
chan11347
so follow the idea you gave is it like this?
mindok
Close…
or…
dimitarvp
But the question here is: do you want to raise an exception, or return an erroneous value to the caller? If exception is fine, you can just pattern match the happy path like this:
Then, if the function is called with an empty
entrieskey the runtime will just raiseFunctionClauseErrorbecause there is no function that handles the empty value.chan11347
what if I want to return an erroneous value?is that I need to set up an error statement by myself?
lucaong
If your map contains keys like
:date,:time, etc., then your code above won’t work (because it matches an entry with key:entries).There are a few possible ways to solve this. First, you could check if any of the entry fields is blank. Remember that maps are
Enumerable, so you can enumerate them as a collection of{key, value}with theEnummodule:Instead of printing an output though, it would be better to return an error, so the caller can pattern match easily. Usually, one would return
:okor{:error, reason}:If the entries always have the same keys, there is a possibly better way to do this: you can use a struct for the entry, instead of a map, to enforce the “shape” of the entry:
This way, the entry is now a struct that enforces that all of
:date,:time, and:valueare present. This also mean, though, that the caller of theadd_entryfunction has to pass anEntrystruct instead of a map, so it’s your choice whether this is desirable or not.P.S.:
Unrelated to your question, but you might want to use
GenServer.call/3instead ofGenServer.cast/2, even if you don’t need a result. The reason is explained here: https://elixir-lang.org/getting-started/mix-otp/genserver.html#call-cast-or-infodimitarvp
Show us what you have tried to make the function return an error value?
chan11347
so if i not enter both value there will having a crash appear
so i want to make it limited at the input part to stop this happening