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

The shape of a functor

A functor is an algebraic type that accepts a value (or usually, a list of values) and has a map function that applies to each element in the list to produce a new functor of the same shape. What is a shape?

Let's look at an imperative example:

ints := []int{1,2,3}impInts := []int{}for _, v := range ints {   impInts = append(impInts, v + 2)}fmt.Println("imperative loop:", impInts)

Here's the output:

imperative loop: [3 4 5]

The shape in this example means a slice with three ints. We started with a slice with three ints, ran our imperative code, and ended up with a slice with three ints.

A functor gets the same results (three elements in and three elements out) but a functor does it in a different way.

We give our functor ...

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