{-# LANGUAGE BangPatterns #-}
module Graphics.Image.Utils
( loop
, loopM_
, (.:)
, (.:!)
, swapIx
) where
(.:) :: (a -> b) -> (c -> d -> a) -> (c -> d -> b)
(.:) f g = \ x y -> f (g x y)
{-# INLINE (.:) #-}
(.:!) :: (a -> b) -> (c -> d -> a) -> (c -> d -> b)
(.:!) f g = \ !x !y -> let !z = g x y in f z
{-# INLINE (.:!) #-}
loop :: t -> (t -> Bool) -> (t -> t) -> a -> (t -> 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 => t -> (t -> Bool) -> (t -> t) -> (t -> 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_ #-}
swapIx :: (a, b) -> (b, a)
swapIx !(i, j) = (j, i)
{-# INLINE swapIx #-}