https://elixirschool.com/en/lessons/basics/collections/
Another way to do a keyword list
[{:foo, "bar"}, {:hello, "world"}]
calling a built-in function:
iex(9)> round 200/15
13
define a function:
my_func = fn (a_num) -> a_num * 2 end
my_func.(5)
10
why have a dot after my defined function and not the round above?
anonymous function shortcut &
my_func = &(&1 * 2)
my_func.(5)
10
name the file the lowercase of module name (dot ex, i.e.: blah.ex) for module Blah.
Module Blah with function fall_velocity defined.
defmodule Blah do
def fall_velocity (distance) do
distance * 10
end
end
functions defined with def are visible outside module, use defp for private functions.
Can compile module with:
iex(1)> c("blah.ex") [Blah]
Creates file: Elixir.Blah.beam.
Call module functions:
Blah.fall_velocity(10) 100
Modules can access each other if in the same directory.
Pipe forward |>, lets you put the result of one function into the first argument of the next function
Drop.fall_velocity(meters) |> Convert.mps_to_mph
If you import a module, then you dont have to prefix its functions with the module name anymore.
To import an Erlang module, prefix the module name with a colon and don’t start the name with an uppercase letter
import :math
just import some functions do:
import :math, only: [sqrt: 1]
if import is only needed in one function you can import INSIDE your function definition:
defmodule Drop do
def fall_velocity(distance) do
import :math, only: [sqrt: 1]
sqrt(2 * 9.8 * distance)
end
end
Default argument values: param \ default_val, gravity \ 9.8
function doc:
defmodule Drop do
@doc """
Calculates the velocity of an object falling on Earth
as if it were in a vacuum (no air resistance). The distance is
the height from which the object falls, specified in meters,
and the function returns a velocity in meters per second.
"""
def fall_velocity(distance) do
import :math, only: [sqrt: 1]
sqrt(2 * 9.8 * distance)
end
end
Atoms are like clojure keywords. Often used for pattern matching.
In elixir buffer
, s I RET
Highlight region then…
, s r
, s l
, m c RET
Keep compile buffer open. Each file save causes compilation.
Run test at point:
, t t
Region
module uses a defstruct
Image
module uses Ecto to make structs, %Image{}
This can be done like so:
use Ecto.Schema
@fields [:name]
schema "dummy" do
field :name, :string
end
This creates some kind of object so running this doesn’t make too much sense to see the output
Ecto.Changeset.cast(%Image{},%{},[:name])
Poison encode:
Poison.encode!(%I{})