November 2017
Intermediate to advanced
670 pages
17h 35m
English
Let's see what we can do with a slice of line items.
First, we define an interface that has three methods, Append, Zero, and Reduce. We wrap our line items in the lineitemContainer. Our lineitemContainer is a struct with three fields that correspond to our invoice's line items:
type Lineitem struct { Quantity int Price int ListPrice int}
Our Append method appends the given line item to the slice of line items it's building up that lives in the magical lineitemContainer.
The Zero morphism for a slice is nil.
The src/monoid/lineitem_monoid.go file will have the following code:
package monoidtype LineitemMonoid interface { Zero() []int Append(i ...int) LineitemMonoid Reduce() int}func WrapLineitem(lineitems []Lineitem) ...