Editing
Elixir Maps
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Elixir Maps == [[File:Elixir-logo.png|thumb|right|200px|The Elixir programming language logo]] In Elixir, a map is a data structure that allows you to store key-value pairs. It is similar to dictionaries or hashmaps in other programming languages. Maps are an essential part of Elixir and are used extensively in various scenarios. === Creating Maps === Maps can be created in Elixir using the `%{}` syntax: ```elixir map = %{:name => "John", :age => 30} ``` Alternatively, you can use the shorthand syntax for atom keys: ```elixir map = %{name: "John", age: 30} ``` === Accessing Values === Once you have created a map, you can access its values using the `.` dot notation or the `Map.get/2` function: ```elixir name = map.name age = Map.get(map, :age) ``` === Updating Maps === Maps in Elixir are immutable, meaning that they cannot be modified once created. However, you can create a new map with updated values using the `Map.put/3` function: ```elixir new_map = Map.put(map, :age, 31) ``` === Map Pattern Matching === Elixir provides a powerful pattern matching feature that allows you to destructure maps and extract specific values: ```elixir %{name: name, age: age} = map ``` === Map Functions === Elixir provides various functions to work with maps, including `Map.keys/1`, `Map.values/1`, and `Map.merge/2`. These functions can be used to manipulate and transform maps in different ways. === Map Enumerables === Maps in Elixir also support the Enumerable protocol, which means you can use functions like `Enum.map/2`, `Enum.filter/2`, and `Enum.reduce/3` on them. === Examples === Here are some examples that demonstrate the usage of maps in Elixir: ```elixir user = %{name: "Alice", age: 25, email: "alice@example.com"} IO.inspect(user) %{name: name} = user IO.puts(name) new_user = Map.put(user, :age, 26) IO.inspect(new_user) user_list = [user, new_user] IO.inspect(Enum.map(user_list, &(&1.name))) ``` === Conclusion === Maps are a fundamental data structure in Elixir, allowing you to store and manipulate key-value pairs. They are versatile and provide various functions to work with them efficiently. Understanding how to create, access, and update maps is crucial for effective Elixir programming. == See Also == * [[Elixir_Data_Types]] * [[Elixir_Lists]] * [[Elixir_Tuples]] * [[Elixir_Pattern_Matching]] * [[Elixir_Enumerables]] [[Category:Elixir Programming Language]] [[Category:Data Structures]]
Summary:
Please note that all contributions to Elixir Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Elixir Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information