Files
loop/src/Padding.hs
T

49 lines
1.0 KiB
Haskell

{-| Basic padding and "justification" of lists. -}
module Padding
( leftPad
, rightPad
, midPad
, midPadL
, rotU
, rotD
, rotListAt
)
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
rotListAt :: Eq a => a -> [a] -> [a]
rotListAt x' xs = f x' xs []
where
f x (y:ys) zs
| y == x = y:ys ++ zs
| otherwise = f x ys (y:zs)
f _ [] zs = zs