KimberlyBrooke

KimberlyBrooke

Comprehensions - what does into %{} do?

Hey! I came across this in the book Elixir in Action and am wondering what the into %{} does here?

iex(6)> multiplication_table =
          for x <- 1..9, y <- 1..9,
              x <= y,    ①  
              into: %{} do
            {{x, y}, x*y}
          end

iex(7)> multiplication_table[{6, 7}]
42

iex(8)> multiplication_table[{7, 6}]
nil

Juric, Sasa. Elixir in Action (pp. 196-197). Manning. Kindle Edition.

Marked As Solved

KimberlyBrooke

KimberlyBrooke

Oh I tried it out in the terminal and it makes sense now it puts all the results into a map.

%{
{4, 5} => 20,
{5, 9} => 45,
{1, 2} => 2,
{3, 6} => 18,
{2, 4} => 8,
{4, 8} => 32,
{1, 1} => 1,
{3, 7} => 21,
{8, 9} => 72,
{7, 9} => 63,
{2, 7} => 14,
{4, 6} => 24,
{5, 8} => 40,
{2, 8} => 16,
{1, 4} => 4,
{5, 6} => 30,
{6, 6} => 36,
{6, 8} => 48,
{1, 7} => 7,
{8, 8} => 64,
{2, 3} => 6,
{1, 8} => 8,
{3, 4} => 12,
{7, 8} => 56,
{4, 7} => 28,
{3, 3} => 9,
{4, 9} => 36,
{7, 7} => 49,
{1, 6} => 6,
{5, 5} => 25,
{1, 9} => 9,
{3, 5} => 15,
{2, 6} => 12,
{1, 5} => 5,
{2, 5} => 10,
{2, 2} => 4,
{5, 7} => 35,
{6, 9} => 54,
{4, 4} => 16,
{6, 7} => 42,
{3, 8} => 24,
{1, 3} => 3,
{9, 9} => 81,
{3, 9} => 27,
{2, 9} => 18
}

Also Liked

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
_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
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement