Unfolding alongside folding

Thinking about fusing indexed maps for lists led me to consider the following function, which generalizes mapWithIndex to allow arbitrary stateful computation foldWithUnfold :: (a -> s -> Maybe (b, s)) -> [a] -> s -> [b] foldWithUnfold f = go where go (a : as) s | Just (b, s') <- f a s = b : go as s' go _ _ = [] which we can fit into fold/build fusion like so: foldWithUnfold f as s0 = build $ \c n -> let go a k s | Just (b, s') <- f a s = b `c` k s' | otherwise = n in foldr go (const n) as s0 Does a function of this general nature exist in some package already? Also, I see that the type can be expressed foldWithUnfoldS :: (a -> StateT s Maybe b) -> [a] -> s -> [b] Is that a better or worse way to say it?
participants (1)
-
David Feuer