Learning path

Full curriculum

Full curriculum

Unit content

Mapping over computational context

Suppose a value is wrapped in some context such as optionality, failure or a collection. A plain function

$$f:A\to B$$

cannot be applied directly as if the wrapped value were an ordinary $A$.

A map operation lifts the function so it transforms the contained value while preserving the surrounding context.

Conceptually,

Context<A> + (A → B) → Context<B>

Optional values

Mapping over

Some(3)

with square produces

Some(9)

while mapping over None leaves it as None.

Results that may fail

If a successful result contains a value, map transforms it. If the result already contains an error, that error passes through unchanged.

Collections

For a list, map applies the function independently to every element and returns another list.

These examples have different meanings, but they share one structural idea: transform successful or contained values without manually unpacking and rebuilding the context every time.

Limitation of map

If the function itself returns another contextual value,

$$f:A\to Context,$$

ordinary map produces a nested result such as Context<Context<B>>. Flattening that nested context in a principled way leads to flatMap or bind.