Get a value from a map Using Elixir Program - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Thursday, September 28, 2017

Get a value from a map Using Elixir Program

This Program shows different ways to get values from a map.

PROGRAM:

# dot syntax can be used if key is atom
1 = %{c: 1}.c

# Raises a key error for missing key
%{c: 1}.a

# [] works for non-atom values
1 = %{"a" => 1}["a"]

# [] returns nil for missing values
nil = %{"a" => 1}["b"] 

# Pattern matching can be used
%{c: value} = %{c: 1}

1   = Map.get(%{c: 1}, :c)
nil = Map.get(%{c: 1}, :a)

# Default value can be specified
# for when the key is misssing
"default" = Map.get(%{c: 1}, :a, "default")

{:ok, value} = Map.fetch(%{c: 1}, :c)
:error       = Map.fetch(%{c: 1}, :a)

1 = Map.fetch!(%{c: 1}, :c)

# Raises a key error
Map.fetch!(%{c: 1}, :a)

No comments:

Post a Comment