Skip to content

Instantly share code, notes, and snippets.

@fschuindt
Created April 27, 2016 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fschuindt/889cd055c1dd0e638e32ad5f9068c53c to your computer and use it in GitHub Desktop.
Save fschuindt/889cd055c1dd0e638e32ad5f9068c53c to your computer and use it in GitHub Desktop.
:: Elixir study notes.
# get some help:
iex> i 'hello'
Term
'hello'
Data type
List
Description
...
Raw representation
[104, 101, 108, 108, 111]
Reference modules
List
# important convention:
When “counting” the number of elements in a data structure, Elixir also abides by a simple rule: the function is named size if the operation is in constant time (i.e. the value is pre-calculated) or length if the operation is linear (i.e. calculating the length gets slower as the input grows).
# string concatenation is done with: <>
# operators 'or', 'and', 'not' can only accept boolean values!
# Besides these boolean operators, Elixir also provides ||, && and ! which accept arguments of any type. For these operators, all values except false and nil will evaluate to true
# pattern matching is actually cool
iex> {a, b, c} = {:hello, "world", 42}
{:hello, "world", 42}
iex> a
:hello
# head and tail example:
iex> [head | tail] = [1, 2, 3]
[1, 2, 3]
iex> head
1
# The pin operator ^ should be used when you want to pattern match against an existing variable’s value rather than rebinding the variable
# The variable _ is special in that it can never be read from. Trying to read from it gives an unbound variable error
# guard clauses are cool!
# anonymous functions can have guard clauses:
# they also apply to the 'case' statement, "when"
iex> f = fn
...> x, y when x > 0 -> x + y
...> x, y -> x * y
...> end
#Function<12.71889879/2 in :erl_eval.expr/5>
iex> f.(1, 3)
4
iex> f.(-1, 3)
-3
==================================================
booleans -> all good, just true and false...
atoms -> just symbols
strings -> simples strings,
anonymous functions -> normal functions, delimited between 'fn' and 'end'.
iex> add = fn a, b -> a + b end
iex> add.(3, 2)
# they are "first class cirizens", closures
lists -> simples lists of values
iex> [2, 23, 42, 11, true]
# add or subtract using ++ or --
# get head and tail from them
iex> list = [1, 2, 3]
iex> hd(list)
1
iex>tl(list)
[2, 3]
# When Elixir sees a list of printable ASCII numbers, Elixir will print that as a char list (literally a list of characters).
# Single-quotes are char lists, double-quotes are strings.
tuples -> same as list, but stored in memory, all data is availible with no recursion need.
iex> {:ok, "hello"}
{:ok, "hello"}
iex> tuple_size {:ok, "hello"}
2
case -> simple as it is
iex> case {1, 2, 3} do
...> {4, 5, 6} ->
...> "This clause won't match"
...> {1, x, 3} ->
...> "This clause will match and bind x to 2 in this clause"
...> _ ->
...> "This clause would match any value"
...> end
#If you want to pattern match against an existing variable, you need to use the ^ operator:
iex> x = 1
1
iex> case 10 do
...> ^x -> "Won't match"
...> _ -> "Will match"
...> end
# another cool example, now with clauses conditions:
iex> case {1, 2, 3} do
...> {1, x, 3} when x > 0 ->
...> "Will match"
...> _ ->
...> "Would match, if guard condition were not satisfied"
...> end
#If none of the clauses match, an error is raised.
cond -> case is useful when you need to match against different values. However, in many circumstances, we want to check different conditions and find the first one that evaluates to true. In such cases, one may use cond.
iex> cond do
...> 2 + 2 == 5 ->
...> "This will not be true"
...> 2 * 2 == 3 ->
...> "Nor this"
...> 1 + 1 == 2 ->
...> "But this will"
...> end
# This is equivalent to else if clauses in many imperative languages.
# If none of the conditions return true, an error is raised. For this reason, it may be necessary to add a final condition, equal to true, which will always match.
if and unless ->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment