a guest Nov 8th, 2018 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- ## Quick Intro
- * Object Orientation is not the only way
- * Functional Programming need not be complex or mathematical
- * The bases of programming are not assignments, if statements and loops
- * Concurrency does not need locks, semaphores, monitors and the like
- * Processes are not necessarily expensive resources
- * Metaprogramming is not just something tacked onto a language
- * Even if it is work, programming should be fun
- ## Pattern Matching
- In Elixir equal sign is not an assignment. Its like an assertion. It succeeds if Elixir can find a way of making the left-hand
- side equal the right-hand side. Elixir calls "=" a match operator.
- For `a = 1` "a" is a variable and "1" is an integer literal, so Elixir can make the match try by binding the variable `a` to
- value `1`.
- To prove that this is not just an assignment:
- `iex> a = 1`
- `iex> 1 = a`
- `iex> 2 = a`
- `** (MatchError) no match of right hand side value: 1`
- Since we first matched value `1` to variable `a`, when we perform `1 = a` Elixir matches it, cause `1 matches 1`.
- But when we try to match `2 = a` Elixir does not allow it, cause `2 does not match 1`, then raises an error.
- More complex Matches:
- `iex(1)> list = [1,2,3]
- [1, 2, 3]
- iex(2)> [a,b,c] = list
- [1, 2, 3]
- iex(3)> a
- iex(4)> b
- iex(5)> c
- Elixir looks for a way to make the value of the left side same as on the right side. If there is a list of 3 variables on
- the left side and a list of 3 values on the right side, then they match and values can be setup to the variables.
- This is called *pattern matching* - A pattern (left side) is matched if the values (right side) have the same structure and
- if each term in the pattern can be matched to the corresponding term in the values.
RAW Paste Data