pillaiindu

pillaiindu

Using a map after do: in a list comprehension

These two work:

iex(1)> for {key, val} <- %{"a" => 1, "b" => 2}, do: {key, val * val} 
[{"a", 1}, {"b", 4}]
iex(2)> for {key, val} <- %{"a" => 1, "b" => 2}, into: %{}, do: {key, val * val} 
%{"a" => 1, "b" => 4}

But this one gives syntax error:

iex(3)> for {key, val} <- %{"a" => 1, "b" => 2}, do: %{key, val * val}            
** (SyntaxError) iex:3:62: syntax error before: '}'
    |
  3 | for {key, val} <- %{"a" => 1, "b" => 2}, do: %{key, val * val} 
    |                                                              ^

Why?
Why can’t we use map directly after the do: in list-comprehensions?

Most Liked

Kurisu

Kurisu

I think you have a synthax error on the way you’re trying to define the map. It is not about the for or do.

One way you could achieve the map definition would be :

%{“#{key}”: val * val}

or

%{“#{key}” => val * val}

eksperimental

eksperimental

Exactly, the first one works because what he is defining is a tuple, not a map.

pillaiindu

pillaiindu

Yes, it’s a tuple not a map, and not even a tuple, because tuple doesn’t have %{. :slight_smile:

I did it accidentally and it took time to catch the mistake. I wanted to see if someone else can catch the mistake, because the error message doesn’t specifically point it out.

%{"#{ isn’t even needed, using key directly works.

for {key, val} <- %{"a" => 1, "b" => 2}, do: %{key => val * val} works.

Last Post!

LostKobrakai

LostKobrakai

Maps are valid returns for for, unless into: %{} is supplied as well. So it’s tricky.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
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
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
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

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 130286 1222
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
dogweather
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
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement