Introduction to Maps in Elixir

Elixir v0.13 has introduced Maps, a key value store. Quoting elixir 0.13 docs

A Dict implementation that works on maps. Maps are key-value stores where keys are compared using the match operator (===). Maps can be created with the %{} special form defined in the Kernel.SpecialForms module.

Now just FYI, to use Maps, elixir branch v0.13 needs to be checked out and also we need to use erlang 17 rc 1. I am not going into details, but here are some useful information.

1.) Get and install erlang 17 rc 1. One can also use multiple release manager for erlang

2.) Get and install elixir v0.13 like

git fetch

git checkout v0.13

make clean #needed to clean old bin stuff otherwise there might be some errors

make

#although I dont prefer doing installation, i just copy binaries and use them to use multiple elixir at given time, but if you wish

make install
Defining a map

Let’s dive in, roll your sleeves and fire up iex

Erlang/OTP 17 [RELEASE CANDIDATE 2] [erts-6.0] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (0.13.0-dev) - press Ctrl+C to exit (type h() ENTER for help) 

my_map = %{ name: "pankaj", age: 22, knows: ["a", "b", "c"] }
#=> %{age: 22, knows: ["a", "b", "c"], name: "pankaj"}

There is one other way to initialize or construct Map via new method, as explained below

Map.new [{ :name, "pankaj"}, { :age, 22 }, { :knows, ["a", "b", "c"] }]
#=>  %{age: 22, knows: ["a", "b", "c"], name: "pankaj"}

#as well as we can define map ruby hash style with =>
my_map = %{ :name => "pankaj", :age => 22, :knows => ["a", "b", "c"] }
Accessing Map

Accessing map is pretty simple, like we see in other language (i.e ruby, javascript)

#ruby hash style
my_map[:age]
#=> 22

#other way json style
my_map.name
#=> "pankaj"

#yet another way
Map.get(my_map, :knows)
#=> ["a", "b", "c"]

Like wise updating, deleting and other operations go by Dict style

Map.keys(my_map)
#=> [:age, :knows, :name]
Map.values(my_map)
#=> [22, ["a", "b", "c"], "pankaj"]

#updating a value
Map.put(my_map, :age, 21)
#=> %{age: 21, knows: ["a", "b", "c"], name: "pankaj"}

#deleting a value
Map.delete(my_map, :knows)
#=> %{age: 22, name: "pankaj"}

Maps implements most of Dict methods, one can look them in docs

Uses

Maps can be used as data model in modules using defstruct. Here is how

#define a module
defmodule Teenage do
  defstruct name: "", age: 15, knows: ["a", "b", "c"]
end

We can access it by

#accessing struct by
%Teenage{}
#=> %Teenage{age: 15, knows: ["a", "b", "c"], name: ""}

#or by

Teenage.__struct__
#=> %Teenage{age: 15, knows: ["a", "b", "c"], name: ""}

Manipulating struct are same as Maps

#accessing
rambo = %Teenage{}
rambo.name
#=>""
rambo.age
#=> 15

#updating
rambo = Map.put(rambo, :name, "Rambo")
#=> %Teenage{age: 15, knows: ["a", "b", "c"], name: "Rambo"}
#Notice we are reassigning rambo and it needs to be done

#getting type of struct

rambo.__struct__
#=> Teenage

Disclaimer: Maps are hot changes, available only in elixir v0.13, they might change in future.

elixir programming :)

comments powered by Disqus