Interruptible.hs 989 B

1234567891011121314151617181920212223242526272829303132333435
  1. {-# LANGUAGE TypeFamilies #-}
  2. module Control.Monad.Trans.Interruptible (
  3. module Control.Monad.Trans.Interruptible.Class,
  4. -- * Interruptible applications
  5. intercalateWith
  6. )where
  7. import Control.Monad.Trans.Interruptible.Class
  8. {- |
  9. Folds the second list with the function applied to the first,
  10. intercalating the evaluation. That is:
  11. @
  12. intercalateWith resume f [a00, a10, a20] [b1, b2] = do
  13. a01 <- resume (f b1) a00
  14. a11 <- resume (f b1) a10
  15. a21 <- resume (f b1) a20
  16. a02 <- resume (f b2) a11
  17. a12 <- resume (f b2) a21
  18. a22 <- resume (f b2) a31
  19. return [a02, a12, a22]
  20. @
  21. Usefull for consuming lazy sequences.
  22. The resume function is parametric for allowing resuming deeper Interruptible chains, with
  23. resume2, resume3, etc.
  24. -}
  25. intercalateWith :: Monad m => ((a -> t a) -> rsta -> m (rsta)) -> (b -> a -> t a) -> [b] -> [rsta] -> m [rsta]
  26. intercalateWith _ _ [] aa = return aa
  27. intercalateWith res f (b:bb) aa = do
  28. aa' <- mapM (res $ f b) aa
  29. intercalateWith res f bb aa'