November 2017
Intermediate to advanced
670 pages
17h 35m
English
Let's see what we can do with a slice of ints.
First, we define an interface that has two methods, Append and Zero. We wrap our int in intContainer. intContainer is a struct with a single int field, ints. Our Append method appends the given int slice to the slice of ints it's building up that lives in the magical intContainer. The Zero morphism for a slice is nil.
Here is the content of src/monoid/int_monoid.go:
package monoidtype IntMonoid interface { Zero() []int Append(i ...int) IntMonoid Reduce() int}func WrapInt(ints []int) IntMonoid {return intContainer{ints: ints}}type intContainer struct { ints []int}func (intContainer) Zero() []int {return nil}func (i intContainer) Append(ints ...int) IntMonoid { i.ints = append(i.ints, ...