Range consolidation: Difference between revisions

m
Line 645:
<lang haskell>import Data.List (intercalate, maximumBy, sort)
import Data.Ord (comparing)
 
------------------- RANGE CONSOLIDATION ------------------
 
consolidated :: [(Float, Float)] -> [(Float, Float)]
consolidated xs =
let go xy [] = [xy]
go xy@(x, y) abetc@((a, b) : etc)
| y >= b = xy : etc
| y >= a = (x, b) : etc
Line 656 ⟶ 658:
| a <= b = (a, b)
| otherwise = (b, a)
in foldr go [] (sort . fmap ab $ xs)
 
 
-- TEST -------------------------- TEST -------------------------
tests :: [[(Float, Float)]]
tests =
[ [],
, [(1.1, 2.2)],
, [(6.1, 7.2), (7.2, 8.3)],
, [(4, 3), (2, 1)],
, [(4, 3), (2, 1), (-1, -2), (3.9, 10)],
, [(1, 3), (-6, -1), (-4, -5), (8, 2), (-6, -6)]
]
 
Line 673 ⟶ 675:
main =
putStrLn $
tabulated
tabulated "Range consolidations:" showPairs showPairs consolidated tests
"Range consolidations:"
 
showPairs
showPairs
consolidated
tests
 
-- DISPLAY FORMATTING ------------------- DISPLAY FORMATTING ------------------
 
tabulated ::
tabulated :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
String ->
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
tabulated s xShow fxShow f xs =
let w =
let w = length $ maximumBy (comparing length) (xShow <$> xs)
rjust n c s = drop (length s) (replicate n c ++ s)$
maximumBy
in unlines $
let w = length $ maximumBy (comparing length) (xShow <$> xs)
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs
(xShow <$> xs)
rjust n c s = drop (length s) (replicate n c <> s)
in unlines $
s :
fmap
( ((<>) . rjust w ' ' . xShow)
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++<>) . fxShow . f)) xs
)
xs
 
showPairs :: [(Float, Float)] -> String
showPairs xs
| null xs = "[]"
| otherwise = '[' : intercalate ", " (showPair <$> xs) ++ "]"
'[' :
intercalate
", "
(showPair <$> xs)
<> "]"
 
showPair :: (Float, Float) -> String
showPair (a, b) = '(' : showNum a ++ ", " ++ showNum b ++ ")"
'(' :
showNum a
<> ", "
<> showNum b
<> ")"
 
showNum :: Float -> String
9,655

edits