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

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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
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
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
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

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
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
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
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
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
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
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 ...
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

We're in Beta

About us Mission Statement