Kapeusz
How to refactor Enum.reduce?
Hello, I have a problem with properly understanding Enum.reduce but after many tries I managed to get this inside my function to work. However, when I run mix credo --strict I see there’s a refactoring opportunity because the function body is nested too deep. Could you please help me with refactoring this function? I would appreciate an additional explanation. Thank you!
Enum.reduce(conditions, queryable, fn {k, v}, acc ->
case {k, v} do
{_, ""} ->
acc
{_, _} when is_atom(k) ->
case {k in like_fields, k in date_fields} do
{true, false} -> where_ilike(acc, k, v)
{false, true} -> where_date(acc, k, v)
{false, false} -> where(acc, [u], field(u, ^k) == ^v)
end
_ ->
acc
end
end)
Marked As Solved
josevalim
Creator of Elixir
Note that the branching and pattern matching can also be applied to fn and are equivalent:
Enum.reduce(conditions, queryable, fn
{_, ""}, acc ->
acc
{k, v}, acc when is_atom(k) ->
case {k in like_fields, k in date_fields} do
{true, false} -> where_ilike(acc, k, v)
{false, true} -> where_date(acc, k, v)
{false, false} -> where(acc, [u], field(u, ^k) == ^v)
end
_, acc ->
acc
end)
You can also merge the first and third branches:
Enum.reduce(conditions, queryable, fn
{k, v}, acc when is_atom(k) and v != "" ->
case {k in like_fields, k in date_fields} do
{true, false} -> where_ilike(acc, k, v)
{false, true} -> where_date(acc, k, v)
{false, false} -> where(acc, [u], field(u, ^k) == ^v)
end
_, acc ->
acc
end)
6
Popular in Questions
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
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
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
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
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Other popular topics
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Hello everyone,
Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New








