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

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New

We're in Beta

About us Mission Statement