Map applies an ordinary function to a value inside a monadic context, producing a new context while preserving capabilities such as laziness, nullability, asynchrony, or sequencing. section 11.3 section 11.0
static class LazyExtensions
{
public static Lazy<R> Map<T, R>(this Lazy<T> x, Func<T, R> func) =>
new Lazy<R>(() => func(x.Value));
}
A Lazy<T> Map implementation delays evaluation and applies the function only when the resulting Lazy<R> is evaluated. section 11.3
Mapping a lazy value
Given a Lazy<int> and a regular function such as DivTwo, Map produces a Lazy<double>. The function remains ordinary; Map supplies the laziness around it. Applying Map to a function that already returns Lazy<double>, however, produces Lazy<Lazy<double>>, an unnecessarily nested context. section 11.3
Map generalizes the same transformation across several computational contexts:
- Nullable<T> preserves nullability while mapping a value. section 11.3
- Task<T> preserves asynchrony while mapping a completed result. section 11.3
- IEnumerable<T> maps elements while retaining sequence structure. section 11.3
- Bind handles functions that already return monadic values; Map can be derived by binding after Unit wraps the ordinary result. section 11.3 section 11.5







