RosettaCodeData/Task/Enumerations/Elixir/enumerations-2.ex

11 lines
628 B
Elixir

fruits = [{:apple, 1}, {:banana, 2}, {:cherry, 3}] # Keyword list
fruits = [apple: 1, banana: 2, cherry: 3] # Above-mentioned different notation
fruits[:apple] #=> 1
Keyword.has_key?(fruits, :banana) #=> true
fruits = %{:apple=>1, :banana=>2, :cherry=>3} # Map
fruits = %{apple: 1, banana: 2, cherry: 3} # Above-mentioned different notation
fruits[:apple] #=> 1
fruits.apple #=> 1 (Only When the key is Atom)
Map.has_key?(fruits, :banana) #=> true