module Test.Framework.Utilities where
import Control.Arrow (first, second)
import Data.Function (on)
import Data.Maybe
import Data.Monoid
import Data.List (intercalate)
newtype K a = K { unK :: a }
secondsToMicroseconds :: Num a => a -> a
secondsToMicroseconds = (1000000*)
microsecondsToPicoseconds :: Num a => a -> a
microsecondsToPicoseconds = (1000000*)
listToMaybeLast :: [a] -> Maybe a
listToMaybeLast = listToMaybe . reverse
mappendBy :: Monoid b => (a -> b) -> a -> a -> b
mappendBy f = mappend `on` f
orElse :: Maybe a -> a -> a
orElse = flip fromMaybe
onLeft :: (a -> c) -> (a, b) -> (c, b)
onLeft = first
onRight :: (b -> c) -> (a, b) -> (a, c)
onRight = second
unlinesConcise :: [String] -> String
unlinesConcise = intercalate "\n"
mapAccumLM :: Monad m => (acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM _ acc [] = return (acc, [])
mapAccumLM f acc (x:xs) = do
(acc', y) <- f acc x
(acc'', ys) <- mapAccumLM f acc' xs
return (acc'', y:ys)
padRight :: Int -> String -> String
padRight desired_length s = s ++ replicate (desired_length length s) ' '
dropLast :: Int -> [a] -> [a]
dropLast n = reverse . drop n . reverse