The shopping cart

The input will be a list of items. For each item will find the next info:

Name

Units

Price per unit

Expiration date

type Item struct {

Name string

Units int

PricePerUnit float32

ExpirationDate time.Time

}

and the software requisites are:

Print the list of items ordered by name . For each item we will print the following information: name, units and the total price

. For each item we will print the following information: name, units and the total price Items on expired date must be removed from cart

If there’s 3 or more units of the same product in the cart, a 20% discount must be saved for this product.

must be saved for this product. We need to print the total price.

After having a look at the requirements we can easily understand that we need to do is:

Filter : We only need to leave in the cart those items whose expiration date has not expired yet.

: We only need to leave in the cart those items whose expiration date has not expired yet. Sort: We need to print the items ordered by name

We need to print the items ordered by name Map: For any item we need to add a new attribute, the total price of the items

For any item we need to add a new attribute, the total price of the items ForEach: For each item in the cart we need to print the required info

For each item in the cart we need to print the required info Reduce: We need to calculate the total price for our shopping cart.

It looks like Functional programming!!!

Since Koazee does a Lazy evaluation of the operations we can define a variable shoppingStream and then we will be able to reuse it for any shopping cart.

var shoppingStream = koazee.Stream().

Filter(func(item *cart.Item) bool {

return item.ExpirationDate.After(time.Now())

}).

Sort(func(itemLeft, itemRight *cart.Item) int {

return strings.Compare(itemLeft.Name, itemRight.Name)

}).

Map(func(item *cart.Item) *ItemTotal {

total := float32(item.Units) * item.PricePerUnit

if item.Units >= 3 {

total *= discount

}

return &ItemTotal{item, total}

}).

ForEach(func(item *ItemTotal) {

fmt.Printf(" - %s, %d units, %.2f€

", item.Name, item.Units, item.total)

})

We define a new struct to add the total price for item

type ItemTotal struct {

*cart.Item

total float32

}

Finally, we only need to define a function that will make use of variable shoppingStream and will calculate the price for the cart.