Elixir
Installation
- Install mise
- oh-my-zsh plugins:
- mise: built in
- elixir
mise install erlangmise install elixir
Commands
iex- repliex -S mix- repl with depsmix local.hex- update hex PMmix deps.get- install deps
Resources
Books
Courses
Syntax
https://github.com/itsgreggreg/elixir_quick_reference
# Comment
:my_atom
`nil`
name = “Josh”
“Strings have double quotes, #{name}”
range = 5..10
Collections
list = [1, 2] # stored as a linked list
length list
Enum.reverse(list)
Enum.join(list, “, “)
tuple = { :a, :b } # stored contiguous in memory
keyword_list = [{:a, “one”}, {:b, 2}]
shorthand = [a: “one”, b: 2]
Keyword.keys(keyword_list)
Keyword.get_keys(keyword_list)
map = %{:a => 1, 1 => [“list”]}
Map.keys(map)
Map.put(map, :b, 2)
defmodule Person do
defstruct name: “”, age: 0
end
josh = %Person{name: “Josh”, age: 43}
josh.name
%Person{ josh | name: "Joshua" }
Functions
anon_func = fn num -> num + 2 end
support_method = fn ("suspenders") -> IO.puts "Hey big spender."
("belt") -> IO.puts "Who is the real hero?"
a when is_list a -> Enum.each(a, support_method)
_ -> IO.puts "What is this wizzardry?"
end
defmodule MyModule do
def my_function(“Josh”) do
IO.puts("Hello Josh!!!”)
end
def my_function(name), do: IO.puts(“Hello #{name}.”)
defp private_func do
end
end
Comprehensions
for num <- [1, 2, 3, 4], rem(num, 2) == 0, do: num * num
Slapdash