|
| 1 | +--- |
| 2 | +title: Round 1 Day 1 |
| 3 | +published: true |
| 4 | +description: Back to school. Back to school. Learning Elixir. |
| 5 | +tags: 100daysofcode, elixir |
| 6 | +cover_image: https://cdn.hashnode.com/res/hashnode/image/upload/v1613181428296/AE6zOKS_p.jpeg?auto=compress |
| 7 | +date: 2021-02-12 |
| 8 | +--- |
| 9 | + |
| 10 | +## TIL |
| 11 | +Currently during the 100DaysOfCode Challenge, I'm learning elixir. I'll be documenting everything I learn or build as I go. I'm starting off with learning the elixir basics at [ElixirSchool](http://elixirschool.com). |
| 12 | + |
| 13 | +## Basics |
| 14 | +- Everything is truthy except for `false` and `nil` |
| 15 | +- Booleans are atoms (:true, :false) |
| 16 | +- Modules are atoms |
| 17 | +- You can access erlang libraries using atoms |
| 18 | +- Aka atoms are important :) |
| 19 | +- Strings use double quotes |
| 20 | +- No modulo operator, but there are the `div` and `rem` functions. |
| 21 | +- Each basic type has a rank :/ |
| 22 | + - number < atom < reference < function < port < pid < tuple < map < list < bistring |
| 23 | + - I'll never memorize this lawl |
| 24 | +- String interpolation "hello #{world}" or "hello " <> "world" ... Pretty Basic. |
| 25 | + |
| 26 | +## Collections |
| 27 | +- Lists |
| 28 | + - Mixed types |
| 29 | + - Not unique |
| 30 | + - Linked List → faster to prepend than append |
| 31 | + > In computer science, a linked list is a linear collection of data elements whose order is not given by their physical placement in memory. Instead, each element points to the next. It is a data structure consisting of a collection of nodes which together represent a sequence. |
| 32 | + - List concat `++/2` - cool |
| 33 | + - List sub `--/2` - cool cool |
| 34 | + - Head/Tail - cool cool cool |
| 35 | + - By pattern matching `[head | tail] = list` #=> head is the first element. tail will be all the others |
| 36 | + - `hd list` #=> returns the first element |
| 37 | + - `tl list` #=> returns all elements after the first |
| 38 | +- Tuples |
| 39 | + - Stored with a set amount of memory |
| 40 | + - Access = fast; modification = slow |
| 41 | + - Common for return info from function |
| 42 | +- Keyword lists |
| 43 | + - commonly used to pass options to functions |
| 44 | +- Maps |
| 45 | + - The Go-to key-value store |
| 46 | + - Similar to ruby's hash |
| 47 | + - Updating syntax is cool? |
| 48 | + - useMap.put/3 to create a new key |
| 49 | + |
| 50 | +## Enum |
| 51 | +- Not much to go over here if you are familiar with any other languages Enum methods |
| 52 | + |
| 53 | +## Pattern Matching |
| 54 | +- The Holy Grail of Elixir |
| 55 | +- If you ask any dev what they love about elixir chances are they will mention pattern matching |
| 56 | +- Curveball `=` is actually a match operator similar to the equals sign in algebra :exploading-head: |
| 57 | +- [1| tail] = list #=> tail has not been matched so it binds to the last elements of the list |
| 58 | + |
| 59 | +## Control Structures |
| 60 | + |
| 61 | +- `if` and `unless` => straight forward |
| 62 | +- `case/2` |
| 63 | + - Similar to `switch`(in javascript) or `case` (ruby) except it uses pattern matching |
| 64 | + - `_` is a catch all like `default` in JavaScript |
| 65 | +- `Cond` |
| 66 | + - Used for matching multiple cases similar to an `else if` |
| 67 | +- `With` |
| 68 | + - This one is bit trickier coming from other languages |
| 69 | + - Can be used to replace multiple `case` statements |
0 commit comments