Skip to content

Added result of MobSession (Krakow on 12.0.2020) #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
cart-*.tar

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Cart

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `cart` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:cart, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/cart](https://hexdocs.pm/cart).

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
defmodule Cart do
@moduledoc """
Documentation for Cart.
"""

def show_cart(events) when is_list(events) do
Enum.reverse(events)
|> Enum.reduce(
[],
fn {event, {product, qty, price}}, cart ->
adjustment = calculate_adjustment(event, qty)
[{_product, {qty, _price}} | rest] = move_to_head(cart, product)

[{product, {qty + adjustment, price}} | rest]
|> remove_item_with_zero_qty()
end
)
end

def calculate_adjustment(:item_added, qty) do
qty
end

def calculate_adjustment(:item_removed, qty) do
-1 * qty
end

def move_to_head(cart, product) do
item_to_move = Enum.find(cart, {product, {0, 0.00}}, fn {item, _} -> item == product end)
rest_of_items = Enum.filter(cart, fn {item, _} -> item != product end)
[item_to_move | rest_of_items]
end

def remove_item_with_zero_qty(cart) do
Enum.filter(cart, fn {_product, {qty, _price}} -> qty > 0 end)
end

def get_total(events) do
show_cart(events) |> Enum.reduce(0, fn {_product, {qty, price}}, sum -> sum + qty * price end)
end

def get_count(events) do
show_cart(events) |> Enum.reduce(0, fn {_product, {qty, _price}}, sum -> sum + qty end)
end

def get_added_items(events) do
events
|> Enum.filter(fn {event_type, _} -> event_type == :item_added end)
|> Enum.reduce([], fn {_event, {product, _, _}}, products -> [product | products] end)
end

def get_removed_items(events) do
events
|> Enum.filter(fn {event_type, _} -> event_type == :item_removed end)
|> Enum.reduce([], fn {_event, {product, _, _}}, products -> [product | products] end)
end
end
28 changes: 28 additions & 0 deletions workshops/an_ounce_of_elixir_2020/cart_krakow_12_feb_2020/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Cart.MixProject do
use Mix.Project

def project do
[
app: :cart,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
defmodule CartTest do
use ExUnit.Case

test "show_cart(events) returns an empty list for an empty cart" do
events = []
assert Cart.show_cart(events) == []
end

test "show_card(events) returns an empty list for a cart that has item added and then removed" do
random_qty = Enum.random(1..100)

events = [
{:item_removed, {:dinosaur_egg, random_qty, 125.00}},
{:item_added, {:dinosaur_egg, random_qty, 125.00}}
]

assert Cart.show_cart(events) == []
end

test "show_cart(events) returns a list of current items in the right form" do
items = [
{:iguana_leash, {4, 12.00}},
{:zombie_repelent, {12, 3.00}}
]

events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_removed, {:iguana_leash, 1, 12.00}},
{:item_added, {:iguana_leash, 2, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}}
]

assert Cart.show_cart(events) == items
end

test "get_total(events) returns a zero for an empty cart" do
events = []
assert Cart.get_total(events) == 0.00
end

test "get_total(events) returns a floating point number for the total cost of all products in the cart" do
events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}}
]

assert Cart.get_total(events) == 72.00
end

test "get_count(events) returns a zero for an empty cart" do
events = []
assert Cart.get_count(events) == 0
end

test "get_count(events) returns a number of items in a cart" do
events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}}
]

assert Cart.get_count(events) == 15
end

test "get_added_items(events) returns an empty list for an empty cart" do
events = []
assert Cart.get_added_items(events) == []
end

test "get_added_items(events) returns an empty list for a cart with only removed items" do
events = [
{:item_removed, {:iguana_leash, 3, 12.00}},
{:item_removed, {:zombie_repelent, 12, 3.00}}
]

assert Cart.get_added_items(events) == []
end

test "get_added_items(events) returns a list of added items for a cart" do
events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_removed, {:iguana_leash, 3, 12.00}},
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}},
{:item_removed, {:zombie_repelent, 5, 3.00}},
{:item_added, {:zombie_repelent, 12, 3.00}},
{:item_added, {:iguana_leash, 3, 12.00}}
]

items = [:iguana_leash, :zombie_repelent, :zombie_repelent, :iguana_leash, :iguana_leash]

assert Cart.get_added_items(events) == items
end

test "get_removed_items(events) returns an empty list for an empty cart" do
events = []
assert Cart.get_removed_items(events) == []
end

test "get_removed_items(events) returns an empty list for a cart with only added items" do
events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}}
]

assert Cart.get_removed_items(events) == []
end

test "get_removed_items(events) returns a list of items removed form a cart" do
events = [
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_removed, {:iguana_leash, 3, 12.00}},
{:item_added, {:iguana_leash, 3, 12.00}},
{:item_added, {:zombie_repelent, 12, 3.00}},
{:item_removed, {:zombie_repelent, 5, 3.00}},
{:item_added, {:zombie_repelent, 12, 3.00}},
{:item_added, {:iguana_leash, 3, 12.00}}
]

items = [:zombie_repelent, :iguana_leash]

assert Cart.get_removed_items(events) == items
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()