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

Int slice monoid

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, ...
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