module Test.Extrapolate.Utils
( (+++)
, nubMerge
, nubMergeOn
, nubMergeBy
, foldr0
, fromLeft
, fromRight
, elemBy
)
where
import Data.Function (on)
nubMergeBy :: (a -> a -> Ordering) -> [a] -> [a] -> [a]
nubMergeBy cmp (x:xs) (y:ys) = case x `cmp` y of
LT -> x:nubMergeBy cmp xs (y:ys)
GT -> y:nubMergeBy cmp (x:xs) ys
EQ -> x:nubMergeBy cmp xs ys
nubMergeBy _ xs ys = xs ++ ys
nubMergeOn :: Ord b => (a -> b) -> [a] -> [a] -> [a]
nubMergeOn f = nubMergeBy (compare `on` f)
nubMerge :: Ord a => [a] -> [a] -> [a]
nubMerge = nubMergeBy compare
(+++) :: Ord a => [a] -> [a] -> [a]
(+++) = nubMerge
infixr 5 +++
foldr0 :: (a -> a -> a) -> a -> [a] -> a
foldr0 f z xs | null xs = z
| otherwise = foldr1 f xs
fromLeft :: Either a b -> a
fromLeft (Left x) = x
fromLeft _ = error "fromLeft: not a left"
fromRight :: Either a b -> b
fromRight (Right x) = x
fromRight _ = error "fromRight: not a right"
elemBy :: (a -> a -> Bool) -> a -> [a] -> Bool
elemBy (==) x = any (== x)