Overview

6 Patterns in functional programming

This chapter introduces a set of core functional programming patterns as simple, reusable functions that appear across many data structures. It begins by showing how familiar operations such as mapping, filtering, and iteration are not tied to lists alone, but can be applied to other containers like Option as well. The key idea is that once you recognize the pattern, you can reuse the same mental model across different types and libraries.

The chapter then explains Map, ForEach, and Bind in detail. Map applies a pure function to the inner value(s) of a structure, whether that structure is an IEnumerable or an Option. ForEach is the side-effecting counterpart that works with actions rather than functions, while Bind is used when the function being applied already returns a wrapped value, preventing nested structures such as Option<Option<T>> or nested lists. This leads naturally to the concepts of functors and monads, with Option and IEnumerable serving as the main examples.

Finally, the chapter broadens the discussion to abstraction itself, distinguishing between regular values and elevated values that carry effects such as optionality, aggregation, laziness, or asynchrony. It emphasizes that many useful functions stay within a single abstraction, while others cross between regular and elevated levels, and that choosing the right function depends on whether you are transforming data, chaining effectful computations, or handling side effects. The chapter closes by showing how these patterns make code more composable, expressive, and easier to reason about, especially when working with optional data and collections together.

Mapping a function over an IEnumerable. This operation yields a list containing the result of applying the given function to each item in the source list.
Mapping a function over an Option
Your aunt bakes the pie, but only if the apples are in the basket!
Bind takes a source list and a function returning a list for each item of the source list. It returns a flat list, whereas Map returns a list of lists.
Return lifts a regular into an elevated value.
Functions classified in relation to levels of abstractions
Map in terms of regular vs. elevated values
Bind in terms of regular vs. elevated values

Summary

  • Structures like Option<T> and IEnumerable<T> can be seen as containers or abstractions, allowing you to work more effectively with the underlying values of type T.
  • You can distinguish between regular values (say, T) and elevated values (like Option<T> or IEnumerable<T>).
  • The core functions of FP that allow you to work effectively with elevated values include
    • Return, which takes a regular value and lifts it into an elevated value
    • Map, which applies a function to the inner value(s) of a structure and returns a new structure wrapping the result
    • ForEach, a side-effecting variant of Map, which takes an action that it performs for each of the container’s inner values
    • Bind, which maps an Option-returning function onto an Option and flattens the result to avoid producing a nested Option, similarly for IEnumerable and other structures
    • Where, which filters the inner value(s) of a structure according to a given predicate
  • Types for which Map is defined are called functors. Types for which Return and Bind are defined are called monads.

FAQ

What are the core functions introduced in Chapter 6?They are Return, Map, Bind, Where, and ForEach. The chapter presents them as common functional patterns that show up across different data structures.
What does Map do in functional programming?Map applies a function to the inner value(s) of a structure and returns a new structure containing the transformed result. For example, it can map over an IEnumerable or an Option.
How is Map related to LINQ’s Select?For IEnumerable, Map is equivalent to LINQ’s Select. The chapter treats them as synonyms, using Map as the standard FP name.
How does Map work with Option?If the Option is None, Map returns None. If it is Some, Map applies the function to the inner value and wraps the result in Some.
What is a functor?A functor is a type for which a suitable Map function is defined. In the chapter, both IEnumerable and Option are examples of functors.
Why is ForEach used instead of Map for side effects?Map is meant for pure transformations, while ForEach is used when you want to perform an Action and trigger side effects. The chapter recommends keeping side effects isolated to ForEach.
What problem does Bind solve?Bind is used when the function you want to apply already returns an elevated value, such as Option<R> or IEnumerable<R>. It prevents nested structures like Option<Option<T>> or IEnumerable<IEnumerable<T>>.
How is Bind different from Map?Map applies a regular function T → R, while Bind applies an upward-crossing function T → C<R>. Bind also flattens the result, which is why it is used with functions that already return a container.
What is a monad according to this chapter?A monad is a type that has both a Return function and a Bind function. These let you lift values into the type and chain computations without creating nested structures.
What is the relationship between functors and monads?Every monad is also a functor, because Map can be implemented in terms of Bind and Return. But not every functor is a monad, because some types support Map but not a meaningful Bind.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Functional Programming in C#, Second Edition Functional Programming in C#, Second Edition ebook for free