module Utils where import System.Random import Text.Read shuffle :: StdGen -> [a] -> [a] shuffle g xs = shuffle' (randoms g) xs shuffle' :: [Int] -> [a] -> [a] shuffle' _ [] = [] shuffle' (i:is) xs = let (firsts, rest) = splitAt (1 + i `mod` length xs) xs in (last firsts) : shuffle' is (init firsts ++ rest) chunksOf :: Int -> [a] -> [[a]] chunksOf n [] = [] chunksOf n xs = take n xs : chunksOf n (drop n xs) query :: Read a => String -> IO a query s = do putStrLn s l <- fmap readMaybe getLine case l of Just x -> return x Nothing -> query s remove :: (a -> Bool) -> [a] -> (a, [a]) remove pred xs = foldr f (undefined, []) xs where f c (old, cs) = if pred c then (c, cs) else (old, c : cs) filterMap :: (a -> Bool) -> (a -> b) -> [a] -> [b] filterMap pred f as = foldr g [] as where g a bs = if pred a then f a : bs else bs