install Elixir
sudo dnf install elixir erlang erlang-docnew elixir project
mix new myprojgenerate binary
mix releaseadd dependencies
vi mix.exs
def deps docompile a binary
>> MIX_ENV=prod mix releaseStrings
concactenate string
iex> "hello " <> "there"interpolation
iex> string = "world"split string into List
String.split("welcome to the terror dome")["welcome", "to", "the", "terror", "dome"]split string into tuple with by position
String.split_at("welcome to the terror dome", 4){"welc", "ome to the terror dome"}
String via sigil
Variables
bind a variable to a number
x = 1Anonymous var - for unneeded vars
calendar.local_time returns tuple {date, time}, dont need date
{_, time} = :calendar.local_timeComparisons
value comparison
strict comparison (check if variable type is same)
1 === 1.0falseLists (Array)
Lists are immutable, any addition or modification of List results in a new mem allocation and new list (mem safe)
get length
length([ 1, 2, 3])concat 2 lists
[1,2,3] ++ [4,5,6]get head or tail of a list
list = [1,2,3]get information about a List
iex(1)> list = ["apple", 2, 3.5]
["apple", 2, 3.5]
iex(2)> i list
Term
["apple", 2, 3.5]
Data type
List
Reference modules
List
Implemented protocols
Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars
get element from list
numbers = [2,3,4,5]check if variable is in list
4 in numbersreplace value
List.replace_at(numbers, 0, 9)insert value
List.insert_at(numbers, 0, 7)Tuples
Tuples like lists are immutable, creates new tuples in memory on any modification. Good for small fixed number of elements
Tuples are good for storing data and reading (read time is faster than list, writing/modifiying is slower)
modify Tuple (stores in a new variable)
tuple = { "Mary", "Bob" }get tuple value
IO.puts elem(tuple,1)Maps (hashes)
map = %{:name => "Bob", :age => 23, :dept => "IT"}update map (creates new map)
new_map = %{map | age: 26, dept: "Billing"}
Case
case {1,2,3} do
{4,5,6} -> "This wont match"
{1,x,3} -> "will bind x to 2"
_ -> "will match any value"
end
Conditionals
Functions
public and private funcs
defmodule TestPrivate do def double(a) ## publicimport function
import IOinstead of
IO.puts "hello"alias a function
alias IO, as: myIOdocument module help, create a constant variable at runtime
defmodule Circle doget doc for a function from iex
iex> h Circleadd Type to function, only accepts number as input and output should only be a number
@doc "computes area of circle"default argument value
def func(a, b \\ 1, c, d \\ 2)lambda functions
print_var = fn x -> IO.puts(x) end
Enum.each( [1,2,3], print_var)
Compiler
compile a binary
elixirc test.exxxxx
Atoms (Symbols)
named constants (value of an atom is its own name)
:my_atomAtoms are efficiently compared for equality because they are internally represented by integers
It's important to note that atoms are not garbage collected and there's a limit to how many can be created (default is 1,048,576). Therefore, you should avoid creating atoms dynamically based on user input or arbitrary data.
Atoms in Elixir serve as lightweight, immutable values that are excellent for representing fixed concepts or states within your application. They provide a way to name things without the overhead of strings, making them ideal for many internal operations in Elixir programs.