Files
loop/src/Padding.hs
T

40 lines
845 B
Haskell

{-| Basic padding and "justification" of lists. -}
module Padding
( leftPad
, rightPad
, midPad
, midPadL
, rotU
, rotD
)
where
leftPad :: Int -> a -> [a] -> [a]
{-# INLINE leftPad #-}
leftPad i x xs = reverse $ take i $ reverse (take i xs) ++ repeat x
rightPad :: Int -> a -> [a] -> [a]
{-# INLINE rightPad #-}
rightPad i x xs = take i $ xs ++ repeat x
midPad :: Int -> a -> [a] -> [a] -> [a]
{-# INLINE midPad #-}
midPad i x xs ys = xs ++ replicate j x ++ ys
where
j = i - (length xs + length ys)
midPadL :: Int -> a -> [a] -> [a] -> [a]
{-# INLINE midPadL #-}
midPadL i x xs ys = take j (xs ++ repeat x) ++ ys
where
j = i - length ys
rotU :: [a] -> [a]
{-# INLINE rotU #-}
rotU [] = []
rotU (x:xs) = xs ++ [x]
rotD :: [a] -> [a]
{-# INLINE rotD #-}
rotD [] = []
rotD xs = last xs : init xs