[Haskell-cafe] Unfolding alongside folding

David Feuer david.feuer at gmail.com
Mon Aug 19 10:14:20 UTC 2019


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190819/51d15988/attachment.html>


More information about the Haskell-Cafe mailing list