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

Maybe

Maybe is a functor that maps every type to the same type with an additional Nothing value. Maybe is like an optional value (note that types are the objects in our category):

data Maybe a = Just a | Nothing

The value of Maybe Int can be either just a number, such as Just 2, or Nothing.

The Maybe type maps types to types. For example, it maps Char to Maybe Char. fmap, defined in the following snippet, shows how every a -> b function has a corresponding version, Maybe a -> Maybe b, which just returns Nothing when given Nothing and behaves normally otherwise:

instance Functor Maybe wherefmap f Nothing = Nothingfmap f (Just x) = Just (f x)
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