module Test.MuCheck.Utils.Common where
import System.FilePath (splitExtension)
import System.Random
import Data.List
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]
genFileNames :: String -> [String]
genFileNames s = map newname [1..]
where (name, ext) = splitExtension s
newname :: Int -> String
newname i= name ++ "_" ++ show i ++ ext
replace :: Eq a => (a,a) -> [a] -> [a]
replace (o,n) lst = map replaceit lst
where replaceit v
| v == o = n
| otherwise = v
sample :: (RandomGen g) => g -> Int -> [t] -> [t]
sample _ 0 _ = []
sample g n xs = val : sample g' (n 1) (remElt idx xs)
where val = xs !! idx
(idx,g') = randomR (0, length xs 1) g
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