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.

msimonborg

msimonborg

Your map syntax itself is invalid

iex(1)> {"a", 1} # valid tuple syntax
{"a", 1}

iex(2)> %{"a" => 1} # valid map syntax
%{"a" => 1}

iex(3)> %{"a", 1} # invalid
** (SyntaxError) iex:3:6: syntax error before: ','
    |
 30 | %{"a", 1}
    |      ^

Kurisu

Kurisu

Ah you’re right ! ^^
No string interpolation is necessary indeed.

I tought it won’t work for map with atom keys, but it also works as well:

iex(1)> key = :a
:a
iex(2)> %{key => 1}               
%{a: 1}
iex(3)>

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement