This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Names do | |
def names() do | |
our_names = ["Zacck", "Carla", "Kristie", "David"] | |
our_new_names = our_names ++ "Jahir" | |
IO.puts inspect our_new_names | |
IO.puts inspect our_names | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Archiver do | |
# function that handles list of items to archive | |
def archive([_h | _t] = items) do | |
Enum.map(items, fn(item) -> Task.async(&archive_thing(&1)) end) # run a short lived task for each item | |
|> Enum.map(&Task.await/1) # collect a result for each item and return a list of results | |
end | |
# function to handle each individual itemd | |
defp archive_thing(_t) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Archiver do | |
# function that handles list of items to archive | |
def archive([_h | _t] = items) do | |
results = | |
for item <- items do | |
archive_thing(item) | |
end | |
results | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Budget do | |
def sub_totals(expenses) do | |
allCategories = | |
Enum.uniq for %{type: t, cost: _} <- expenses, do: t | |
category_costs = Enum.map(allCategories, fn(x) -> category_total(expenses, x) end) | |
end | |
def category_total(expenses, category) do | |
catExpenses = | |
Enum.sum for %{type: ^category, cost: c} <- expenses, do: c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 defmodule Cooker do | |
3 def cook([head | tail], total) do | |
2 cook(tail, total <> ", " <> head) | |
1 end | |
5 | |
1 | |
2 def cook([], total), do: total <> " burger" | |
3 end | |
~ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Speaker do | |
def shout(term) do | |
String.upcase(term) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prices = [12,45,78,90] | |
defmodule Total do | |
def totaller([h|t],tot) do | |
totaller(t, tot+h) | |
end | |
def totaller([],tot), do: tot | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Application.Person do | |
defstruct first_name: "", second_name: "" age: nil, height: "" | |
@doc ~S""" | |
returns a persons full name | |
""" | |
def fullname(person) do | |
"#{person.firstname} #{person.second_name}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Application.Profile do | |
alias Application.Person | |
@doc ~S""" | |
This prints the persons profile | |
""" | |
def log(%Person{} = person) do | |
IO.puts person | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MathTest do | |
use ExUnit.Case, async: true | |
doctest Math | |
end |