TIL
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.
Basics
- Everything is truthy except for
falseandnil - Booleans are atoms (:true, :false)
- Modules are atoms
- You can access erlang libraries using atoms
- Aka atoms are important :)
- Strings use double quotes
- No modulo operator, but there are the
divandremfunctions. - Each basic type has a rank :/
- number < atom < reference < function < port < pid < tuple < map < list < bistring
- I'll never memorize this lawl
- String interpolation "hello #{world}" or "hello " <> "world" ... Pretty Basic.
Collections
- Lists
- Mixed types
- Not unique
- Linked List β faster to prepend than append > 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 that together represent a sequence.
- List concat
++/2- cool - List sub
--/2- cool cool - Head/Tail - cool cool cool
- By pattern matching
[head | tail] = list#=> head is the first element. the tail will be all the others -
hd list#=> returns the first element -
tl list#=> returns all elements after the first
- Tuples
- Stored with a set amount of memory
- Access = fast; modification = slow
- Common for return info from function
- Keyword lists
- commonly used to pass options to functions
- Maps
- The Go-to key-value store
- Similar to ruby's hash
- Updating syntax is cool?
- useMap.put/3 to create a new key
Enum
- Not much to go over here if you are familiar with any other languages Enum methods
Pattern Matching
- The Holy Grail of Elixir
- If you ask any dev what they love about elixir chances are they will mention pattern matching
- Curveball
=is actually a match operator similar to the equals sign in algebra :exploding-head: - [1| tail] = list #=> tail has not been matched so it binds to the last elements of the list
Control Structures
-
ifandunless=> straight forward -
case/2- Similar to
switch(in javascript) orcase(ruby) except it uses pattern matching -
_is a catch all likedefaultin JavaScript
- Similar to
-
Cond- Used for matching multiple cases similar to an
else if
- Used for matching multiple cases similar to an
-
With- This one is bit trickier coming from other languages
- Can be used to replace multiple
casestatements
Top comments (0)