>>> traverse Just [1,2,3,4] Just [1,2,3,4]
>>> traverse id [Right 1, Right 2, Right 3, Right 4] Right [1,2,3,4]In the next examples, we show that Nothing and Left values short circuit the created structure.
>>> traverse (const Nothing) [1,2,3,4] Nothing
>>> traverse (\x -> if odd x then Just x else Nothing) [1,2,3,4] Nothing
>>> traverse id [Right 1, Right 2, Right 3, Right 4, Left 0] Left 0
traverseWithKey f = fmap (map id) . Data.HashMap.Lazy.traverseWithKey fNote: the order in which the actions occur is unspecified. In particular, when the map contains hash collisions, the order in which the actions associated with the keys involved will depend in an unspecified way on their insertion order.
>>> Just 1 ^.. traversed [1]traversed is the same as traverse, but can be faster thanks to magic rewrite rules.
traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(1, 'a'), (5, 'e')]) == Just (fromList [(1, 'b'), (5, 'f')]) traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) (fromList [(2, 'c')]) == Nothing
>>> traverse_ print ["Hello", "world", "!"] "Hello" "world" "!"
bitraverse f g ≡ bisequenceA . bimap f gFor a version that ignores the results, see bitraverse_.
>>> bitraverse listToMaybe (find odd) (Left []) Nothing
>>> bitraverse listToMaybe (find odd) (Left [1, 2, 3]) Just (Left 1)
>>> bitraverse listToMaybe (find odd) (Right [4, 5]) Just (Right 5)
>>> bitraverse listToMaybe (find odd) ([1, 2, 3], [4, 5]) Just (1,5)
>>> bitraverse listToMaybe (find odd) ([], [4, 5]) Nothing
>>> bitraverse_ print (print . show) ("Hello", True) "Hello" "True"
>>> bitraverse_ print (print . show) (Right True) "True"
>>> bitraverse_ print (print . show) (Left "Hello") "Hello"