Kevin McCarthy
June 21, 2015
110

# Functors

Part 1 of the Functors, Monoids, and Monads presentation here: http://www.meetup.com/dynamic/events/222501291/

June 21, 2015

## Transcript

1. A monad is a monoid in the category of endofunctors,
what's the problem?

2. The magic of functional composition

3

sub1(x) = x - 1
2

5. crunch(5)
6

6. crunch(5)
6

7. crunch([1,2,3])
WTF?????

8. FUNCTORS

9. fcrunch([1,2,3])
fcrunch = fmap crunch
[2,3,4]

10. class Functor f where
fmap :: (a -> b) -> f a -> f b

11. fmap transforms a “normal” function (g :: a -> b) into
one which operates over containers/contexts (fmap
g :: f a -> f b). This transformation is often referred to
as a lift; fmap “lifts” a function from the “normal world”
into the “f world”.

add2 :: Num a => a -> a
*> :t crunch
crunch :: Num a => a -> a
*> :t fcrunch
fcrunch :: (Functor f, Num b) => f b -> f b

13. instance Functor [] where
fmap _ [] = []
fmap g (x:xs) = g x : fmap g xs
-- or we could just say fmap = map

14. instance Functor Maybe where
fmap _ Nothing = Nothing
fmap g (Just a) = Just (g a)

15. •[]
•Tree
•Map
•Sequence
•Maybe
•IO

16. MATH TIME

17. –Wikipedia
“In mathematics, a functor is a type of mapping
between categories, which is applied in category
theory. Functors can be thought of as
homomorphisms between categories”

18. Categories are bags of stuff that hold:
• Objects
• Morphisms
Math

19. Categories are bags of stuff that hold:
• Types
• Functions
Code

sub1
Number
f sub1
f Number
Functor

sub1
10
fmap sub1
[10]
[]
LIFT

22. FUNCTOR RULES
Math

23. FUNCTOR RULES
•fmap id x = x
•fmap(g.h) = (fmap g).(fmap h)
Code

24. BROKEN FUNCTOR
instance Functor [] where
fmap _ [] = []
fmap g (x:xs) = g x : g x : fmap g xs