Skip to content

Instantly share code, notes, and snippets.

View zacck-zz's full-sized avatar
Lemur Heavy

Zacck Osiemo zacck-zz

Lemur Heavy
View GitHub Profile
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
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
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
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
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
~
defmodule Speaker do
def shout(term) do
String.upcase(term)
end
end
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
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
defmodule Application.Profile do
alias Application.Person
@doc ~S"""
This prints the persons profile
"""
def log(%Person{} = person) do
IO.puts person
end
end
defmodule MathTest do
use ExUnit.Case, async: true
doctest Math
end