Incrementing value in Map : variable-key ?

1,791 views
Skip to first unread message

mraptor

unread,
Sep 29, 2014, 11:33:18 AM9/29/14
to elixir-l...@googlegroups.com
Why this works :

%{m | :k1 => m[:k1]+1}

but this do not :
x = :k1
%{m | x => m[:k1]+1}

 >>> illegal use of variable x in map key

how do I update map if i dont know the key in advance ?

José Valim

unread,
Sep 29, 2014, 11:57:56 AM9/29/14
to elixir-l...@googlegroups.com
You can use the API in the Map module, for example, Map.update/4.



José Valim
Skype: jv.ptec
Founder and Lead Developer

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

mraptor

unread,
Sep 29, 2014, 2:15:50 PM9/29/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Hmm this starts to get little confusing for multilevel structures :

def inc(map,key) do
   Map.update map, key, 0, fn v -> v + 1 end
#  %{m | k => m[k] + 1 }
 end
 def inc(map,key,sub_key) do
   Map.update map, key, 0, fn k -> inc(k,sub_key)  end
 end

Ben Wilson

unread,
Sep 29, 2014, 2:41:25 PM9/29/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br

mraptor

unread,
Sep 29, 2014, 3:10:14 PM9/29/14
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
That's nicer, thank you both ....

def inc(map,keys = [ k,sk | _ ], n \\ 1) do
   put_in map, keys, map[k][sk]+n
 end

mraptor

unread,
Sep 29, 2014, 4:28:50 PM9/29/14
to elixir-l...@googlegroups.com
Contd... Once I have the update/inc operation, how would you apply a list of increments ... i.e. something like this :
(the sub key is the same on all keys)

apply_inc_updates(map, [k1,k2,k1,k3,k1,k6,k9,k1])

I did something like this :

 #helper list increase
 def hlst_inc([],acc), do: acc
 def hlst_inc([h|t],acc) do
   x = inc acc, [h,:sub_key]
   hlst_inc(t,x)
 end

does it seem like correct way of doing this ?

Ben Wilson

unread,
Sep 29, 2014, 4:40:15 PM9/29/14
to elixir-l...@googlegroups.com
I recommend taking some time to browse through the Elixir standard library, there are a lot of functions out there that will make your life a lot easier. The Enum module is likely where you'd want to start. Assuming that you have some function already to increment a key in a map called inc/2 just do ```Enum.reduce(keys, map, &inc/2)```

As an aside, there's no need to abbreviate your function names so severely, it impedes readability. map_increment or something would be a lot clearer than hlst_inc. When you need a comment just to make the function name readable you should probably reconsider the function name.

Ben Wilson

unread,
Sep 29, 2014, 4:46:24 PM9/29/14
to elixir-l...@googlegroups.com
Forgot to send you a link! http://elixir-lang.org/docs/master/elixir/

mraptor

unread,
Sep 29, 2014, 5:22:07 PM9/29/14
to elixir-l...@googlegroups.com
Thanks... I forgot about reduce, will try it... Probably because I associate it with Lists :), wasnt thinking of reducing-maps ...
I was using short names because I was experimenting around...


On Monday, September 29, 2014 4:40:15 PM UTC-4, Ben Wilson wrote:
Reply all
Reply to author
Forward
0 new messages