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
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}
2
eksperimental
pillaiindu
Yes, it’s a tuple not a map, and not even a tuple, because tuple doesn’t have %{. ![]()
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.
2
Last Post!
LostKobrakai
Popular in Questions
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
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
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
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
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









