Collecting successes and errors

  • traverse
  • haskell
  • purescript
  • fp-ts
  • elm

traverse (“map with effects”)

Often we want to iterate through a collection of items, performing some effect for each item. This means we want some function that looks like

(a -> f b) -> t a -> result

where a -> f b is our effectful computation, t a is our collection (typically a list or an array of as) and result could take a few different shapes depending on the requirements of our program, especially in the common case when the effect f encapsulates some notion of failure (like TaskEither in fp-ts, or anything with ExceptT in its stack in Haskell).

A decision tree

wilt is from the Witherable type class in PureScript and fp-ts, and appears as mapEitherA in Haskell. Conceptually, “partitionMap with effects.” It’s a rarer bird than traverse.