Skip to Content
Learning Functional Programming in Go
book

Learning Functional Programming in Go

by Lex Sheehan
November 2017
Intermediate to advanced
670 pages
17h 35m
English
Packt Publishing
Content preview from Learning Functional Programming in Go

Lineitem slice monoid

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) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781787281394Supplemental Content