sf8193

sf8193

Creating smaller list from enum.Map() leads to lots of nil values

Hello,

I’m trying to create a new list from a map I’m given like so

Enum.map(params, fn {k,v} -> if String.contains?(k, "checklist") and v != "", do: v end)

but the result is list = [nil,nil,nil,nil, 5,3,2] when I just want it to be [5,3,2]. Is there a way to get this result?

Marked As Solved

NobbZ

NobbZ

Enum.filter/2 after mapping.

Something like this:

params
|> Enum.map(fn {k, v} -> String.contains?(k, "checklist") and v != "", do: v end)
|> Enum.filter(&(&1 != nil))

Or, to reduce the conditionals, turn it around like this:

params
|> Enum.filter(fn {k, v} -> String.contains?(k, "checklist") and v != "" end)
|> Enum.map(fn {_, v} -> v end)

Also Liked

mudasobwa

mudasobwa

Creator of Cure

Use Kernel.SpecialForms.for/1 comprehension.

for {k, v} <- params, String.contains?(k, "checklist"), v != "", do: v
OvermindDL1

OvermindDL1

Yeah I’d use either for or flat_map. map and filter are both just special-cases of the generic flat_map, which can do both all at once and it is single-pass:

params
|> Enum.flat_map(fn {k, v} -> if(String.contains?(k, "checklist") and v != "", do: [v], else: []) end)

And @mudasobwa’s for is great. :slight_smile:

mudasobwa

mudasobwa

Creator of Cure

Enum.reject(&is_nil/1)?

NobbZ

NobbZ

Its creating an anonymous function using the capture operator.

It is equivalent to fn x -> x != nil end but less verbose if you don’t need pattern matching.

sf8193

sf8193

@mudasobwa @NobbZ never thought such a simple question could teach me so much about elixir. Thanks a ton

Where Next?

Popular in Questions Top

logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
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
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
shahryarjb
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
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement