{-# LANGUAGE BangPatterns #-}
module Data.Massiv.Core.Iterator
( loop
, loopM
, loopM_
, loopDeepM
) where
loop :: Int -> (Int -> Bool) -> (Int -> Int) -> a -> (Int -> a -> a) -> a
loop !init' condition increment !initAcc f = go init' initAcc
where
go !step !acc =
case condition step of
False -> acc
True -> go (increment step) (f step acc)
{-# INLINE loop #-}
loopM :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> a -> (Int -> a -> m a) -> m a
loopM !init' condition increment !initAcc f = go init' initAcc
where
go !step !acc =
case condition step of
False -> return acc
True -> f step acc >>= go (increment step)
{-# INLINE loopM #-}
loopM_ :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> (Int -> m a) -> m ()
loopM_ !init' condition increment f = go init'
where
go !step =
case condition step of
False -> return ()
True -> f step >> go (increment step)
{-# INLINE loopM_ #-}
loopDeepM :: Monad m => Int -> (Int -> Bool) -> (Int -> Int) -> a -> (Int -> a -> m a) -> m a
loopDeepM !init' condition increment !initAcc f = go init' initAcc
where
go !step !acc =
case condition step of
False -> return acc
True -> go (increment step) acc >>= f step
{-# INLINE loopDeepM #-}