module Data.List.Match.Private where
import Data.Maybe (fromJust, isNothing, )
import Data.Maybe.HT (toMaybe, )
import Data.Tuple.HT (mapFst, forcePair, )
import Data.Bool.HT (if', )
import qualified Data.List as List
import Prelude hiding (take, drop, splitAt, replicate, )
take :: [b] -> [a] -> [a]
take = zipWith (flip const)
drop :: [b] -> [a] -> [a]
drop xs ys =
map fromJust (dropWhile isNothing
(zipWith (toMaybe . null) (iterate laxTail xs) ys))
drop' :: [b] -> [a] -> [a]
drop' xs ys =
map snd (dropWhile (not . null . fst) (zip (iterate laxTail xs) ys))
drop'' :: [b] -> [a] -> [a]
drop'' xs ys =
snd $ head $
dropWhile (not . null . fst) $
zip (iterate laxTail xs) (iterate laxTail ys)
drop''' :: [b] -> [a] -> [a]
drop''' (_:xs) (_:ys) = drop''' xs ys
drop''' _ ys = ys
laxTail :: [a] -> [a]
laxTail = List.drop 1
propTake :: (Eq a) => [b] -> [a] -> Bool
propTake xs ys =
take xs ys == List.take (length xs) ys
propDrop :: (Eq a) => [b] -> [a] -> Bool
propDrop xs ys =
drop xs ys == List.drop (length xs) ys
propDropAlt :: (Eq a) => [b] -> [a] -> Bool
propDropAlt xs ys =
drop xs ys == drop' xs ys &&
drop xs ys == drop'' xs ys &&
drop xs ys == drop''' xs ys
propTakeDrop :: (Eq a) => [b] -> [a] -> Bool
propTakeDrop xs ys =
take xs ys ++ drop xs ys == ys
splitAt :: [b] -> [a] -> ([a],[a])
splitAt nt xt =
forcePair $
case (nt,xt) of
(_:ns, x:xs) -> mapFst (x:) $ splitAt ns xs
(_, xs) -> ([],xs)
propSplitAt :: (Eq a) => [b] -> [a] -> Bool
propSplitAt xs ys =
(take xs ys, drop xs ys) == splitAt xs ys
compareLength :: [a] -> [b] -> Ordering
compareLength (_:xs) (_:ys) = compareLength xs ys
compareLength [] [] = EQ
compareLength (_:_) [] = GT
compareLength [] (_:_) = LT
compareLength' :: [a] -> [b] -> Ordering
compareLength' xs ys =
let boolList zs = replicate zs True ++ repeat False
in uncurry compare (head
(dropWhile (uncurry (&&)) (zip (boolList xs) (boolList ys))))
compareLength'' :: [a] -> [b] -> Ordering
compareLength'' xs ys =
compare (length xs) (length ys)
propCompareLength :: [Integer] -> [Int] -> Bool
propCompareLength xs ys =
compareLength xs ys == compareLength' xs ys &&
compareLength xs ys == compareLength'' xs ys
lessOrEqualLength :: [a] -> [b] -> Bool
lessOrEqualLength [] _ = True
lessOrEqualLength _ [] = False
lessOrEqualLength (_:xs) (_:ys) = lessOrEqualLength xs ys
shorterList :: [a] -> [a] -> [a]
shorterList xs ys =
let useX = lessOrEqualLength xs ys
in zipWith (if' useX) xs ys
shorterListEq :: (Eq a) => [a] -> [a] -> [a]
shorterListEq xs ys =
let useX = lessOrEqualLength xs ys
in zipWith (\x y -> if' (x==y || useX) x y) xs ys
replicate :: [a] -> b -> [b]
replicate xs y =
take xs (repeat y)