Created
March 11, 2019 10:00
-
-
Save fes300/31dbf14e2200b6945f8f244ee003a75a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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