Skip to content

Instantly share code, notes, and snippets.

@mrcampbell
Last active April 4, 2020 05:49
Show Gist options
  • Save mrcampbell/2dc21ecc0c4505a28465a43be64835d1 to your computer and use it in GitHub Desktop.
Save mrcampbell/2dc21ecc0c4505a28465a43be64835d1 to your computer and use it in GitHub Desktop.
# NESTED FOR LOOP
def create_deck do
values = ["Ace", "Two", "Three", "Four", "Five"]
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
# list comprehension. Basically a .map() function
for suit <- suits, value <- values do
"#{value} of #{suit}"
end
end
comment """
output:
["Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades",
"Five of Spades", "Ace of Clubs", "Two of Clubs", "Three of Clubs",
"Four of Clubs", "Five of Clubs", "Ace of Hearts", "Two of Hearts",
"Three of Hearts", "Four of Hearts", "Five of Hearts", "Ace of Diamonds",
"Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds"]
"""
# CASE STATEMENT (no if), READ/WRITE FILE
def save(deck, filename) do
binary = :erlang.term_to_binary(deck)
File.write(filename, binary)
end
def load(filename) do
{status, binary} = File.read(filename)
case status do
:ok -> :erlang.binary_to_term binary
:error -> "That file does not exist"
end
end
# compressed version of load/1
def load(filename) do
case File.read(filename) do
{:ok, binary} -> :erlang.binary_to_term binary
{:error, :enoent} -> "That file does not exist"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment