mattfara50

mattfara50

How would you accumulate errors?

In “Elixir in Action”, there is an example about using pattern matching with the “with” special form:

with
       {:ok, login}  <- extract_login(user),
       {:ok, email} <- extract_mail(user),
       {:ok, pw}     <- extract_pw(user)
do
  {:ok,   %{login: login, email: email, pw: pw}}
end

If each validation of user is successful, the “ok” tuple is returned. Otherwise, the first failing validation’s “error” tuple is returned.

But suppose I wanted to run all the validations, returning the same “ok” if they all pass, but returning a list of all the “error” tuples for all failing validations.

Is there an idiom for this in Elixir? afaik this is like an applicative functor.

Marked As Solved

eksperimental

eksperimental

you can use

for {key, validate_fun} <- validation, reduce: [] do
  acc ->
   ....
end

Also Liked

kokolegorille

kokolegorille

You could reduce each function call… then check if Enum.all?, Enum.any?, whatever tests You may want to run on the list.

# NOT TESTED!

result = ~w(extract_login extract_mail extract_pw)a
|> Enum.reduce(&apply(__MODULE__, &1, [user]))

But your code looks like a user validation, and usually this can be done with a changeset.

eksperimental

eksperimental

Something like this, (I haven’t run the code though)

validations = [
  login: &extract_login/1,
  mail: &extract_mail/1,
  pw: &extract_pw/1
]

Enum.reduce(validatations, [], fn {key, validate_fun}, acc ->
  case validate_fun.(user) do
    {:ok, value} ->
      [{:ok, key, value} | acc]

    :error ->
      [{:error, key} | acc]
  end
end)
|> Enum.reverse()
mattfara50

mattfara50

This looks like what I was doing in Java, where functional programming idioms are at last available. I was hoping there were a macro or special form like with that might accomplish what that reduce is doing there. I’m still learning the bare basics. Thank you!

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement