2020-12-01

Elixir notes

Modules

Defining a module:

defmodule ModName do
    def some_function() do
    end
end

ModName.some_function()

Functions

Making a private function use defp

defmodule Mod do
    defp private_func() do
        # I am a private function
    end

    def func() do
        # I am a function
    end
end

Recursive functions

TODO: this is not complete. This needs to be confirmed and tested

def sum(list) do
    sum(list, 0)
end

def sum([], total) do
    total
end

def sum([h | tail], total) do
    new_total = h + total
    sum(tail, new_total)
end