module Test.MuCheck.Utils.Common where
import System.Random
import Data.List
import Data.Time.Clock.POSIX (getPOSIXTime)
import Control.Monad (liftM)
import qualified Data.Hashable as H
choose :: [a] -> Int -> [[a]]
choose xs n = filter (\x -> length x == n) $ subsequences xs
coupling :: Eq a => (a -> a -> t) -> [a] -> [t]
coupling fn ops = [fn o1 o2 | o1 <- ops, o2 <- ops, o1 /= o2]
replaceFst :: Eq a => (a,a) -> [a] -> [a]
replaceFst _ [] = []
replaceFst (o, n) (v:vs)
| v == o = n : vs
| otherwise = v : replaceFst (o,n) vs
sample :: (RandomGen g) => g -> Int -> [t] -> [t]
sample _ 0 _ = []
sample _ n xs | length xs <= n = xs
sample g n xs = val : sample g' (n 1) (remElt idx xs)
where val = xs !! idx
(idx,g') = randomR (0, length xs 1) g
rSample :: Int -> [t] -> IO [t]
rSample n t = do g <- genRandomSeed
return $ sample g n t
sampleF :: (RandomGen g) => g -> Rational -> [t] -> [t]
sampleF g f xs = sample g l xs
where l = round $ f * fromIntegral (length xs)
remElt :: Int -> [a] -> [a]
remElt idx xs = front ++ ack
where (front,_:ack) = splitAt idx xs
swapElts :: Int -> Int -> [t] -> [t]
swapElts i j ls = [get k x | (k, x) <- zip [0..length ls 1] ls]
where get k x | k == i = ls !! j
| k == j = ls !! i
| otherwise = x
genSwapped :: [t] -> [[t]]
genSwapped lst = map (\(x:y:_) -> swapElts x y lst) swaplst
where swaplst = choose [0..length lst 1] 2
genRandomSeed :: IO StdGen
genRandomSeed = liftM (mkStdGen . round) getPOSIXTime
curryM :: (t1 -> t2 -> m t) -> (t1, t2) -> m t
curryM fn (a,b) = fn a b
hash :: String -> String
hash s = (if h < 0 then "x" else "y") ++ show (abs h)
where h = H.hash s