TODO: To learn about the basis of functional programming and the basic Elixir code structure
-
iex -S mix
inside the directory where mix.exs is there to compile and run iex -
Update
defp deps
block to update dependencies just like Gemfile -
Run
mix deps.get
to install the dependecies
-
Pattern Matching is Elixir's way for assigning a variable.
-
If the data structure and the no of left/right side's elements are same, then the pattern matches and assigns the variable.
Ex.)
# returns ["red"]
color1 = ["red"]
[color1] = ["red"]
return color1 #returns red
[color1, color2] = ["red", "blue"]
return color1 #returns "red"
return color2 #returns "blue"
Ex.2)
["red", color] = ["red", "blue"]
-> In this case, left hard coded value must match in the right side as well.
:some_word
is the primitive data type of Elixir. Something like symbols in Ruby.
Chain method using |>
With the pipe operator, there is no need to save a variable.
method after |>
will use the returned result from the method before.
two version of a documentation.
on top of the codes inside the module.
@moduledoc """
Provides methods for creating and handling deck of cards
"""
run mix docs: Generates docs/index.html
on top of each functions
@doc """
Check if a card is inside the created deck
## Examples
iex> deck = Cards.create_deck
iex> Cards.contains?(deck, "Ace of Spades")
true
"""
run mix docs: Generates docs/index.html
doctest Module
will include the function documentations' examples.
Example test codes:
test "create_deck makes 20 cards" do
deck_length = length(Cards.create_deck)
assert deck_length == 20
end
test "Shuffling a deck randomizes" do
deck = Cards.create_deck
refute Cards.shuffle(deck) == Cards.create_deck
end
Maps are collections of key and values. Just like hashes
!!
iex> colors = %{primary: "red", secondary: "blue"}
# returns %{primary: "red"}
iex> colors.primary
# Use with pattern matching
iex> %{secondary: secondary_color} = colors
# returns blue
iex> secondary_color
colors = %{primary: "red"}
# returns an error
colors.primary = "blue"
In order to update the map, we can do it in 2 ways.
- With the function
- With the special syntax
Elixir provides rudimentary functions for manipulating the map.
put
function:
colors # returns %{primary: "red"}
colors_two = Map.put(colors, :primary, "blue")
colors_two.primary # returns blue
colors # Still returns %{primary: "red"}
# Using syntax to update original map
colors_three = %{ colors | primary: "green" }
colors_three.primary # returns green
# adding new key:value to the map
Map.put(colors, secondary_color: "green")
colors # returns # returns %{primary: "red", secondary_color: "green"}
colors = [{:primary, "red"}, {:secondary, "green"}]
colors[:primary] # returns "red"
colors = [primary: "red", secondary: "green"] # returns samething
%{primary: "red", primary: "blue"} # returns blue and not duplicate keys
Keyword list stores the duplicate keys unlike Map.
Keyword List is useful when using something like ecto
. Because it can store multiple duplicate keys
When using Keyword List, when the last argument of a function is a Keyword List, []
can be removed.
ex.
# This passes the keyword list as an argument to find_where function
# However, since it's the last argument of the function [] was removed,
# and since duplicate key of 'where' is used, it is a keyword list
query = User.find_where where: user.age > 10, where: user.subscribed == true
Write a module that generates identicon(auto-generated pixel image), which will return images based on the string inputs.
Identicon requires 5 x 5 grid, 250px x 250px with each square being 50px. Colors will be automatically filled and will always be identical or symmetric about the center.
- Elixir
Check identicon directory in the repository.
- String input
- Compute MD5 hash of string
- List of numbers based on the string
- Pick color
- Build grid squares
- Convert grid into image
- Save image as a file