1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
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
-- | Like 'unlines', but does not append a trailing newline if there
-- is at least one line. For example:
--
-- > unlinesConcise ["A", "B"] == "A\nB"
-- > unlinesConcise [] == ""
--
-- Whereas:
--
-- > unlines ["A", "B"] == "A\nB\n"
-- > unlines [] == ""
--
-- This is closer to the behaviour of 'unwords', which does not append
-- a trailing space.
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
|