Skip to content

Instantly share code, notes, and snippets.

@fes300
Created March 11, 2019 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fes300/31dbf14e2200b6945f8f244ee003a75a to your computer and use it in GitHub Desktop.
Save fes300/31dbf14e2200b6945f8f244ee003a75a to your computer and use it in GitHub Desktop.
overlap :: Int -> Int -> Int -> Int -> Boolean
overlap a b c d = (c < b && d > a) || (d > a && c < b)
instance tupleInt :: Semigroup IntTuple where
append (IntTuple (Tuple a b)) (IntTuple (Tuple c d)) =
if (overlap a b c d)
then IntTuple $ Tuple (min a c) (max b d)
else IntTuple $ Tuple a b
sanitizeTupleList' :: List IntTuple -> List IntTuple
sanitizeTupleList' Nil = Nil
sanitizeTupleList' (x:xs) = nub (getAggregateValue x xs : sanitizeTupleList' xs)
where
getAggregateValue :: IntTuple -> List IntTuple -> IntTuple
getAggregateValue tuple = foldl (\t1 -> \t2 -> append t1 t2) tuple
sanitizeTupleList :: List IntTuple -> SanitizedList
sanitizeTupleList = removeOverlapping <<< nub <<< sanitizeTupleList' where
removeOverlapping :: List IntTuple -> SanitizedList
removeOverlapping Nil = SanitizedList Nil
removeOverlapping l = SanitizedList $ filter (not $ isContained l) l
isContained :: List IntTuple -> IntTuple -> Boolean
isContained l' (IntTuple (Tuple a b)) = isJust $ find (\(IntTuple (Tuple c d)) -> ((a > c && b <= d) || (a >= c && b < d))) l'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment