Lesson 37 of 55
10 mins readPython The Functor Typeclass & fmap (<$>)
In Plain English
A Functor is a computational context that can be mapped over. fmap (or infix <$>) applies a function (a -> b) to a wrapped value f a, producing f b while preserving the outer structure.
Deep Dive: How It Works
class Functor f where fmap :: (a -> b) -> f a -> f b.
Infix Operator (<$>): f <$> x is identical to fmap f x.
Instances: List ([]), Maybe, Either e, and IO () are all standard Functors.
Functor Laws: Identity (fmap id == id) and Composition (fmap (f . g) == fmap f . fmap g).
Core Rules to Remember

Universal Mapping Interface: Map over lists, optional values, error containers, and async IO uniformly.

<$> Operator: Clean infix sugar for fmap (e.g. (+1) <$> Just 5 gives Just 6).
Live Interactive Example
Hit Run Code to see it liveMapping Over Diverse Functors
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Output Console
Click "Run Code" to view the rendered output.
How it works: fmap (+1) unpacked Just 5, computed 5+1=6, and re-wrapped in Just 6.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Double Wrapped Maybe with fmap
Apply `fmap (* 2) (Just 21)` and print `"Result: "` followed by its `show` string.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.