One of the new hot topic items in Erlang R17 is maps. In Ruby you would call them a hashes, PHP calls them associative arrays, no matter what you call them it’s the same idea: key-value pairs. Back in the “Good old days” :tm: an Erlang programmer would need to leverage a record to get the same functionality. While records are very good and carry benifit they lack some of the flexibility found in maps.
Defining a map is pretty strait forward. The #{
starts it off and then is
followed by any number of key => value
pairs that are seperated by commas
before being terminated by the lonely }
.
|
Lets say we want to build a fun that will up the age by one, to do so we could write it like this:
|
It is important to know the differance between the :=
and =>
operators.
:=
will only work for pattern matching or updating a key from a map that has
that key already defined. As in this example the brightness value cannot be
updated from sun as it was not one of the original values.
|
=>
can be used to initialize, add, or update map values.
|