{-# LANGUAGE TypeFamilies #-}

module Control.Monad.Trans.Interruptible (
  module Control.Monad.Trans.Interruptible.Class,
  -- * Interruptible applications
  intercalateWith
  )where

import Control.Monad.Trans.Interruptible.Class

{- |
Folds the second list with the function applied to the first,
intercalating the evaluation. That is:

@
intercalateWith resume f [a00, a10, a20] [b1, b2] = do
  a01 <- resume (f b1) a00
  a11 <- resume (f b1) a10
  a21 <- resume (f b1) a20
  a02 <- resume (f b2) a11
  a12 <- resume (f b2) a21
  a22 <- resume (f b2) a31
  return [a02, a12, a22]
@

Usefull for consuming lazy sequences.

The resume function is parametric for allowing resuming deeper Interruptible chains, with
resume2, resume3, etc.
-}
intercalateWith :: Monad m => ((a -> t a) -> rsta -> m (rsta)) -> (b -> a -> t a) -> [b] -> [rsta] -> m [rsta]
intercalateWith _ _ [] aa = return aa
intercalateWith res f (b:bb) aa = do
  aa' <- mapM (res $ f b) aa
  intercalateWith res f bb aa'