module Language.REST.Internal.Util where

import qualified Data.List as L

-- | @removeEqBy f xs ys@ removes elements from @xs@ and @ys@ such that for each
--  element x removed from xs, an element y is removed from ys such @f x y@.
--  In other words in the result @(xs', ys')@, there does not exist any @x@ in
--  @xs'@, @y@ in @ys'@ such that @f x y@.
removeEqBy :: (Eq a) => (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
removeEqBy :: (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
removeEqBy a -> a -> Bool
_ [] [a]
ys = ([], [a]
ys)
removeEqBy a -> a -> Bool
f (a
x : [a]
xs) [a]
ys
  | Just a
y <- (a -> Bool) -> [a] -> Maybe a
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (a -> a -> Bool
f a
x) [a]
ys
  = (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
forall a. Eq a => (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
removeEqBy a -> a -> Bool
f [a]
xs ([a] -> ([a], [a])) -> [a] -> ([a], [a])
forall a b. (a -> b) -> a -> b
$ a -> [a] -> [a]
forall a. Eq a => a -> [a] -> [a]
L.delete a
y [a]
ys
  | Bool
otherwise
  = let ([a]
xs', [a]
ys') = (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
forall a. Eq a => (a -> a -> Bool) -> [a] -> [a] -> ([a], [a])
removeEqBy a -> a -> Bool
f [a]
xs [a]
ys in (a
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
xs', [a]
ys')