Last active
November 10, 2018 22:21
-
-
Save fay-jai/5092b451851687d1d979f92b31734d98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Basic Data Types in Elixir | |
# Atoms | |
name = :willson # an atom's name is its value | |
full_name = :willson_mock # underscores and other symbols are also valid in atom names | |
# Tuple | |
result = { :ok, "Hello World" } | |
{ response, message } = result # Tuples are often used in pattern matching | |
# Lists | |
empty_list = [] # A list can either be empty... | |
non_empty_list = [1, 2] # Or it can consist of a head value (i.e. 1) and a tail list (i.e. [2]) | |
long_way_to_build_list = [ 1 | [ 2 ] ] # this is the same as [1, 2] | |
# Map | |
person = %{ name: "Willson", weight: 150 } | |
friend = %{ "name" => "Kanye", occupation: "Rapper" } | |
IO.puts(person.name) # This will work and print out "Willson" | |
IO.puts(person[:weight]) # This will work and print out "150" | |
IO.puts(person["weight"]) # This will work but will return nil because "weight" and :weight are not the same | |
IO.puts(friend["name"]) # This will work and print out "Kanye" | |
IO.puts(friend[:name]) # This will work but will return nil because :name and "name" are not the same | |
IO.puts(friend.name) # This won't work and will throw an error because dot notation only works when the keys are atoms | |
# Keyword Lists | |
options = [ {:auto_indent, true}, {:max_characters: 255}, {:default_value: "Hello World"} ] | |
IO.puts(options) # When Elixir prints out a keyword list, it will use a shorthand notation like so: [auto_indent: true, max_characters: 255, default_value: "Hello World"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment