daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Nov 8th, 2018 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Quick Intro
  2.  
  3. * Object Orientation is not the only way
  4. * Functional Programming need not be complex or mathematical
  5. * The bases of programming are not assignments, if statements and loops
  6. * Concurrency does not need locks, semaphores, monitors and the like
  7. * Processes are not necessarily expensive resources
  8. * Metaprogramming is not just something tacked onto a language
  9. * Even if it is work, programming should be fun
  10.  
  11. ## Pattern Matching
  12.  
  13. 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
  14. side equal the right-hand side. Elixir calls "=" a match operator.
  15. 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
  16. value `1`.
  17. To prove that this is not just an assignment:
  18.  
  19. `iex> a = 1`
  20. `1`
  21. `iex> 1 = a`
  22. `1`
  23. `iex> 2 = a`
  24. `** (MatchError) no match of right hand side value: 1`
  25.  
  26. Since we first matched value `1` to variable `a`, when we perform `1 = a` Elixir matches it, cause `1 matches 1`.
  27. But when we try to match `2 = a` Elixir does not allow it, cause `2 does not match 1`, then raises an error.
  28.  
  29. More complex Matches:
  30.  
  31. `iex(1)> list = [1,2,3]
  32. [1, 2, 3]
  33. iex(2)> [a,b,c] = list
  34. [1, 2, 3]
  35. iex(3)> a
  36. 1
  37. iex(4)> b
  38. 2
  39. iex(5)> c
  40. 3`
  41.  
  42. 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
  43. the left side and a list of 3 values on the right side, then they match and values can be setup to the variables.
  44. This is called *pattern matching* - A pattern (left side) is matched if the values (right side) have the same structure and
  45. if each term in the pattern can be matched to the corresponding term in the values.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top