Overview

17 Traversable and stacked monads

This chapter introduces Traverse as a tool for working with lists of elevated values, especially when mapping would leave you with the wrong nesting order. Using examples like parsing a comma-separated list of numbers, it shows how Traverse turns an IEnumerable<T> and a function returning an applicative into a single result in the applicative context, such as Option<IEnumerable<R>> or Validation<IEnumerable<R>>. The key idea is that Traverse flips the effects around so that the container effect ends up on the outside rather than buried inside the list.

The chapter then contrasts monadic and applicative traversal. With Option, a monadic traverse short-circuits on the first failure, which is useful when you only care whether the whole operation succeeds. With Validation, an applicative traverse is usually better because it evaluates every element and accumulates all errors, making it ideal for reporting multiple validation problems at once. The same pattern also applies to Task, where traverse can either run operations sequentially or in parallel, with the parallel applicative version being the default choice for independent work.

Finally, the chapter extends the idea beyond lists to single-value structures and stacked monads. It shows that types like Validation, Option, and Exceptional can also be made traversable, letting you swap the order of effects when needed. This becomes especially useful when combining asynchronous work with validation in real workflows, such as a transfer request handler that validates input, loads account state asynchronously, applies a debit, and saves the result. The overall message is that Traverse helps tame nested effects, but when stacks get too deep, it is often better to reduce the number of effects or design a type that combines them more cleanly.

Comparing Map and Traverse

Summary

  • If you have two monads, A and B, you might like to stack them up in values like A<B<T>> to combine the effects of both monads.
  • You can use Traverse to invert the order of monads in the stack.
  • Implementing the LINQ query pattern for such a stack allows you to combine A’s, B’s, and A<B<>>’s with relative ease.
  • Still, stacked monads tend to be cumbersome, so use them sparingly.

FAQ

What is a traversable structure in Chapter 17?A traversable structure is a container of values, such as IEnumerable<T>, for which you can apply a function returning an elevated value and “flip” the effects, producing something like A<Tr<R>> instead of Tr<A<R>>.
How does Traverse differ from Map?Map turns Tr<T> into Tr<A<R>>, leaving the applicative effect inside the traversable. Traverse instead returns A<Tr<R>>, moving the effect to the outside.
Why is Traverse useful when parsing a comma-separated list of numbers?Because parsing each item returns an Option<double>, and Traverse lets you turn IEnumerable<Option<double>> into a single Option<IEnumerable<double>>. That way, one failed parse makes the whole result None.
What is the monadic signature of Traverse described in the chapter?The general shape is Tr<T> → (T → A<R>) → A<Tr<R>>. In the example with Option, it becomes IEnumerable<T> → (T → Option<R>) → Option<IEnumerable<R>>.
What is the difference between TraverseM and TraverseA for Validation?TraverseM uses monadic flow and short-circuits on the first failure. TraverseA uses applicative flow and evaluates every item, so it accumulates all validation errors.
Why does the chapter define Traverse for Validation to default to the applicative version?Because for validation, accumulating all errors is usually the desired behavior. The chapter therefore points Traverse to TraverseA, while still keeping TraverseM available if short-circuiting is needed.
How does HarvestErrors use Traverse?It takes a list of validators, traverses them by applying each validator to the same input, and then maps the successful result back to the original value. This collects all validation errors in one place.
How is Traverse used with Task?With Task, TraverseA runs independent tasks in parallel, while TraverseM runs them sequentially. The default Traverse uses the applicative version so you can await a single task containing all results.
What problem are “stacked monads”?Stacked monads are nested types like Task<Validation<T>> or Validation<Task<T>>. They are hard to compose directly because you have to manage multiple effects at once, often with awkward noise or complex code.
How does Chapter 17 combine asynchrony and validation in the MakeTransfer example?It adapts the dependencies so they fit a Task<Validation<T>> workflow, then uses a LINQ query expression to compose validation, account lookup, debit, and save/publish steps. This keeps the code readable while handling both asynchronous work and validation errors.

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