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

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
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

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
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 31586 112
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
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

We're in Beta

About us Mission Statement